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

huangapple go评论57阅读模式
英文:

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

问题

我想要这个git lg输出:

* 3333333 - 将特性分支合并到 `main`
* 2222222 - 其他一些提交
* 1111111 - 初始提交

如何做到这一点?您可以重新命名这个问题为:“如何查看git log,就像它是线性的一样”。

我有这个git lg别名。它很棒!通过运行以下命令来获取它:

# 从Coderwall添加`git lg`别名
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"

它的输出如下:

*   3333333 - 将特性分支合并到 `main`
|\
| * ccccccc - 特性分支提交 3
| * bbbbbbb - 特性分支提交 2
| * aaaaaaa - 特性分支提交 1
|/
* 2222222 - 其他一些提交
* 1111111 - 初始提交

在这里,我已经标记了两个分支mainfeature。提交111111122222223333333要么是直接提交到main,要么是合并提交到main。提交aaaaaaabbbbbbbccccccc是在feature分支上的提交。

**我想要一种“更宏观”的视图。我不关心每个特性分支中的单个提交。我只想看到这些“main”提交,**无论我运行git log还是git lg

* 3333333 - 将特性分支合并到 `main`
* 2222222 - 其他一些提交
* 1111111 - 初始提交

如何做到这一点?git lg --mergesgit log --merges更接近,但不完全正确。它显示如下:

* 3333333 - 将特性分支合并到 `main`

但是,我真正想要的是整个线性历史,包括所有直接提交到main或合并到main的提交。这就好像我强制执行了git rebase+squash+fast-forward合并,而不是git merge合并。

无论如何,您如何筛选并获得这个git lg输出呢?

* 3333333 - 将特性分支合并到 `main`
* 2222222 - 其他一些提交
* 1111111 - 初始提交
英文:

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 &quot;log --color --graph --pretty=format:&#39;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset&#39; --abbrev-commit&quot;

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

答案1

得分: 1

我认为你正在寻找git log命令的--first-parent选项。它确切地描述了你所寻找的内容。从git-log手册页面

--first-parent

在看到合并提交时只跟踪第一个父提交。当查看特定主题分支的演变时,此选项可以提供更好的概览,因为合并到主题分支的合并通常只涉及不时地调整以适应更新的上游内容,此选项允许你忽略由此类合并引入到你的历史记录中的个别提交。

以本地存储库为例,如果我运行以下命令:

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

我会得到以下输出:

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

但如果我添加了--first-parent选项("在查找要包括的提交时,只跟踪合并提交时的第一个父提交"):

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

我会得到以下输出:

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

我认为这就是你要找的行为。

英文:

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=&#39;%h&#39; 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=&#39;%h&#39; --first-parent HEAD~5..

I get:

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

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

答案2

得分: 0

Update: @larsks got it!

最终答案:如何显示git log的一般摘要

# 仅显示直接合并到`main`的主要提交,而不是合并到功能分支的子提交!
# - 这仅追踪合并提交上的左(第一个)父提交,
#   从而显示分支的更好“概述”或摘要

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

我的初始尝试:

这只是一个简单的部分答案。它仅适用于git lg,并要求我关心的提交从它们的git lg输出行以*字符开头:

git lg | grep '^*'
  • ^表示“行的开头”,以及
  • \*表示字面上的*字符

因此,这只是在行的开头查找字面上的*字符。

我仍然需要帮助找到更好的答案。

另请参阅

  1. 我刚刚发现这个答案也提到了--first-parent查看不包含合并提交的git日志
英文:

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 &quot;overview&quot; 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 &#39;^\*&#39;
  • ^ means "start of the line", and
  • \* means the * character, literally

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

<s>I still need help figuring out a better answer.</s>

See also

  1. I just discovered that this answer mentions --first-parent too: View git log without merge commits

huangapple
  • 本文由 发表于 2023年7月6日 11:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定