找到未合并的 Git 分支?

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

Find unmerged Git branches?

gitgit-mergebranching-and-merging

提问by fluca1978

I have a Git repository with many branches, some of them already merged and some not. Since the number of branches is quite large, how can I determine which branches have not yet been merged? I would like to avoid having to do an "octopus" merge and re-merging branches that have already been merged.

我有一个包含许多分支的 Git 存储库,其中一些已经合并,有些还没有。由于分支数量比较多,如何判断哪些分支还没有合并呢?我想避免必须进行“章鱼”合并和重新合并已经合并的分支。

回答by Amber

Try this:

尝试这个:

git branch --merged master

It does what it says on the tin (lists branches which have been merged into master). You can also pull up the inverse with:

它按照它在锡上所说的做(列出已合并到 中的分支master)。您还可以使用以下方法拉起逆序:

git branch --no-merged master

If you don't specify master, e.g...

如果您不指定master,例如..

git branch --merged

then it will show you branches which have been merged into the current HEAD(so if you're on master, it's equivalent to the first command; if you're on foo, it's equivalent to git branch --merged foo).

然后它会向您显示已合并到当前的分支HEAD(因此,如果您在 上master,则相当于第一个命令;如果您在 上foo,则相当于git branch --merged foo)。

You can also compare upstream branches by specifying the -rflag and a ref to check against, which can be local or remote:

您还可以通过指定-r标志和要检查的参考来比较上游分支,可以是本地或远程:

git branch -r --no-merged origin/master

回答by NemoXP

You can also use the -r parameter to show remote branches that were not merged into master:

您还可以使用 -r 参数来显示未合并到 master 中的远程分支:

git branch -r --merged master

git branch -r --no-merged

回答by Adam Dymitruk

If a branch is merged already, merging it again won't do anything.So you don't have to be worried about "re-merging" branches that are already merged.

如果一个分支已经合并,再次合并它不会做任何事情。所以你不必担心“重新合并”已经合并的分支。

To answer your question, you can simply issue

要回答您的问题,您只需发出

 git branch --merged

to see the merged branches or

查看合并的分支或

 git branch --no-merged

to see the unmerged branches. Your current branch is implied but you can specify other branches if you wish.

查看未合并的分支。您当前的分支是隐含的,但您可以根据需要指定其他分支。

 git branch --no-merged integration

will show you branches that are not yet merged into integrationbranch.

将显示尚未合并到integration分支中的分支。

回答by moldcraft

The below script will find all origin/*branches that are ahead of current branch

下面的脚本将查找origin/*当前分支之前的所有分支

#!/bin/bash

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

echo -e "Current branch: \e[94m$CURRENT_BRANCH\e[0m"
echo ''

git branch -a | grep remotes/origin/ | while read LINE
do
    CMD="git diff --shortstat remotes/origin/${CURRENT_BRANCH}...${LINE}"

    if $CMD | grep ' file' > /dev/null; then
        echo -e "\e[93m$LINE\e[0m" | sed 's/remotes\/origin\///'
        $CMD
        echo ''
    fi
done

The up-to-date version of the script

脚本的最新版本

回答by user3915489

Below script will fetch you unmerged branches and write results in .xls file 
#!/usr/bin/env bash
echo "getting list of unmerged_branches from the remote"
file_name=unmerged_branches.xls`enter code here`
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
for branch in `git branch -r --no-merged | grep -v HEAD`;
do echo -e `git show --format="%cd  \t%cr  \t%ae" $branch | head -n 1` \t$branch; 
done | sort -r >> $current_time.$file_name
echo "result is writtein in ";
echo $current_time.$file_name;