30

I went through couple of blogs to understand a merge commit, but not completely clear as to what it is and what is the best practice to avoid it. And the tree diagram of the git log added further confusion to my understanding of merge commits. Help?

Iowa
  • 2,171
  • 4
  • 22
  • 31

1 Answers1

27

A merge commit is just like another commit, the state of your repository at a given point in time plus the history it evolved from.

The one thing special about a merge commit is that it has at least two predecessors, therefore a plain vanilla diff doesn't make sense, you can only compare the merge with one of the predecessor, which yields the changes from the other parent(s)

To avoid merge commits, you can rebase your changes before pushing them to a remote repository.

rethab
  • 7,170
  • 29
  • 46
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • ok, so in the diagram [link]http://stackoverflow.com/q/5860944/2663585 , A B and C are local commits and A was the latest local change and then git fetch was done. Am i right? – Iowa Sep 03 '13 at 05:09
  • and assuming i am working on a public repository, is it safe to do a git pull --rebase – Iowa Sep 03 '13 at 05:14
  • No, C is the last local commit. Otherwise your understanding is correct (read: the same as mine) – Jens Schauder Sep 03 '13 at 05:20
  • pull --rebase seems to be safe, see this answer to the question you posted yourself http://stackoverflow.com/a/11531552/66686 but I never used it so far. This page also look interesting, although I only skimmed it: http://notes.envato.com/developers/rebasing-merge-commits-in-git/ – Jens Schauder Sep 03 '13 at 05:21
  • So does a merge commit come simply from something like 'git checkout branch', 'git merge master'? – learning2learn Apr 09 '20 at 12:44
  • 2
    @learning2learn Yes, the git merge command creates merge commits by default, except when a "fast forward merge" is possible. It is possible to configure git so that it avoids merge commit and does a rebase instead. It is also possible to configure it to make merge commits, even when there a fast forward is possible. At least I think it is. – Jens Schauder Apr 09 '20 at 13:02
  • Thank you @JensSchauder, that helped me complete some usage guidelines at work regarding our GitLab setup. My takeaway is it seems preferred to avoid merge commits and as you suggested, that seems to be the default configuration on our project. – learning2learn Apr 13 '20 at 00:02
  • If not, to avoid merge commits, you can [squash](https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---squash) them into one single commits, of course loosing some details. – andreagalle Sep 13 '22 at 12:42