Search code examples
gitgitlabmerge-conflict-resolution

Git (GitLab) - Merge Fails - Trying instructions for "Check out, review, merge locally"


I tried to do a merge from my user-branch to our shared "develop" branch. I'm the only working on this project. GitLab gave me the following instructions which I'm struggling with.

enter image description here

in Step 1 - when I did "git fetch origin" - I did it in the repository I already on my hard drive. Was that correct? If I try it in some newly made directory, it syas "fatal: Not a git repository (or any of the parent directories): .git

for the second part of Step 1 - I get the error: fatal: A branch name 'nw-ob210refactor' already exists.

So I tried adding a 2 on the end, and I get this error: fatal: 'origin/nw-ob210refactor2' is not a commit and a branch 'nw-ob210refactor ' cannot be created from it

Are the above instructions form GitLab complete and correct; or am I doing some obviously wrong.


Solution

  • git checkout -b localName origin/onlineName
    

    creates a new local branch. See man git checkout

    -b "new_branch"
    Creates the branch "new_branch" and start it at "start_point"; if it already exists, then reset it to "start_point". This is equivalent to running "git branch" with "-f"; see git-branch(1) for details

    if you aleady have the according branch local ofcourse you don't want to create it again. Instead use only

    git checkout nw-ob210refactor
    

    and than

    git pull
    

    If I try it in some newly made directory

    there you would have to do a fresh

    git clone https://your/repository
    

    first to download your repository

    So I tried adding a 2 on the end, and I get this error: fatal: 'origin/nw-ob210refactor2' is not a commit and a branch 'nw-ob210refactor ' cannot be created from it

    if something and you want to create a new local branch you have to change the parameter after -b (= the local brnach name) not the origin (remote branch name) you are trying to download:

    git checkout -b checkout nw-ob210refactor2 origin/checkout nw-ob210refactor
    

    Update

    Once you are Already up to date go on with step 3

    do also there either

    git checkout -b develop origin/develop
    

    if the develop branch does not exist local so far. Otherwise again use only

    git checkout develop
    

    to only switch to it. In that case to be sure again run

    git pull
    

    Now you have both branches local and up-to-date and are ready to merge

    git merge --no-ff nw-ob210refactor
    

    This merges nw-ob210refactor into develop. If you need it the otherway round simply switch to the target branch (checkout) and exchange the name.

    --no-ff reads no fast forward and means

    Create a merge commit even when the merge resolves as a fast-forward. This is the default behaviour when merging an annotated (and possibly signed) tag that is not stored in its natural place in refs/tags/ hierarchy.

    or in simple words you want to create a special commit that will have the message merge from branch nw-ob210refactor into develop even though there are no conflicts and you actually could merge the branches without an additional commit. This is most of time useful to have a better overview what was merged when in the history and makes it easier to revert stuff later in case you need to.


    after merging push back the according branch you merged to. In this example it was develop so do

    git push origin develop