简体   繁体   English

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

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

I have a git repository with multiple branches.我有一个带有多个分支的 git 存储库。

How can I know which branches are already merged into the master branch?我怎么知道哪些分支已经合并到主分支了?

git branch --merged master lists branches merged into master git branch --merged master列出合并到master 的分支

git branch --merged lists branches merged into HEAD (ie tip of current branch) git branch --merged列出合并到HEAD 的分支(即当前分支的提示)

git branch --no-merged lists branches that have not been merged git branch --no-merged列出尚未合并的分支

By default this applies to only the local branches.默认情况下,这仅适用于本地分支。 The -a flag will show both local and remote branches, and the -r flag shows only the remote branches. -a标志将显示本地和远程分支,而-r标志仅显示远程分支。

You can use the git merge-base command to find the latest common commit between the two branches.您可以使用git merge-base命令查找两个分支之间的最新公共提交。 If that commit is the same as your branch head, then the branch has been completely merged.如果该提交与您的分支负责人相同,则该分支已完全合并。

Note that git branch -d does this sort of thing already because it will refuse to delete a branch that hasn't already been completely merged.需要注意的是git branch -d做这样的事情已经因为它会拒绝删除已不是完全合并的一个分支。

There is a graphical interface solution as well.还有一个图形界面解决方案。 Just type只需输入

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 的图形表示,很容易意识到分支是否已经合并

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=$1
  merge_source_branch=$2

  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
}

In order to verify which branches are merged into master you should use these commands:为了验证哪些分支合并到 master 中,您应该使用以下命令:

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

Flags Are:标志是:

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

for example: git branch -r --merged master will show you all remote repositories merged into master.例如: git branch -r --merged master将显示所有合并到 master 的远程存储库。

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> .运行命令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.如果第 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 的更多信息。

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 即可查看分支的提交历史记录

I use git for-each-ref to get a list of branches that are either merged or not merged into a given remote branch (eg origin/integration )我使用git for-each-ref来获取合并或未合并到给定远程分支的分支列表(例如origin/integration

Iterate over all refs that match <pattern> and show them according to the given <format>, after sorting them according to the given set of <key>.在根据给定的 <key> 集对它们进行排序后,遍历所有与 <pattern> 匹配的引用并根据给定的 <format> 显示它们。

Note: replace origin/integration with integration if you tend to use git pull as opposed to git fetch .注意:如果您倾向于使用git pull而不是git fetchintegration替换origin/integration

List of local branches merged into the remote origin/integration branch合并到远程origin/integration分支的本地分支列表

git for-each-ref --merged=origin/integration --format="%(refname:short)" refs/heads/
#                ^                           ^                           ^
#                A                           B                           C
branch1
branch2
branch3
branch4

A: Take only the branches merged into the remote origin/integration branch A:只取合并到远程origin/integration分支的分支
B: Print the branch name B:打印分支名称
C: Only look at heads refs (ie branches) C:只看heads refs(即分支)

List of local branches NOT merged into the remote origin/integration branch未合并到远程origin/integration分支的本地分支列表

git for-each-ref --no-merged=origin/integration --format="%(committerdate:short) %(refname:short)" --sort=committerdate refs/heads
#                ^                              ^                                                  ^                    ^
#                A                              B                                                  C                    D
2020-01-14 branch10
2020-01-16 branch11
2020-01-17 branch12
2020-01-30 branch13

A: Take only the branches NOT merged into the remote origin/integration branch A:只取未合并到远程origin/integration分支的分支
B: Print the branch name along with the last commit date B:打印分支名称和最后提交日期
C: Sort output by commit date C:按提交日期对输出进行排序
D: Only look at heads refs (ie branches) D:只看heads refs(即分支)

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.使用诸如 gitk 或 TortoiseGit 之类的可视化工具,或者简单地使用 --all git log 来查看历史记录以查看所有合并到主分支的情况。 You should be able to spot if this particular feature branch has been merged or not.您应该能够发现此特定功能分支是否已合并。

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=$1
ORIGIN=$2
BRANCH=$3

# 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: $2/$3.\e[0m" || exit "$?"
        git push $2 :$3; 
        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="$1"
}

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 $1 && echo Incorporates $1 || echo Out of date from $1 && :

Then I can call:然后我可以打电话:

$ git current origin/master

to check if I am current.检查我是否当前。

要检查源分支是否已合并到主分支,可以使用以下 bash 命令:

git merge-base --is-ancestor <source branch name> master && echo "merged" || echo "not merged"

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

相关问题 我怎么知道 git 中的分支是否已经重新定位到 master? - How can I know in git if a branch has been already rebased onto master? 已经与master合并的分支中的错误如何解决? - How to fix a bug in a branch after it has already been merged with master? Git:如何删除已经提交,推送并合并到master的分支? - Git: How do I remove a branch that has already been committed, pushed, and merged into master? 我怎么知道分支是否已经在SVN / Mercurial / Git中合并? - How do I know if a branch has already been merged in SVN/Mercurial/Git? 如何检查master是否已合并到当前分支 - How do I check if master has been merged into current branch 知道第三个分支何时合并到 master - Know when a third branch has been merged to master 如何仅显示在已经合并回到父级中的分支上所做的提交? - How can I show only the commits made on a branch that has already been merged back into the parent? 我们可以在已经与 master 合并的分支上提交吗? - Can we commit on branch that's already been merged with master? 我可以安全地删除已合并到master的Git主题分支吗? - Can I safely remove my Git topic branch that has been merged into master? 如何重做已经合并到另一个分支中的提交 - How to rework a commit that has already been merged in an another branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM