Tracking a single remote branch as a local branch is straightforward enough.
$ git checkout --track -b ${branch_name} origin/${branch_name}
<
Update Q1 2020: Mohsen Abasi proposes in the comments, based on the 2014 slm's answer, the simpler alternative:
for i in $(git branch -r | grep -vE "HEAD|master" | sed 's/^[ ]\+//');
And it uses $() instead of obsolete backticks.
As I mention in another old answer, using git for-each-ref is probably faster.
And I would use the new (Git 2.23+) git switch command, which replaces the confusing git checkout.
for i in $(git for-each-ref --format=%(refname:short) \
--no-merged=origin/HEAD refs/remotes/origin); do \
git switch --track $i; \
done
That way, no grep
needed.
Old (2011) original answer:
Here is my one-liner I use (in a bash shell, tested with msysgit1.7.4):
For copy-paste:
remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch --set-upstream-to $remote/$brname $brname; done
For more readability:
remote=origin ; // put here the name of the remote you want
for brname in `
git branch -r | grep $remote | grep -v master | grep -v HEAD
| awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do
git branch --set-upstream-to $remote/$brname $brname;
done
remote
variable (it can be 'origin
' or whatever name you have set for one of the remotes of your current Git repo).origin/a/Branch/Name => a/Branch/Name
through the awk
expression.it will set the upstream branch through --set-upstream-to (or -u), not --track
:
The advantage is that, if the branch already exists, it won't fail and it won't change that branch origin, it will only configure the branch.xxx.(remote|merge)
setting.
branch.aBranchName.remote=origin
branch.aBranchName.merge=refs/heads/a/Branch/Name
That command will create local branches for all remote upstream branches, and set their remote and merge setting to that remote branch.
Using bash, If you want to checkout all branches:
for remote in `git branch -r`; do git checkout $(echo $remote | cut -d'/' -f 2); done
It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them.
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do git branch --track ${branch##*/} $branch; done
Use this and you will not have such warning as: refname 'origin/dev' is ambiguous
To do the same as tjmcewan's answer but on Windows, call this from a batch file:
for /f "delims=" %%r in ('git branch -r ^| grep -v master') do git checkout --track %%r
Or this from the command line:
for /f "delims=" %r in ('git branch -r ^| grep -v master') do git checkout --track %r
for rembranch in `git remote update 2>&1 > /dev/null ; git branch -r|egrep -wv "HEAD|master"`
do
git checkout --track -b `echo $rembranch|awk -F\/ '{print $2}'` $rembranch;
done
Explanation:
line 1: 'git branch -r' (followed by 'git remote update' to update the info on changes to remote) lists all remote branches; 'egrep -vw' is used to knock entries having HEAD and master in the result.
line 3: Track the named remote branch while checking it out locally. A simple awk is used to avoid 'origin/' being the suffix for local branches.
You could script that easily enough, but I don't know when it'd be valuable. Those branches would pretty quickly fall behind, and you'd have to update them all the time.
The remote branches are automatically going to be kept up to date, so it's easiest just to create the local branch at the point where you actually want to work on it.