Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git merge sometimes create a commit, sometimes does not?

Tags:

git

People also ask

Does git merge always create commit?

In these scenarios, git merge takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new "merge commit" that combines the changes of each queued merge commit sequence.

Does a merge create a new commit?

Merging your branch into master is the most common way to do this. Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Why does git pull create a merge commit?

This usually happens when we're collaborating on a branch with other people, and we've made changes on our local version of a branch, and someone else (or the other you, if you use git to sync between multiple dev platforms) has made changes to the remote version of a branch in the meantime.


So-called "Fast-forward" merges don't produce a commit, whereas other merges (often refered to as "octopus merge" (now you see why github's mascott is an octocat)) produce commits.

Basically, a Fast-forward happens when your branches did not diverge.

Say you want to merge a branch foo in the master branch. If these branches did not diverge, you would have an history like this (each * represents a commit):

*---* (master)
     \
      *---*---* (foo)

In this situation, the merge is fast-forward because (according to the graph theory, which is the underlying foundation of a git graph), master is reachable from foo. In other words, you just have to move the master reference to foo, and you're done:

*---*
     \
      *---*---* (master, foo)

When your branches diverge:

*---*---* (master)
     \
      *---*---* (foo)

You have to create a commit to "join" the two branches:

                ↓
*---*---*-------* (master)
     \         / 
      *---*---* (foo)

The commit pointed by the arrow is the merge commit and has two parent commits (the former master branch tip, and the current foo branch tip).

Note that you can force Git to create a merge commit for a fast-forward merge with the --no-ff option.

I highly recommend reading http://think-like-a-git.net/ for a good understanding of how the graph theory applies to git (you don't need to know the graph theory, everything you need to know is on the website), which will make working with Git incredibly easier :)


You can use the --no-ff option to force a new commit to avoid the fast-forward.


A fast forward means that a commit has already happened and is stored in your log, and your HEAD (pointer) has moved forward to that commit. You can check out merge behavior here


When no fast-forward --no-ff option is presented git will not create a commit if the head of the branch you are merging in is the ancestor of the merged branch. In this case (no --no-ff option) it will just move the head (it's a fast-forward).