Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch and checkout a remote git branch in just one command

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
like image 933
das-g Avatar asked May 22 '15 09:05

das-g


People also ask

How do I fetch a particular remote 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.

How do I pull just one branch in github?

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).


1 Answers

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' -
like image 153
knittl Avatar answered Oct 22 '22 02:10

knittl