2

I'd like a command similar to git branch that will list all local branches that are not merged with their upstream.

This is something git branch knows about, as git branch -d <branch-name> will fail for branches that are not merged.

Benjohn
  • 13,228
  • 9
  • 65
  • 127

3 Answers3

2

Are you looking for this:

git branch --no-merged

Above command will list out all the branches which are not merged to your current branch. if you need to check for other branch then

git branch --no-merged master

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
Gourav
  • 570
  • 3
  • 16
  • 1
    [This question](http://stackoverflow.com/questions/7773939/show-git-ahead-and-behind-info-for-all-branches-including-remotes) might also be helpful. – sendaran Nov 26 '15 at 11:29
  • Hi, thank you. No, I don't think I am after this, sorry. I want to see the branches that are not merged with _their_ upstream. As in, those that have something to push to their remote. – Benjohn Nov 26 '15 at 16:55
1

As far as I know, there is no single built-in Git command to list all local branches that are behind their upstream branch (if any). However, you can implement the desired functionality using existing Git commands. See below.

Script

#!/bin/sh

# git-bbu.sh
#
# List the local Branches Behind their Upstream branch, if any.
#
# Usage: git bbu
#
# To make a Git alias called 'bbu' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.bbu \
#       '!sh git-bbu.sh'

if [ $# -ne 0 ]; then
    printf "%s\n\n" "usage: git bbu"
    exit 1
fi

git for-each-ref --format='%(refname:short)' refs/heads | \
    while read ref; do
      if (git rev-parse --verify --quiet "$ref"@{upstream} &&
          ! git diff --quiet "$ref"..."$ref"@{upstream}) \
              >/dev/null 2>&1; then
        printf "%s\n" "$ref"
      fi
    done

Explanation

Run

git for-each-ref --format='%(refname:short)' refs/heads

to list all local branches (Why not just use git branch, here? Because, in a script, you should try to use plumbing Git commands instead of porcelain ones). Pipe it to a while loop, and, for each such local branch,

  • Check whether the local branch has an upstream branch, using

    git rev-parse --verify --quiet "$ref"@{upstream}
    
  • Check whether the local branch is behind its upstream branch, using (the logical negation of)

    git diff --quiet "$ref"..."$ref"@{upstream}
    

If both conditions are verified, print the name of the branch.

References

Community
  • 1
  • 1
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • Thanks – I'll give this a try. – Benjohn Nov 26 '15 at 23:13
  • Yes, thank you – it works! Now, I'm wondering if I can get something similar that shows branches that are ahead / behind, and gives the number of commits on each side that need reconciling :-) – Benjohn Dec 03 '15 at 08:49
  • @Benjohn It could be done, but you should probably ask a new question. – jub0bs Dec 03 '15 at 16:00
0

git for-each-ref has an "upstream" field, which can show if the branch is up-to-date with its upstream. You can then filter its output to show only those which are ahead of their upstreams:

$ git for-each-ref --format='%(upstream:trackshort)..%(refname:short)' | awk -F '\\.\\.' '$1~/>/{print $2}'

this skips branches which do not have upstreams, modify the filter if it's not what you need (the $1 is empty then in the awk record). ".." is used a a separator because you cannot have the sequence in reference name

max630
  • 8,762
  • 3
  • 30
  • 55