问题
If
- I have local repo with a remote
$REMOTE
already set up - and a new branch
$BRANCH
exists on the remote repo that I haven't fetched, yet
can I fetch that branch and check it out into a tracking local branch of the same name in a single command?
I can achieve the desired result in two commands either with
git fetch $REMOTE $BRANCH
git checkout $BRANCH # or more explicitly git checkout -b $BRANCH $REMOTE/$BRANCH
or (inspired by this answer to Question How do I check out a remote Git branch?) with
git fetch $REMOTE $BRANCH:$BRANCH
git branch --set-upstream-to=$BRANCH $BRANCH
回答1:
There is no builtin command, but you could define an alias in your ~/.gitconfig
:
[alias]
fetch-checkout = !sh -c 'git fetch $1 $2 && git checkout $2' -
来源:https://stackoverflow.com/questions/30392969/fetch-and-checkout-a-remote-git-branch-in-just-one-command