Git: git pull for all branches, each of them is tracking the remote


Here’s the command which pulls all of git branches which is tracking the remote correspondent.

Command

The following command works.

It’s the combination of the commands. I think it’s easy with programming language like perl or ruby, but I tried with sed and awk. I’ll explain what the command does.

Get Local Branches with the Tracked Remote Branches

git branch -vv gets every local and tracked remote branch. The result is as below.

Current local branch starts with *. The tracked remote branches are surrounded by [, ]. And when the remote branch has been deleted, it shows “gone”. Convert these output to the command we want.

Put All Line to The Same Format

The current local branch starts with ‘*‘. It makes it difficult to analyze. So I put all line to the same format.

sed -e 's/^s*(*|)s*//g'.

Exclude Not Appropriate Line

From the above result, exclude no-target branch, whose remote branch is gone or which is not tracking. awk '$3 ~ /^[/ && $4 != "gone]" ... extract only target line.

If the branch is tracking the remote, $3 contains remote branch as the text starts with [. Then, continue if $3 starts with [. (If you create comment which stats with [, the command can’t exclude it.)

And if the remote branch is gone, $4 become gone]. Then, continue the procedure if $4 is not gone]. (If you create comment which includes gone], the command can’t exclude it.)

Generate git Command

Look at the latter of awk, ... '{print "git checkout " $1 "; git pull"}', it outputs git commands. (awk内でコマンドを実行する方法もあると思いますが、わかりませんでした。)

It executes git pull for all tracking branches.

The awk command outputs the text like the following.

Execute on Shell

Execute the above output with shell. Pass the result to shell by pipe.

Then, your shell execute the output and start pulling all local branches tracking the remote.

Create Alias

I know, it is hard to copy the above command all the time. So, create alias in .bashrc or .zshrc, etc.

If you use bash, put the following text to your .bashrc.

Then, gpall command pulls all the tracking branches.

Get Information of the Tracked Branches

If you want to get remote repository name and branch, the following code will help.

The above script outputs as below.

Is there more simple method, please tell me. @_kjot

If you use ruby, I heard that the gem git-up is helpful for pull all branches.