git 我如何知道一个分支是否已经合并到 master 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/226976/
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 05:54:04  来源:igfitidea点击:

How can I know if a branch has been already merged into master?

git

提问by hectorsq

I have a git repository with multiple branches.

我有一个带有多个分支的 git 存储库。

How can I know which branches are already merged into the master branch?

我怎么知道哪些分支已经合并到主分支了?

回答by hectorsq

git branch --merged masterlists branches merged into master

git branch --merged master列出合并到master中的分支

git branch --mergedlists branches merged into HEAD(i.e. tip of current branch)

git branch --merged列出合并到HEAD 的分支(即当前分支的尖端)

git branch --no-mergedlists branches that have not been merged

git branch --no-merged列出尚未合并的分支

By default this applies to only the local branches. The -aflag will show both local and remote branches, and the -rflag shows only the remote branches.

默认情况下,这仅适用于本地分支。该-a标志将显示本地和远程分支,并且该-r标志仅显示远程分支。

回答by Greg Hewgill

You can use the git merge-basecommand to find the latest common commit between the two branches. If that commit is the same as your branch head, then the branch has been completely merged.

您可以使用该git merge-base命令查找两个分支之间的最新公共提交。如果该提交与您的分支负责人相同,则该分支已完全合并。

Note that git branch -ddoes this sort of thing already because it will refuse to delete a branch that hasn'talready been completely merged.

需要注意的是git branch -d做这样的事情已经因为它会拒绝删除该分支还没有已经完全合并。

回答by iberbeu

There is a graphical interface solution as well. Just type

还有一个图形界面解决方案。只需输入

gitk --all

gitk --all

A new application window will prompt with a graphical representation of your whole repo, where it is very easy to realize if a branch was already merged or not

一个新的应用程序窗口将提示您整个 repo 的图形表示,很容易意识到分支是否已经合并

回答by Carl G

I am using the following bash function like: git-is-merged develop feature/new-feature

我正在使用以下 bash 函数,例如: git-is-merged develop feature/new-feature

git-is-merged () {
  merge_destination_branch=
  merge_source_branch=

  merge_base=$(git merge-base $merge_destination_branch $merge_source_branch)
  merge_source_current_commit=$(git rev-parse $merge_source_branch)
  if [[ $merge_base = $merge_source_current_commit ]]
  then
    echo $merge_source_branch is merged into $merge_destination_branch
    return 0
  else
    echo $merge_source_branch is not merged into $merge_destination_branch
    return 1
  fi
}

回答by Hari

Use git merge-base <commit> <commit>.

使用git merge-base <commit> <commit>.

This command finds best common ancestor(s) between two commits. And if the common ancestor is identical to the last commit of a "branch" ,then we can safely assume that that a "branch" has been already merged into the master.

此命令查找两次提交之间的最佳公共祖先。如果共同祖先与“分支”的最后一次提交相同,那么我们可以安全地假设“分支”已经合并到主节点中。

Here are the steps

以下是步骤

  1. Find last commit hash on master branch
  2. Find last commit hash on a "branch"
  3. Run command git merge-base <commit-hash-step1> <commit-hash-step2>.
  4. If output of step 3 is same as output of step 2, then a "branch" has been already merged into master.
  1. 在主分支上查找最后一个提交哈希
  2. 在“分支”上查找最后一次提交哈希
  3. 运行命令git merge-base <commit-hash-step1> <commit-hash-step2>
  4. 如果第 3 步的输出与第 2 步的输出相同,则“分支”已经合并到 master 中。

More info on git merge-base https://git-scm.com/docs/git-merge-base.

有关 git merge-base https://git-scm.com/docs/git-merge-base 的更多信息。

回答by avivamg

In order to verify which branches are merged into master you should use these commands:

为了验证哪些分支合并到 master 中,您应该使用以下命令:

  • git branch <flag[-r/-a/none]> --merged masterlist of all branches merged into master.
  • git branch <flag[-r/-a/none]> --merged master | wc -lcount number of all branches merged into master.
  • git branch <flag[-r/-a/none]> --merged master合并到 master 的所有分支的列表。
  • git branch <flag[-r/-a/none]> --merged master | wc -l计算合并到 master 中的所有分支的数量。

Flags Are:

标志是:

  • -aflag - (all)showing remote and local branches
  • -rflag - (remote)showing remote branches only
  • <emptyFlag>- showing localbranches only
  • -a标志 - (全部)显示远程和本地分支
  • -r标志 - (远程)仅显示远程分支
  • <emptyFlag>-只显示本地分行

for example:git branch -r --merged masterwill show you all remote repositories merged into master.

例如:git branch -r --merged master将显示所有合并到 master 中的远程存储库。

回答by xxjjnn

On the topic of cleaning up remote branches

关于清理远程分支的话题

git branch -r | xargs -t -n 1 git branch -r --contains

This lists each remote branch followed by which remote branches their latest SHAs are within.

这列出了每个远程分支,然后是其最新 SHA 所在的远程分支。

This is useful to discern which remote branches have been merged but not deleted, and which haven't been merged and thus are decaying.

这有助于辨别哪些远程分支已合并但未删除,哪些尚未合并因此正在衰减。

If you're using 'tig' (its like gitk but terminal based) then you can

如果您使用的是“tig”(它类似于 gitk 但基于终端),那么您可以

tig origin/feature/someones-decaying-feature

to see a branch's commit history without having to git checkout

无需 git checkout 即可查看分支的提交历史记录

回答by angularsen

Here are my techniques when I need to figure out if a branch has been merged, even if it may have been rebased to be up to date with our main branch, which is a common scenario for feature branches.

这是我需要确定分支是否已合并时的技术,即使它可能已重新定位以与我们的主分支保持同步,这是功能分支的常见场景。

Neither of these approaches are fool proof, but I've found them useful many times.

这些方法都不是万无一失的,但我发现它们很多次都很有用。

1 Show log for all branches

1 显示所有分支的日志

Using a visual tool like gitk or TortoiseGit, or simply git log with --all, go through the history to see all the merges to the main branch. You should be able to spot if this particular feature branch has been merged or not.

使用诸如 gitk 或 TortoiseGit 之类的可视化工具,或者简单地使用 --all git log,查看历史记录以查看所有合并到主分支的情况。您应该能够发现此特定功能分支是否已合并。

2 Always remove remote branch when merging in a feature branch

2 合并功能分支时始终删除远程分支

If you have a good habit of always removing both the local and the remote branch when you merge in a feature branch, then you can simply update and prune remotes on your other computer and the feature branches will disappear.

如果你有一个好的习惯,当你在一个特性分支中合并时总是删除本地和远程分支,那么你可以简单地在你的另一台计算机上更新和修剪远程,特性分支就会消失。

To help remember doing this, I'm already using git flow extensions (AVH edition)to create and merge my feature branches locally, so I added the following git flow hook to ask me if I also want to auto-remove the remote branch.

为了帮助记住这样做,我已经在使用git flow 扩展(AVH 版)在本地创建和合并我的功能分支,所以我添加了以下 git flow 钩子来询问我是否还想自动删除远程分支。

Example create/finish feature branch

示例创建/完成功能分支

554 Andreas:MyRepo(develop)$ git flow start tmp
Switched to a new branch 'feature/tmp'

Summary of actions:
- A new branch 'feature/tmp' was created, based on 'develop'
- You are now on branch 'feature/tmp'

Now, start committing on your feature. When done, use:

     git flow feature finish tmp

555 Andreas:MyRepo(feature/tmp)$ git flow finish
Switched to branch 'develop'
Your branch is up-to-date with 'if/develop'.
Already up-to-date.

[post-flow-feature-finish] Delete remote branch? (Y/n)
Deleting remote branch: origin/feature/tmp.

Deleted branch feature/tmp (was 02a3356).

Summary of actions:
- The feature branch 'feature/tmp' was merged into 'develop'
- Feature branch 'feature/tmp' has been locally deleted
- You are now on branch 'develop'

556 Andreas:ScDesktop (develop)$

.git/hooks/post-flow-feature-finish

.git/hooks/post-flow-feature-finish

NAME=
ORIGIN=
BRANCH=

# Delete remote branch
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

while true; do
  read -p "[post-flow-feature-finish] Delete remote branch? (Y/n) " yn
  if [ "$yn" = "" ]; then
    yn='Y'    
  fi
  case $yn in
      [Yy] ) 
        echo -e "\e[31mDeleting remote branch: /.\e[0m" || exit "$?"
        git push  :; 
        break;;
      [Nn] ) 
        echo -e "\e[32mKeeping remote branch.\e[0m" || exit "$?"
        break;;
      * ) echo "Please answer y or n for yes or no.";;
  esac
done

# Stop reading user input (close STDIN)
exec <&-
exit 0

3 Search by commit message

3 按提交消息搜索

If you do not always remove the remote branch, you can still search for similar commits to determine if the branch has been merged or not. The pitfall here is if the remote branch has been rebased to the unrecognizable, such as squashing commits or changing commit messages.

如果您不总是删除远程分支,您仍然可以搜索类似的提交以确定该分支是否已合并。这里的陷阱是远程分支是否已重新定位到无法识别的位置,例如压缩提交或更改提交消息。

  • Fetch and prune all remotes
  • Find message of last commit on feature branch
  • See if a commit with same message can be found on master branch
  • 获取并修剪所有遥控器
  • 在功能分支上查找上次提交的消息
  • 查看是否可以在 master 分支上找到具有相同消息的提交

Example commands on master branch:

master 分支上的示例命令:

gru                   
gls origin/feature/foo
glf "my message"

In my bash .profile config

在我的 bash .profile 配置中

alias gru='git remote update -p'
alias glf=findCommitByMessage

findCommitByMessage() {
    git log -i --grep=""
}

回答by radke

Here is a little one-liner that will let you know if your current branch incorporates or is out of data from a remote origin/master branch:

这是一个简单的单行代码,可以让您知道您当前的分支是否包含来自远程源/主分支的数据:

$ git fetch && git branch -r --merged | grep -q origin/master && echo Incorporates origin/master || echo Out of date from origin/master

I came across this question when working on a feature branch and frequently wanting to make sure that I have the most recent work incorporated into my own separate working branch.

我在处理功能分支时遇到了这个问题,并且经常想确保我将最近的工作合并到我自己的独立工作分支中。

To generalize this test I have added the following alias to my ~/.gitconfig:

为了概括这个测试,我在 ~/.gitconfig 中添加了以下别名:

[alias]
   current = !git branch -r --merged | grep -q  && echo Incorporates  || echo Out of date from  && :

Then I can call:

然后我可以打电话:

$ git current origin/master

to check if I am current.

检查我是否当前。