如何查看线性`git log`(不包括功能提交),仅显示对`main`的提交或合并到`main`

问题描述 投票:0回答:2

我的问题的快速总结

我有这个

git lg
输出(在下面的“详细信息”部分中获取此别名):

*   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

我想要这个

git lg
输出:

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

我怎样才能做到这一点?您可以将这个问题重新命名为:“如何查看

git log
,就好像它是 线性的一样”。

详情

我有这个

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

# 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"

其输出如下所示:

*   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

在这里,我标记了两个分支

main
feature
。提交
1111111
2222222
3333333
可以直接提交到
main
,也可以合并提交到
main
。提交
aaaaaaa
bbbbbbb
ccccccc
是在
feature
分支上的提交:

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

我想要一个“更大的图景”。我不关心每个功能分支中的单独提交。我只想看到这些“主要”提交,无论我运行

git log
还是
git lg

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

我怎样才能做到这一点?

git lg --merges
git log --merges
更接近,但不正确。它显示了这个:

* 3333333 - merge of feature branch into `main` 

但是,我真正想要的是整个线性历史,所有提交都直接提交到

main
,或合并到
main
。那将是就好像我通过强制执行
git log
+挤压+快进合并而不是
git rebase
合并来压缩线性
git merge
历史。

无论如何,我怎样才能过滤并得到这个

git lg
输出?

* 3333333 - merge of feature branch into `main` 
* 2222222 - some other commit
* 1111111 - initial commit
git git-log
2个回答
1
投票

我认为您正在寻找

--first-parent
git log
选项。它准确地描述了您正在寻找的内容。来自
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

我认为这就是您正在寻找的行为。


0
投票

更新:@larsks 明白了!

最终答案:如何显示
git log
总体摘要

# 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

我最初的尝试:

这是一个俗气的部分答案。它仅适用于

git lg
,并且它要求我关心的提交以
git lg
字符开始其
*
输出行:

git lg | grep '^\*'
  • ^
    表示“行的开始”,并且
  • \*
    表示
    *
    字符,字面意思是

所以,这只是在行首查找文字

*
字符。

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

另请参阅

  1. 对于这个问题的相反,您可能只想看到针对给定合并提交的功能分支提交,请参阅我的问答:
    git merge
    风格的工作流程中,仅显示某人的独特提交在合并之前在他们的 PR(Pull Request)中有过
  2. 我刚刚发现这个答案也提到了
    --first-parent
    View git log without merge commits
© www.soinside.com 2019 - 2024. All rights reserved.