简体   繁体   English

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

[英]Using Git, show all commits that are in one branch, but not the other(s)

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?].因此,我希望看到对我当前分支所做的所有提交都没有应用于任何其他分支[或者,如果没有一些脚本就不可能做到这一点,那么如何查看一个分支中尚未应用的所有提交到另一个给定的分支?]。

To see a list of which commits are on one branch but not another, use git log:要查看在一个分支上但不在另一个分支上的提交列表,请使用 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, eg您可以列出要包含和排除的多个分支,例如

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 ^ :注意:在 Windows 命令提示符(不是 Powershell)上^是一个转义键,所以它需要用另一个^转义:

git log --no-merges oldbranch ^^newbranch

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.如果它报告的不仅仅是“branch-to-delete”,则该分支已被合并。

Your alternatives are really just rev-list syntax things.您的替代方案实际上只是 rev-list 语法的东西。 eg git log one-branch..another-branch shows everything that one-branch needs to have everything another-branch has.例如git log one-branch..another-branch显示了one-branch需要拥有another-branch拥有的一切。

You may also be interested in git show-branch as a way to see what's where.您可能还对git show-branch感兴趣,因为它可以作为查看内容的一种方式。

To show the commits in oldbranch but not in newbranch:要在 oldbranch 中显示提交但不在 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这是带有图表说明的文档https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges

For those still looking for a simple answer, check out git cherry .对于那些仍在寻找简单答案的人,请查看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 将其与您的主要开发分支进行比较:

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.注意: -v标志将包含提交消息和 SHA 哈希。

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.前面带有“-”的那些在 master 中有一个等效的提交。

For JUST the commits that aren't in master, combine cherry pick with grep:对于不在 master 中的提交,将cherry pick 与grep 结合使用:

git cherry -v master | grep "^\\+"

Example output:示例输出:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message

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.虽然这里发布的一些答案将帮助您找到所需的内容,但git branch的以下子命令是更适合您的任务的解决方案。

--merged is used to find all branches which can be safely deleted, since those branches are fully contained by HEAD. --merged用于查找可以安全删除的所有分支,因为这些分支完全包含在 HEAD 中。

While in master one could run the command to enumerate the branches one could safely remove, like so:虽然在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

jimmyorr's answer does not work on Windows. jimmyorr 的答案在 Windows 上不起作用。 it helps to use --not instead of ^ like so:它有助于使用--not而不是^像这样:

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

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:如果您需要检查一个(单个)分支,例如,如果您希望分支“B”完全合并到分支“A”中,则只需执行以下操作:

$ git checkout A
$ git branch -d B

git branch -d <branchname> has the safety that "The branch must be fully merged in HEAD." git branch -d <branchname>具有“分支必须在 HEAD 中完全合并”的安全性。

Caution : this actually deletes the branch B if it is merged into A.注意:如果合并到 A 中,这实际上会删除分支 B。

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您还可以使用工具git-wtf来显示分支的状态

Show commits and commit contents from other-branch that are not in your current branch:显示不在当前分支中的other-branch提交和提交内容:

git show @..other-branch

Additionally you can apply the commits from other-branch directly to your current branch:此外,您可以将other-branch的提交直接应用到当前分支:

git cherry-pick @..other-branch

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 :计算当前分支( HEAD )上有多少提交,但不在master

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

wc -l means "word count"--count the number of 'l'ines. wc -l意思是“字数统计”——计算“行”的数量。

And of course to see the whole log messages, as other answers have given:当然,要查看整个日志消息,正如其他答案所给出的那样:

git log ^master HEAD

...or in a condensed --oneline form: ...或以浓缩的--oneline形式:

git log --oneline ^master HEAD

If you don't want to count merge commits either, you can exclude those with --no-merges :如果您也不想计算合并提交,您可以使用--no-merges排除那些:

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

etc.等等。

Just use git cherry to pick all commits in the branch newFeature42 for example:只需使用git cherry选择分支newFeature42中的所有提交,例如:

git cherry -v master newFeature42 gitcherry -v master newFeature42

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?].因此,我想查看对当前分支所做的所有提交,这些提交尚未应用到任何其他分支[或者,如果没有一些脚本编写就无法实现,那么如何查看一个分支中尚未应用的所有提交?到另一个给定的分支?]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Git,显示所有*仅*存在于一个特定分支上的提交,而不是*任何*其他分支 - Using Git, show all commits that exist *only* on one specific branch, and not *any* others Git-使用rebase将提交从一个分支提交到另一分支 - Git - applying commits from one branch onto the other using rebase GIT使用git cherry和format显示master分支中所有缺少的提交 - GIT show all commits from branch missing in master using git cherry and format 如何获取某个分支与Git中所有其他分支之间的提交数量差异? - How do I get the differences in the number of commits between one certain branch and all other ones in Git? 如何使用git找到一个分支中但不在另一个分支中的所有提交? - How can I find all commits that are in one branch but not in another using git? GIT:在master分支上将多个提交合并为一个,同时保留test分支上的所有提交 - GIT: Join multiple commits into one on master branch while preserving all commits on the testing branch git:如何将某个特定作者的所有提交重新绑定到一个单独的分支中? - git: How to rebase all commits by one certain author into a separate branch? 如何将分支中的所有 git 提交合并为一个提交 - How to merge all git commits in a branch, into one commit git branch stash提交到一个 - git branch stash commits into one 如何在一个分支的提交中“git bisect”? - How to `git bisect` only on one branch's commits?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM