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
我认为您正在寻找
--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
我认为这就是您正在寻找的行为。
更新:@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 '^\*'
^
表示“行的开始”,并且\*
表示 *
字符,字面意思是所以,这只是在行首查找文字
*
字符。
我仍然需要帮助找出更好的答案。
git merge
风格的工作流程中,仅显示某人的独特提交在合并之前在他们的 PR(Pull Request)中有过。--first-parent
:View git log without merge commits