Fetching and checking the remote git branch with just one command

If

  • I have a local repo with $REMOTE already configured
  • and a new $BRANCH branch exists on the remote repo, which I did not select, but

Is it possible to get this branch and check it in the local tracking branch with the same name in one command ?

I can achieve the desired result in two teams with

 git fetch $REMOTE $BRANCH git checkout $BRANCH # or more explicitly git checkout -b $BRANCH $REMOTE/$BRANCH 

or (inspired by this answer to the question How to check the remote Git branch? ) with

 git fetch $REMOTE $BRANCH:$BRANCH git branch --set-upstream-to=$BRANCH $BRANCH 
+5
source share
1 answer

There is no built-in command, but you can define an alias in ~/.gitconfig :

 [alias] fetch-checkout = !sh -c 'git fetch $1 $2 && git checkout $2' - 
+7
source

All Articles