Find unmerged Git branches?

巧了我就是萌 提交于 2019-11-26 23:25:49

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:

git branch --no-merged master

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

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).

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

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

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

git branch -r --merged master

git branch -r --no-merged

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 integration branch.

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

#!/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

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!