How to create a local branch from an existing remote branch?

Old post, still I'd like to add what I do.

1. git remote add <remote_name> <repo_url>
2. git fetch <remote_name>
3. git checkout -b <new_branch_name> <remote_name>/<remote_branch_name>

This series of commands will

  1. create a new remote,
  2. fetch it into your local so your local git knows about its branches and all,
  3. create a new branch from the remote branch and checkout to that.

Now if you want to publish this new local branch to your remote and set the upstream url also

git push origin +<new_branch_name>

Also, if only taking in remote changes was your requirement and remote already exists in your local, you could have done, instead of step 2 and 3,

git pull --rebase <remote_name> <remote_branch_name>

and then opted for git mergetool (needs configurations separately) in case of any conflicts, and follow console instructions from git.


This should work:

git checkout --track origin/<REMOTE_BRANCH_NAE>

Tags:

Git

Git Branch