Table of Contents
Here’s the command which pulls all of git branches which is tracking the remote correspondent.
Command
The following command works.
1 |
git branch -vv|sed -e 's/^s*(*|)s*//g'|awk '$3 ~ /^[/ && $4 != "gone]" {print "git checkout " $1 "; git pull"}'|sh |
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.
1 2 3 4 5 6 |
dev_rel/20160412 b84788a [origin/dev_rel/20160412] Merge branch 'feature/deploy' into dev_rel/20160412 feature/content 21db3cd [origin/feature/content] Merge branch 'master' into feature/content feature/history b153753 [origin/feature/history] feat(log): change column name feature/ss_upload 0e94961 Merge branch 'dev_rel/20160412' of xxx into feature/ss_upload feature/test b93826f [origin/feature/test: gone] feat(test): setup test component * master 2ce6356 [origin/master] close #24 |
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.
1 2 3 4 |
git checkout dev_rel/20160412; git pull git checkout feature/content; git pull git checkout feature/history; git pull git checkout master; git pull |
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
.
1 |
alias gpall="git branch -vv | sed -e 's/^s*(*|)s*//g' | awk '$3 ~ /^[/ && $4 != "gone]" {print "git checkout " $1 "; git pull"}' | sh" |
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.
1 |
git branch -vv|sed -e 's/^s*(*|)s*//g'|awk '$3 ~ /^[/ && $4 != "gone]" {print $1 " " substr($3, 2, length($3) - 2)}'|sed -e 's/s([^/]*)/(.*)$/ 1 2/g' |
The above script outputs as below.
1 2 3 4 |
dev_rel/20160412 origin dev_rel/20160412 feature/content origin feature/content feature/history origin feature/history master origin master |
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.