39

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 <jone@doe.com>
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 <jone@doe.com>
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.

Nullptr
  • 3,103
  • 4
  • 27
  • 28
  • Possible duplicate of [Git pull results in extraneous "Merge branch" messages in commit log](http://stackoverflow.com/questions/8509396/git-pull-results-in-extraneous-merge-branch-messages-in-commit-log), which has a lot more detailed answers – steve cook Jul 04 '16 at 22:58

2 Answers2

30

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.

r.v
  • 4,697
  • 6
  • 35
  • 57
  • 1
    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. – kalvn Mar 30 '17 at 08:25
  • 8
    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. – r.v Apr 08 '17 at 01:02
  • Thanks for the explanation :) – kalvn Apr 26 '17 at 14:20
13

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)

vonbrand
  • 11,412
  • 8
  • 32
  • 52