Home > Back-end >  How to pull a remote branch into a new local branch
How to pull a remote branch into a new local branch

Time:01-27

Is there a single git command, that allow to pull a remote branch creating a new local branch with a custom name on the fly?

That is, an equivalent to

git fetch --all
git checkout remote_name/branch_name
git switch -c new_branch_name
git branch -u remote_name/branch_name

CodePudding user response:

This should work:

git checkout remote_name/branch_name -b new_branch_name

More information can be found here: https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt--bltnew-branchgt

CodePudding user response:

Yes, you can pull a branch and create a new branch in one command, by doing: git pull origin <remoteBranchToPullFrom>:<NameForYourNewLocalBranch>.

If you use the same name for both, you will just pull changes into that branch. If you use a new name for the second argument it will create a new branch.

CodePudding user response:

With git checkout, it's (almost) a single command:

git fetch --all
git checkout -b your_local_name --track origin/remote_name

If you want to fuse fetching and checking out into a single command, consider defining an alias:

git config --global alias.fco \
  '!f() { git fetch --all; git checkout -b "$1" --track "origin/$2"; } f'

Then simply:

git fco your_local_name remote_name
  •  Tags:  
  • git
  • Related