Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-branch

I want to create a branch from an existing remote branch (let's say remote-A) and then commit the changes to the repository.

I have used the below commands to create a local branch from the existing remote-A

$git checkout remote-A  git branch master * remote-A 

Now I have created local-B from Remote A using the below commands

git branch local-B git checkout local-B 

How do I make sure the changes I have on local-B are on top of remote-A so that when I push local-B to the remote repo, the changes are on top of remote-A?

like image 646
tempuser Avatar asked Jun 19 '14 08:06

tempuser


People also ask

How do I create a local branch that tracks a remote branch?

To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.


1 Answers

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.

like image 63
xploreraj Avatar answered Sep 20 '22 15:09

xploreraj