Git fetch all remote branches

git branch -r | awk -F'/' '{print "git fetch "$1,$2}' | xargs -I {} sh -c {}

By: cakyus
2014-10-25 07:36:50

These Might Interest You

  • If you rebase and delete a lot of remote branches via git, your list of branches will end up showing branches on origin that no longer exist. This command will clear them out. Show Sample Output

    git remote prune origin
    donnoman · 2010-02-01 18:45:51 0
  • attempts to delete all local branches. git will fail on any branches not fully merged into local master, so don't worry about losing work. git will return the names of any successfully deleted branches. Find those in the output with grep, then push null repositories to the corresponding names to your target remote. assumes: - your local and remote branches are identically named, and there's nothing extra in the remote branch that you still want - EDIT: you want to keep your local master branch

    git branch | cut -c3- | grep -v "^master$" | while read line; do git branch -d $line; done | grep 'Deleted branch' | awk '{print $3;}' | while read line; do git push <target_remote> :$line; done
    gocoogs · 2011-08-13 16:58:34 0
  • Add this line to your ~/.gitconfig for a git alias "git brd" (i.e., brd = (br)anch+(d)ate) which sorts branches by date. Allows you to pass in limited "git branch" options such as "-r" (remote) or "-a" (all). (Note: forum added "$" prefix to command; obviously in gitconfig there is no "$" prefix.) Show Sample Output

    brd = "! f() { for k in $(git branch $@ | sed 's/^..//; s/ .*//'); do echo "$(git log -1 --pretty='%Cgreen%ci %Cblue(%cr)%Creset ' $k) $k" ; done | sort -r; }; f"
    michael_n_1138 · 2012-09-28 10:20:51 0
  • Simpler and without all of the coloring gimmicks. This just returns a list of branches with the most recent first. This should be useful for cleaning your remotes. Show Sample Output

    for k in $(git branch | sed /\*/d); do echo "$(git log -1 --pretty=format:"%ct" $k) $k"; done | sort -r | awk '{print $2}'
    dahuie · 2012-04-07 11:19:00 0

What Others Think

Hmm... You need to filter out the HEAD line (it's just a pointer, not a branch): "remotes/origin/HEAD -> origin/master" git branch -r | grep -v -- "->" | ... This will also fail if there are / characters in the branch names (for example people using gitflow: feature/widget). Awk now becomes a bit ugly: ... | awk '{ i=index($0,"/"); printf "git fetch %s %s\n",substr($0,0,i-1),substr($0,i+1); }' | ... I might also be tempted to change your use of xargs. Consider: echo origin master | xargs -n2 git fetch This changes the original command into: git branch -r | awk -F'/' '{print $1,$2}' | xargs -n2 git fetch or git branch -r | grep -v -- "->" | awk '{ i=index($0,"/"); printf substr($0,0,i-1),substr($0,i+1); }' | xargs -n2 git fetch
flatcap · 187 weeks and 1 day ago
Hi, i'm surprise how quickly i got response here. Yes, flatcap .. my command will fail if there are / characters in the branch names. Your's looks good to me.
cakyus · 187 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: