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.