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?
1 Answer
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.
-
ok, so in the diagram [link]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?– IowaSep 3, 2013 at 5:09
-
and assuming i am working on a public repository, is it safe to do a git pull --rebase– IowaSep 3, 2013 at 5:14
-
No, C is the last local commit. Otherwise your understanding is correct (read: the same as mine) Sep 3, 2013 at 5:20
-
pull --rebase seems to be safe, see this answer to the question you posted yourself stackoverflow.com/a/11531552/66686 but I never used it so far. This page also look interesting, although I only skimmed it: notes.envato.com/developers/rebasing-merge-commits-in-git Sep 3, 2013 at 5:21
-
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. Apr 9, 2020 at 13:02