使用 Git,显示一个分支中的所有提交,但不显示其他分支中的所有提交
我有一个旧分支,我想删除它。但是,在这样做之前,我想检查对此分支所做的所有提交是否在某个时刻合并到其他分支中。因此,我想查看对当前分支所做的所有提交,这些提交尚未应用于任何其他分支[或者,如果没有一些脚本就不可能做到这一点,那么如何查看一个分支中尚未应用的所有提交到另一个给定的分支?]。
I have an old branch, which I would like to delete. However, before doing so, I want to check that all commits made to this branch were at some point merged into some other branch. Thus, I'd like to see all commits made to my current branch which have not been applied to any other branch [or, if this is not possible without some scripting, how does one see all commits in one branch which have not been applied to another given branch?].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要查看一个分支上而非另一个分支上的提交列表,请使用 git log:
...也就是说,显示旧分支上不在新分支上的所有提交的提交日志。您可以列出要包含和排除的多个分支,例如
注意:在 Windows 命令提示符(不是 Powershell)上
^
是转义键,因此需要使用另一个^
进行转义:To see a list of which commits are on one branch but not another, use git log:
...that is, show commit logs for all commits on oldbranch that are not on newbranch. You can list multiple branches to include and exclude, e.g.
Note: on Windows command prompt (not Powershell)
^
is an escape key, so it needs to be escaped with another^
:您可能只想
列出包含“要删除的分支”提交的所有分支。
如果它报告的不仅仅是“要删除的分支”,则该分支已被合并。
您的替代方案实际上只是 rev-list 语法。例如
git log one-branch..another-branch
显示one-branch
需要拥有another-branch
拥有的一切。您可能还对
git show-branch
感兴趣,它是一种查看内容在哪里的方法。You probably just want
This will list all branches which contain the commits from "branch-to-delete".
If it reports more than just "branch-to-delete", the branch has been merged.
Your alternatives are really just rev-list syntax things. e.g.
git log one-branch..another-branch
shows everything thatone-branch
needs to have everythinganother-branch
has.You may also be interested in
git show-branch
as a way to see what's where.要显示 oldbranch 但不在 newbranch 中的提交:
要显示这些提交的差异(注意有三个点):
这是带有图表说明的文档 https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges
To show the commits in oldbranch but not in newbranch:
To show the diff by these commits (note there are three dots):
Here is the doc with a diagram illustration https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges
对于那些仍在寻找简单答案的人,请查看 gitcherry。它比较实际的差异而不是提交哈希。这意味着它可以容纳经过精心挑选或重新调整的提交。
首先签出要删除的分支:
git checkout [branch-to-delete]
,然后使用 gitcherry 将其与主开发分支进行比较:
gitcherry -v master
输出示例:
注意:
-v
标志将包含提交消息以及 SHA 哈希值。前面带有“+”的行位于要删除的分支中,但不是主分支中。那些前面有“-”的人在 master 中有一个等效的提交。
对于不在 master 中的提交,请将cherry pick 与grep 结合起来:
gitcherry -v master | grep "^\+"
示例输出:
For those still looking for a simple answer, check out git cherry. It compares actual diffs instead of commit hashes. That means it accommodates commits that have been cherry picked or rebased.
First checkout the branch you want to delete:
git checkout [branch-to-delete]
then use git cherry to compare it to your main development branch:
git cherry -v master
Example output:
Note: The
-v
flag is to include the commit message along with the SHA hash.Lines with the '+' in front are in the branch-to-delete, but not the master branch. Those with a '-' in front have an equivalent commit in master.
For JUST the commits that aren't in master, combine cherry pick with grep:
git cherry -v master | grep "^\+"
Example output:
虽然此处发布的一些答案将有助于找到您所寻求的内容,但gitbranch的以下子命令是更适合您的任务的解决方案。
--merged 用于找到所有可以安全删除的分支,因为这些分支完全包含在 HEAD 中。
在
master
中,可以运行命令来枚举可以安全删除的分支,如下所示:While some of the answers posted here will help find what you seek, the following sub-command of git branch is a more suitable solution for your task.
--merged is used to find all branches which can be safely deleted, since those branches are fully contained by HEAD.
While in
master
one could run the command to enumerate the branches one could safely remove, like so:jimmyorr 的答案在 Windows 上不起作用。使用
--not
代替^
会有所帮助,如下所示:jimmyorr's answer does not work on Windows. it helps to use
--not
instead of^
like so:如果您需要检查的是一个(单个)分支,例如,如果您希望分支“B”完全合并到分支“A”中,则只需执行以下操作:
gitbranch -d
具有“分支必须完全合并到 HEAD 中”的安全性。警告:如果分支 B 合并到 A 中,这实际上会删除分支 B。
If it is one (single) branch that you need to check, for example if you want that branch 'B' is fully merged into branch 'A', you can simply do the following:
git branch -d <branchname>
has the safety that "The branch must be fully merged in HEAD."Caution: this actually deletes the branch B if it is merged into A.
我也想计算提交,因此具体操作方法如下:
计算当前分支 (
HEAD
) 上有多少提交,但不在上master
:wc -l
表示“字数统计”——计算“行”的数量。当然,要查看整个日志消息,正如其他答案所给出的那样:
...或以压缩的
--oneline
形式:如果您也不想计算合并提交,则可以排除那些带有
--no-merges
的:等等。
I'd like to count the commits too, so here's how to do that:
Count how many commits are on the current branch (
HEAD
), but NOT onmaster
:wc -l
means "word count"--count the number of 'l'ines.And of course to see the whole log messages, as other answers have given:
...or in a condensed
--oneline
form:If you don't want to count merge commits either, you can exclude those with
--no-merges
:etc.
您可以使用这个简单的脚本来查看未合并的提交
您还可以使用工具 git-wtf 将显示分支的状态
You can use this simple script to see commits that are not merged
You can use also tool git-wtf that will display state of branches
显示
other-branch
中不在当前分支中的提交和提交内容:此外,您可以将
other-branch
中的提交直接应用到当前分支:Show commits and commit contents from
other-branch
that are not in your current branch:Additionally you can apply the commits from
other-branch
directly to your current branch: