170

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?

8 Answers 8

247

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.

Sign up to request clarification or add additional context in comments.

7 Comments

git checkout -b <new_branch_name> <remote_name>/<remote_branch_name> was the command I was trying to find. Nothing more. I removed the original statement, since it seems to have been confusing.
You can use -t to create the local branch with same name to save a bit of typing - git checkout -t <remote_server>/<remote_branch>
On step 3 use --no-track if you don't want your new branch to track the remote one.
@xploreraj Your answer is what I was looking for, thanks alot.
@xploreraj, Good Answer. Never loosing track, where branch came from. Thanks.
|
109

This should work:

git checkout --track origin/<REMOTE_BRANCH_NAME>

4 Comments

If you want to give a specific name to the new branch: git checkout -b master-copy --track origin/master, replace master-copy with whatever you wish.
Or git checkout -t origin/<REMOTE_BRANCH_NAME>
this short one line of command is really work... ! and yet easy to understand.
I found this article useful for understanding this answer (also added to Wayback Machine)
32

you want to create branch on base of remote-A, make changes on it and then push them on remote-A?

git checkout -b remote-A git pull origin remote-A git checkout -b remote-B 

make changes on remote-B

 git commit -a -m 'describe changes on remote-B branch' git checkout remote-A git merge remote-B git push origin remote-A 

1 Comment

Nowadays, git checkout feature/A will set up a new branch tracking remote origin/feature/A, unless feature/A already exists. You can also do this explicitly with git checkout -b feature/A --track origin/feature/A.
22

I wanted to create a new local tracking branch from a remote git branch with a different name.

So I used this command:

git checkout -b <new_branch_name> --track <remote_name>/<remote_branch_name>

Example:

git checkout -b local-A --track origin/remote-A

I saw it in multiple comments to the above answers, but it's good to have it in the first sight.

Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git pull, Git automatically knows which server to fetch from and which branch to merge in.

1 Comment

It is efficient to do both checkout and track in a single command - this worked for me too.
20

First we need to fetch the remote branch using

git fetch origin <remote-branch> 

Then just create a new local branch to track the remote branch

git checkout -b <local-branch> origin/<remote-branch> 

Replace origin with your remote name.

1 Comment

Important to note this is only applicable after you have created a branch manually on your remote repo first either through git command or GUI.
15

Since the introduction of git switch in version 2.23:

git switch -c <new-branch> <start-point> 

Where <start-point> is your remote branch, for example origin/main.

In case you want to simply create a local branch from a remote one, for example from origin/remote-branch you can simply run:

git switch remote-branch 

It will create a new local branch from the remote one.

3 Comments

Finally nice syntax to what is done frequently without million commands and million parameters. Someone should give a prize to git UI developer, finally.
What's the difference between having the -c and not? I'm trying to run these commands in a script and I want it to create a local branch if it doesn't exist but if it does, then I just want it to switch to it.
@tzg You migh want to look at -C then, the capital C that is.
9

First download all your remote branches by :

git fetch 

then create a local branch from it:

git checkout -b local_branch_name origin/remote_branch_name 

1 Comment

I have a grievance with git that I have to look up and do these steps. The tool should be able to understand what I'm trying to achieve.
-5

To make sure your changes are on top, you must not pull from remote. you must fetch and rebase. il will be something like this:

fetch->stash->rebase->stash pop->commit->push 

1 Comment

From git documentation: "git pull --rebase" is a shorthand for "git fetch" followed by "git rebase". See git-scm.com/docs/git-pull

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.