0

NOTE: The solution presented below (which does not work as expected) was found by searching SO :-)

I want to list all commits of current branch, ignoring merges from other branch, like this:

$ git status
On branch 2021050704
Your branch is up to date with 'origin/2021050704'.

nothing to commit, working tree clean
$ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
*   51fa955 2021-05-07 | schema update (HEAD -> 2021050704, origin/2021050704) [xrfang]
|\  
| * 5b542c5 2021-05-07 | schema update (origin/2021050703, 2021050703) [xrfang]
|/  
* b69d581 2021-05-07 | removed redundant row_format specifier [xrfang]
* 39685bb 2021-05-06 | added collect_invasion series [xrfang]
$ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short --no-merges
* 5b542c5 2021-05-07 | schema update (origin/2021050703, 2021050703) [xrfang]
* b69d581 2021-05-07 | removed redundant row_format specifier [xrfang]
* 39685bb 2021-05-06 | added collect_invasion series [xrfang]

Now the problem is, with --no-merge, it get rid of 51fa955, but what I want is to eliminate 5b542c5, which is from branch 2021050703, and my current branch is 2021050704.

xrfang
  • 1,754
  • 4
  • 18
  • 36

2 Answers2

0

I just found out after a minute that --first-parent is exactly what I need:

$ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short --first-parent
* 51fa955 2021-05-07 | schema update (HEAD -> 2021050704, origin/2021050704) [xrfang]
* b69d581 2021-05-07 | removed redundant row_format specifier [xrfang]
* 39685bb 2021-05-06 | added collect_invasion series [xrfang]
xrfang
  • 1,754
  • 4
  • 18
  • 36
0

Note that commit 5b542c5 is on your branch. In Git, commits are often on multiple branches, and a merge commit typically causes one or more commits that were not on the branch before, to be on the branch now.

That aside, the --first-parent option is probably what you want. It tells Git that, upon encountering a merge, Git should follow only the first parent commit of that merge commit, ignoring second and any additional parents. Since the first parent of a new merge commit made by git merge is the commit that was the tip of the branch before running git merge, this will get the stream of commits you want—provided the commits were made by running git merge on the intended branch.

This will malfunction (sort of—by definition it's correct, but usually these aren't the commits you wanted) if someone used git pull to make what some people refer to as a foxtrot merge.

torek
  • 448,244
  • 59
  • 642
  • 775