git: Why "Merge branch 'master' of ... "? when pull and push

39,577

Solution 1

When you did a git-pull, modifications from the remote branch were merged into your local branch. The automatically generated commit signifies that.

Merging could have resulted in conflicts, which would then need to be resolved manually. In your particular case, this did not happen and git could take care of everything.

Solution 2

If there could be changes by others, it might be a good idea to do a git pull --rebase (i.e., add your new changes after the remote changes; this might find conflicts you'd have to resolve) and then git push the result. Be careful, this creates new commits that did never exist before (as does any history rewriting), but it gives a clean, linear history (no tangle of merges)

Share:
39,577
Nullptr
Author by

Nullptr

Updated on August 31, 2020

Comments

  • Nullptr
    Nullptr over 3 years

    I'm still git newbie. I modified some source files and committed. Then, I did git push. But, I got this error.

    To /foo/bar/  ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to '/foo/bar/' To prevent you from
    losing history, non-fast-forward updates were rejected Merge the
    remote changes before pushing again.  See the 'Note about
    fast-forwards' section of 'git push --help' for details.
    

    This reject seems that I didn't git pull before push. So, I did git pull. Okay, there were two modified files by others.

    Then, I was able to git push successfully.

    Question: In this case, I'm seeing one more log like following with my original commit message:

    commit 59e04ce13b8afa...
    Merge: 64240ba 76008a5
    Author: Jone Doe <[email protected]>
    Date:   Fri Mar 15 11:08:55 2013 -0700
    
        Merge branch 'master' of /foo/bar/
    

    And this is my original commit message.

    commit 64240bafb07705c...
    Author: Jone Doe <[email protected]>
    Date:   Fri Mar 15 11:06:18 2013 -0700
    
        Fixed bugs and updated!
    

    I'd like to understand why "merge branch master of location" is added.

  • kalvn
    kalvn over 6 years
    Is there a way to avoid such additional commit? For example by including the merging modifications in the current commit (Fixed bugs and updated! above)? Because it kind of pollutes the log history.
  • r.v
    r.v over 6 years
    Git will try to do a fast-forward merge if the remote branch is ahead of the local branch, i.e., the remote branch has some commits on top of the commits of the local branch. In this case there is no additional commit. The additional commit appears only when the histories of the two branches diverge. In such a case you can rewrite the commit history of your local branch using git rebase or git pull --rebase, performing a fast-forward merge. Rebasing has its own downsides though and extra merge commits are not always bad -- they are just recording the course of development.
  • kalvn
    kalvn over 6 years
    Thanks for the explanation :)