使用 Git,显示一个分支中的所有提交,但不显示其他分支中的所有提交

发布于 2024-08-10 12:45:27 字数 151 浏览 6 评论 0原文

我有一个旧分支,我想删除它。但是,在这样做之前,我想检查对此分支所做的所有提交是否在某个时刻合并到其他分支中。因此,我想查看对当前分支所做的所有提交,这些提交尚未应用于任何其他分支[或者,如果没有一些脚本就不可能做到这一点,那么如何查看一个分支中尚未应用的所有提交到另一个给定的分支?]。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

你げ笑在眉眼 2024-08-17 12:45:27

要查看一个分支上而非另一个分支上的提交列表,请使用 git log:

git log --no-merges oldbranch ^newbranch

...也就是说,显示旧分支上不在新分支上的所有提交的提交日志。您可以列出要包含和排除的多个分支,例如

git log  --no-merges oldbranch1 oldbranch2 ^newbranch1 ^newbranch2

注意:在 Windows 命令提示符(不是 Powershell)上 ^ 是转义键,因此需要使用另一个 ^ 进行转义:

git log --no-merges oldbranch ^^newbranch

To see a list of which commits are on one branch but not another, use git log:

git log --no-merges oldbranch ^newbranch

...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.

git log  --no-merges oldbranch1 oldbranch2 ^newbranch1 ^newbranch2

Note: on Windows command prompt (not Powershell) ^ is an escape key, so it needs to be escaped with another ^:

git log --no-merges oldbranch ^^newbranch
离线来电— 2024-08-17 12:45:27

您可能只想

git branch --contains branch-to-delete

列出包含“要删除的分支”提交的所有分支。
如果它报告的不仅仅是“要删除的分支”,则该分支已被合并。

您的替代方案实际上只是 rev-list 语法。例如 git log one-branch..another-branch 显示 one-branch 需要拥有 another-branch 拥有的一切。

您可能还对 git show-branch 感兴趣,它是一种查看内容在哪里的方法。

You probably just want

git branch --contains branch-to-delete

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 that one-branch needs to have everything another-branch has.

You may also be interested in git show-branch as a way to see what's where.

吾家有女初长成 2024-08-17 12:45:27

要显示 oldbranch 但不在 newbranch 中的提交:

git log newbranch..oldbranch

要显示这些提交的差异(注意有三个点):

git diff newbranch...oldbranch

这是带有图表说明的文档 https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges

To show the commits in oldbranch but not in newbranch:

git log newbranch..oldbranch

To show the diff by these commits (note there are three dots):

git diff newbranch...oldbranch

Here is the doc with a diagram illustration https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges

暗恋未遂 2024-08-17 12:45:27

对于那些仍在寻找简单答案的人,请查看 gitcherry。它比较实际的差异而不是提交哈希。这意味着它可以容纳经过精心挑选或重新调整的提交。

首先签出要删除的分支:

git checkout [branch-to-delete]

,然后使用 gitcherry 将其与主开发分支进行比较:

gitcherry -v master

输出示例:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message
- 85867e38712de930864c5edb7856342e1358b2a0 Yet another message

注意:-v 标志将包含提交消息以及 SHA 哈希值。

前面带有“+”的行位于要删除的分支中,但不是主分支中。那些前面有“-”的人在 master 中有一个等效的提交。

对于不在 master 中的提交,请将cherry pick 与grep 结合起来:

gitcherry -v master | grep "^\+"

示例输出:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message

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:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message
- 85867e38712de930864c5edb7856342e1358b2a0 Yet another message

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:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message
安稳善良 2024-08-17 12:45:27

虽然此处发布的一些答案将有助于找到您所寻求的内容,但gitbranch的以下子命令是更适合您的任务的解决方案。

--merged 用于找到所有可以安全删除的分支,因为这些分支完全包含在 HEAD 中。

master 中,可以运行命令来枚举可以安全删除的分支,如下所示:

git branch --merged
  develop
  fpg_download_links
* master
  master_merge_static

# Delete local and remote tracking branches you don't want
git branch -d fpg_download_links
git push origin :fpg_download_links
git branch -d master_merge_static
git push origin :master_merge_static

# There is also a flag to specify remote branches in the output
git branch --remotes --merged

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:

git branch --merged
  develop
  fpg_download_links
* master
  master_merge_static

# Delete local and remote tracking branches you don't want
git branch -d fpg_download_links
git push origin :fpg_download_links
git branch -d master_merge_static
git push origin :master_merge_static

# There is also a flag to specify remote branches in the output
git branch --remotes --merged
久而酒知 2024-08-17 12:45:27

jimmyorr 的答案在 Windows 上不起作用。使用 --not 代替 ^ 会有所帮助,如下所示:

git log oldbranch --not newbranch --no-merges

jimmyorr's answer does not work on Windows. it helps to use --not instead of ^ like so:

git log oldbranch --not newbranch --no-merges
中性美 2024-08-17 12:45:27

如果您需要检查的是一个(单个)分支,例如,如果您希望分支“B”完全合并到分支“A”中,则只需执行以下操作:

$ git checkout A
$ git branch -d B

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 checkout A
$ git branch -d B

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.

℉絮湮 2024-08-17 12:45:27

我也想计算提交,因此具体操作方法如下:

计算当前分支 (HEAD) 上有多少提交,但不在 上master

git log --oneline ^master HEAD | wc -l

wc -l 表示“字数统计”——计算“行”的数量。

当然,要查看整个日志消息,正如其他答案所给出的那样:

git log ^master HEAD

...或以压缩的 --oneline 形式:

git log --oneline ^master HEAD

如果您也不想计算合并提交,则可以排除那些带有 --no-merges 的:

git log --oneline --no-merges ^master HEAD | wc -l

等等。

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 on master:

git log --oneline ^master HEAD | wc -l

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:

git log ^master HEAD

...or in a condensed --oneline form:

git log --oneline ^master HEAD

If you don't want to count merge commits either, you can exclude those with --no-merges:

git log --oneline --no-merges ^master HEAD | wc -l

etc.

倾听心声的旋律 2024-08-17 12:45:27

您可以使用这个简单的脚本来查看未合并的提交

#!/bin/bash
# Show commits that exists only on branch and not in current
# Usage:
#   git branch-notmerge <branchname>
#
# Setup git alias
#   git config alias.branch-notmerge [path/to/this/script]
grep -Fvf <(git log --pretty=format:'%H - %s') <(git log $1 --pretty=format:'%H - %s')

您还可以使用工具 git-wtf 将显示分支的状态

You can use this simple script to see commits that are not merged

#!/bin/bash
# Show commits that exists only on branch and not in current
# Usage:
#   git branch-notmerge <branchname>
#
# Setup git alias
#   git config alias.branch-notmerge [path/to/this/script]
grep -Fvf <(git log --pretty=format:'%H - %s') <(git log $1 --pretty=format:'%H - %s')

You can use also tool git-wtf that will display state of branches

几度春秋 2024-08-17 12:45:27

显示 other-branch 中不在当前分支中的提交和提交内容:

git show @..other-branch

此外,您可以将 other-branch 中的提交直接应用到当前分支:

git cherry-pick @..other-branch

Show commits and commit contents from other-branch that are not in your current branch:

git show @..other-branch

Additionally you can apply the commits from other-branch directly to your current branch:

git cherry-pick @..other-branch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文