Search This Blog

Saturday 12 August 2017

How to tell if you need to push any branch in any of several Git repositories

I want to reformat my laptop. I have a number of repositories for work, each in a subfolder off of ~/work. Before I reformat, do I have any commits that I have forgotten to push?

Old Sarum
First, here's a Bash script to recurse over subfolders and run a command in each one:
# Adapted from https://stackoverflow.com/a/26495371/
dirs=($(find . -maxdepth 1 -type d \( ! -name . \)))
for dir in "${dirs[@]}"; do
    echo $(basename $dir)
done
Within each folder there's a couple of different things that might be helpful to run. This command is equivalent to Mercurial's hg outgoing:
# From https://stackoverflow.com/a/7625074/
git log --branches --not --remotes=origin
It gives you a summary of all the commits that haven't been pushed.

However, I find following, more complex, script more useful. It lists the branches that need pushing, and I tend to be thinking branch by branch rather in commits.
# Adapted from https://stackoverflow.com/a/7774433
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
while read local remote
do
    [ -z "$remote" ] && continue
    git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
    AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
    if (( AHEAD > 0 )); then
        echo "$local $AHEAD commits ahead of $remote"
    fi
done
Putting it altogether here is the full script. It gives output like this:
~/work/vimc$ branch-status.sh
db
- master 2 commits ahead of origin/master
- i26 1 commits ahead of origin/master
api
webapps
- master 1 commit ahead of origin/master
vault