How to view a linear `git log` (exclude feature commits), showing only commits to `main` or merges to `main`
Asked Answered
S

2

0

Quick summary of my question

I have this git lg output (get this alias in my "Details" section below):

*   3333333 - merge of feature branch into `main`
|\  
| * ccccccc - feature branch commit 3
| * bbbbbbb - feature branch commit 2
| * aaaaaaa - feature branch commit 1
|/  
* 2222222 - some other commit
* 1111111 - initial commit

I want this git lg output:

* 3333333 - merge of feature branch into `main` 
* 2222222 - some other commit
* 1111111 - initial commit

How can I do that? You might re-title this question: "how to view a git log as if it was linear".

Details

I have this git lg alias. It's great! Get it by running this:

# add `git lg` alias from Coderwall
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Its output looks like this:

*   3333333 - merge of feature branch into `main`
|\  
| * ccccccc - feature branch commit 3
| * bbbbbbb - feature branch commit 2
| * aaaaaaa - feature branch commit 1
|/  
* 2222222 - some other commit
* 1111111 - initial commit

Here, I have labeled the two branches main and feature. Commits 1111111, 2222222, and 3333333 are either direct commits to main, or merge commits to main. Commits aaaaaaa, bbbbbbb, and ccccccc are commits on feature branch:

v-branch `main`
  v-branch `feature`


*   3333333 - merge of feature branch into `main`
|\  
| * ccccccc - feature branch commit 3
| * bbbbbbb - feature branch commit 2
| * aaaaaaa - feature branch commit 1
|/  
* 2222222 - some other commit
* 1111111 - initial commit

I'd like a "bigger picture" sort of view. I don't care about the individual commits in each feature branch. I just want to see these "main" commits, whether I run git log or git lg:

* 3333333 - merge of feature branch into `main` 
* 2222222 - some other commit
* 1111111 - initial commit

How can I do that? git lg --merges or git log --merges is closer, but not right. It shows this:

* 3333333 - merge of feature branch into `main` 

But, what I really want is the whole linear history, with all commits directly to main, or merged to main. That would be as if I had a squashed linear git log history by enforcing git rebase+squash+fast-forward merges instead of git merge merges.

Anyway, so how can I filter and just get this git lg output?

* 3333333 - merge of feature branch into `main` 
* 2222222 - some other commit
* 1111111 - initial commit
Soup answered 6/7, 2023 at 3:44 Comment(1)
For the opposite of this question, where you might want to see only the feature branch commits for a given merge commit, see my Q&A here: In a git merge-style workflow, show only the unique commits someone had in their PR (Pull Request) before merging.Soup
E
1

I think you're looking for the --first-parent option to git log. It describes exactly what you're looking for. From the git-log man page:

--first-parent

Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge.

Using a local repository as an example, if I run:

git log --graph --format='%h' HEAD~5..

I get:

* 6abb395
*   cb88d06
|\  
| * aa53a16
|/  
*   0abcc50
|\  
| * 1e7bcc6
|/  
*   4e0ab62
|\  
| * fd16fb0
|/  
* 5d7f675
* 6941b71

But if I add the --first-parent option ("When finding commits to include, follow only the first parent commit upon seeing a merge commit"):

git log --graph --format='%h' --first-parent HEAD~5..

I get:

* 6abb395
* cb88d06
* 0abcc50
* 4e0ab62
* 5d7f675

And I think that's the behavior you were looking for.

Extenuatory answered 6/7, 2023 at 4:0 Comment(4)
I think you missed one: 6941b71 would be below 5d7f675 in the output.Soup
That's it! git log even states this outright, describing exactly the "bigger picture" sort of view I wanted to see!: --first-parent Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge.Soup
@GabrielStaples this is the actual, complete output of the given git log command. I did not edit it in any way.Extenuatory
I see. I've noticed that some git log commands, depending on the options, including git lg, exclude the initial commit for some reason.Soup
S
0

Update: @larsks got it!

Final answer: how to show a git log general summary

# Show only the primary commits directly to `main`, not the sub-commits on 
# feature branches merged in!
# - this tracks down only the left (first) parent on merge commits,
#   thereby showing a better "overview" or summary of the branch

git lg --first-parent
git log --first-parent

My initial attempt:

This is a cheesy partial answer. It only works with git lg, and it requires that the commits I care about begin their git lg output lines with the * character:

git lg | grep '^\*'
  • ^ means "start of the line", and
  • \* means the * character, literally

So, this just looks for a literal * char at the start of a line.

I still need help figuring out a better answer.

See also

  1. For the opposite of this question, where you might want to see only the feature branch commits for a given merge commit, see my Q&A here: In a git merge-style workflow, show only the unique commits someone had in their PR (Pull Request) before merging.
  2. I just discovered that this answer mentions --first-parent too: View git log without merge commits
Soup answered 6/7, 2023 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.