If
$REMOTE
already set up$BRANCH
exists on the remote repo that I haven't fetched, yetcan 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
If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.
git structure will then include all the branches done on that repository. To use a specific branch do git checkout [branch_name] If the branch exists the files will be made available locally (as just that, the current files in the project directories).
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' -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With