git-branch-status icon indicating copy to clipboard operation
git-branch-status copied to clipboard

Compare not only two branches, but all of them

Open danon opened this issue 5 years ago • 5 comments

There is an option to compare two branches (local or remote):

git-branch-status [ base-branch-name compare-branch-name ]

And there is also an option to compare each local branch with its upstream:

  $ git-branch-status --all
    | master         | (even)     | (ahead 1) | origin/master             |
    | tracked-branch | (even)     | (even)    | origin/tracked-branch     |
    | (no local)     | n/a        | n/a       | origin/untracked-branch   |
    | local-branch   | n/a        | n/a       | (no upstream)             |
    | master         | (behind 1) | (ahead 1) | a-remote/master           |
    | (no local)     | n/a        | n/a       | a-remote/untracked-branch |

My problem: I have to manage a lot of branches locally and remotely before they're merged to master (often 20-30). What I would really love, is to run one command and see how does each of my local branches compare to my local master (and if they don't differ, I'd like to remove them but I'll do that on my own), and also see which my local branches' remote upstreams differ from remote master.

Is this possible?

danon avatar Mar 23 '20 16:03 danon

this script does not have that feature - that job as you described could be accomplished with a bit of shell-foo though

e.g. you could get a readable report like so:

#!/bin/bash

compare_branch='master'
local_branches=$(git branch | tr -d '*' | grep -v $compare_branch)

for branch in $local_branches ; do git-branch-status $compare_branch $branch ; done

or, if you are not interested in the pretty output, something like this would suffice:

#!/bin/bash

compare_branch='master'
local_branches=$(git branch | tr -d '*' | grep -v $compare_branch)

for branch in $local_branches
do  echo "comparing: $compare_branch to: $branch"
    ahead=$(git rev-list $branch..$compare_branch --count)
    behind=$(git rev-list $compare_branch..$branch --count)
    (( $ahead + $behind )) && continue

    echo -e "\tbranch: $branch is identical to: $compare_branch\n\tDelete it? [y/N]"
    read -p " " -n 1 -r ; echo ; [[ $REPLY =~ ^[Yy]$ ]] && git branch -D $branch
done

you could make either of those scripts into an alias function, or put into separate file like: 'compare-all-local-to-master' somewhere in your $PATH, make it executable, then that could be the single command you want

bill-auger avatar Apr 01 '20 23:04 bill-auger

just today i was going over a project that i had not touched in a long time; and i realized that this feature would have been handy in that situation too - i may add it to the --local report

bill-auger avatar Apr 04 '20 01:04 bill-auger

i have implemented this feature on the development branch if you would like to try it out

it is the -c --cleanup option, (for lack of a better name - suggestions welcome)

bill-auger avatar May 29 '20 21:05 bill-auger

it is the -c --cleanup option, (for lack of a better name - suggestions welcome)

The builtin git branch has --merged and --no-merged options. It would probably be better to use the same terminology.

SystemParadox avatar Sep 14 '21 11:09 SystemParadox

'merged/un-merged" would be appropriate, if it were only comparing branches - i had originally conceived the mnemonic of -c as "compare mode" - but as the motivating use-case seems to be the pruning of identical branches, not merely listing them, i figured that --cleanup was more fitting

i suppose that my intuition was on target then, because someone else requested the same feature again today #14

bill-auger avatar Sep 16 '22 03:09 bill-auger