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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1710894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:19:45  来源:igfitidea点击:

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

gitbranchgit-branch

提问by sircolinton

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

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

采纳答案by Dustin

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. e.g. git log one-branch..another-branchshows everything that one-branchneeds to have everything another-branchhas.

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

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

您可能也有兴趣git show-branch作为查看内容的一种方式。

回答by jimmyorr

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 noton newbranch. You can list multiple branches to include and exclude, e.g.

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

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

Note: on Windows ^is an escape key, so it needs to be escaped with another ^:

注意:在 Windows 上^是一个转义键,所以它需要用另一个转义^

git log --no-merges oldbranch ^^newbranch

回答by Xuan

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

回答by Tim S

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.

对于那些仍在寻找简单答案的人,请查看git cherry。它比较实际差异而不是提交哈希。这意味着它可以容纳已被挑选或重新定位的提交。

First checkout the branch you want to delete:

首先签出要删除的分支:

git checkout [branch-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

git cherry -v master

Example output:

示例输出:

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

Note: The -vflag 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 "^\+"

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

Example output:

示例输出:

+ 8a14709d08c99c36e907e47f9c4dacebeff46ecb Commit message
+ b30ccc3fb38d3d64c5fef079a761c7e0a5c7da81 Another commit message

回答by Freddie

While some of the answers posted here will help find what you seek, the following sub-command of git branchis a more suitable solution for your task.

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

--mergedis used to find all branches which can be safely deleted, since those branches are fully contained by HEAD.

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

While in masterone 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

回答by sebeck

jimmyorr's answerdoes not work on Windows. it helps to use --notinstead of ^like so:

jimmyorr 的答案在 Windows 上不起作用。它有助于使用--not而不是^这样:

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

回答by Jakub Nar?bski

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。

回答by manRo

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  --pretty=format:'%H - %s')

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

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

回答by M?ltus

Just use git cherryto pick all commits in the branch newFeature42for example:

仅用于git cherry选择分支中的所有提交,newFeature42例如:

git cherry -v master newFeature42

gitcherry -v master newFeature42

回答by pkamb

Start to Create a Pull Requestvia the git hosting service you're using. If the branch has been fully merged into the base branch, you'll be unable to create the new PR.

开始通过您正在使用的 git 托管服务创建拉取请求。如果分支已完全合并到基础分支中,您将无法创建新的 PR。

You don't need to actually make the pull request, just use the first step where you pick branches.

您不需要实际发出拉取请求,只需使用选择分支的第一步。

For example, on GitHub:

例如,在 GitHub 上:

There isn't anything to compare

没有什么可以比较的

Can't create a PR for branches that have been merged.

无法为已合并的分支创建 PR。

This doesn't use git on the command line, but I often find it's helpful to use the other tools at your disposal with a clear mental model rather than attempt to remember another arcane git command.

这不会在命令行上使用 git,但我经常发现使用其他工具和清晰的思维模型会很有帮助,而不是试图记住另一个神秘的 git 命令。