tempuser
tempuser

Reputation: 1937

How to create a local branch from an existing remote 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?

Upvotes: 161

Views: 373867

Answers (8)

ali jafargholi
ali jafargholi

Reputation: 1229

This should work:

git checkout --track origin/<REMOTE_BRANCH_NAME>

Upvotes: 105

Kipr
Kipr

Reputation: 1076

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.

Upvotes: 14

parastoo
parastoo

Reputation: 2469

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

Upvotes: 9

yksolanki9
yksolanki9

Reputation: 529

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.

Upvotes: 19

OM Bharatiya
OM Bharatiya

Reputation: 2150

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.

Upvotes: 22

xploreraj
xploreraj

Reputation: 4362

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.

Upvotes: 238

Vladimir Dimitrov
Vladimir Dimitrov

Reputation: 1088

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

Upvotes: 32

ionutioio
ionutioio

Reputation: 238

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

Upvotes: -5

Related Questions