Skip to content

Instantly share code, notes, and snippets.

@lava
Last active November 21, 2018 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lava/8d0fd58a871c47bd7f5ba5445a084f50 to your computer and use it in GitHub Desktop.
Save lava/8d0fd58a871c47bd7f5ba5445a084f50 to your computer and use it in GitHub Desktop.
Git history and merge commits

Testing the --no-ff merge strategy

I created a sample git repository by creating branches second and third adding a file with the same name each, and then rebasing the branches and merging them using git merge --no-ff:

bevers@poincare:~/code/git$ ls
initial  second  third

bevers@poincare:~/code/git$ git log --graph --oneline
*   044c523 (HEAD -> master) Merge branch 'third'
|\  
| * 080a881 Third.
|/  
*   ab286d7 Merge branch 'second'
|\  
| * 8e5204a Add second.
|/  
* 66ee9d4 (first2) Initial.

Below I list several observations.

Behaviour of HEAD

bevers@poincare:~/code/git$ git log --oneline
044c523 (HEAD -> master) Merge branch 'third'
080a881 Third.
ab286d7 Merge branch 'second'
8e5204a Add second.
66ee9d4 Initial.

bevers@poincare:~/code/git$ git show --oneline HEAD~1
ab286d7 Merge branch 'second'

I would have expected HEAD~1 to point at 080a881.

Viewing the contents of commits

bevers@poincare:~/code/git$ git log --stat --oneline
044c523 (HEAD -> master) Merge branch 'third'
080a881 Third.
 third | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
ab286d7 Merge branch 'second'
8e5204a Add second.
 second | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
66ee9d4 (first2) Initial.
 initial | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

bevers@poincare:~/code/git$ git log --stat --oneline --merges
044c523 (HEAD -> master) Merge branch 'third'
ab286d7 Merge branch 'second'

bevers@poincare:~/code/git$ git log -p --oneline --merges
044c523 (HEAD -> master) Merge branch 'third'
ab286d7 Merge branch 'second'

This also has implications for tasks like finding out the PR in which a particular file was last touched: Because the merge commit itself does not change the file, it does not show up in git blame etc. and we would have to walk the log in reverse direction to get the PR number:

bevers@poincare:~/code/git$ git log --oneline -- second
8e5204a Add second.

Showing a history with only "correct" merges

For this I tried adding new branch fourth (basing off master) and initial2 (basing off the initial commit) where initial2 is merged into fourth before fourth is merged into master using --no-ff.

bevers@poincare:~/code/git$ git log --graph --oneline
*   35fc573 (HEAD -> master) Merge branch 'fourth'
|\  
| *   195e250 (fourth) Merge branch 'initial2' into fourth
| |\  
| | * 509c299 (initial2) Add initial2.
| * | c133ca1 Add fourth.
|/ /  
* |   044c523 Merge branch 'third'
|\ \  
| * | 080a881 Third.
|/ /  
* |   ab286d7 Merge branch 'second'
|\ \  
| |/  
|/|   
| * 8e5204a Add second.
|/  
* 66ee9d4 Initial.

It looks like this makes it very hard to view a history with only the merge commits where branches where merged into master:

35fc573 (HEAD -> master) Merge branch 'fourth'
195e250  Merge branch 'initial2' into fourth
044c523 Merge branch 'third'
ab286d7 Merge branch 'second'

Bisecting on merge commits

I wanted to try this as well since it came up in the comments, but I don't know how to do it. Suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment