121

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?

Flip
  • 6,233
  • 7
  • 46
  • 75
tempuser
  • 1,517
  • 3
  • 11
  • 15

8 Answers8

186

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.

xploreraj
  • 3,792
  • 12
  • 33
  • 51
  • I think the commands I added will help you. Or did I misunderstand anything from your statement? I normally fetch the remote, create a branch out of it, work on it, push changes to my origin remote, raise PR to main repo and once it gets merged, I delete the feature branch. This helps me segregate many tasks and keep the tree clean. – xploreraj Apr 19 '18 at 10:48
  • 3
    `git checkout -b /` was the command I was trying to find. Nothing more. I removed the original statement, since it seems to have been confusing. – Abandoned Cart Apr 19 '18 at 12:41
  • 10
    You can use `-t` to create the local branch with same name to save a bit of typing - `git checkout -t /` – HankCa Jan 15 '20 at 00:13
  • 3
    On step 3 use `--no-track` if you don't want your new branch to track the remote one. – derpedy-doo Feb 07 '20 at 22:48
  • 1
    @xploreraj Your answer is what I was looking for, thanks alot. – PhillipMwaniki Jun 10 '21 at 12:57
  • 1
    @xploreraj, Good Answer. Never loosing track, where branch came from. Thanks. – gs1208 Feb 21 '22 at 05:05
  • Why I got this error `fatal: 'orgin/alex/ITCAML-1315' is not a commit and a branch 'ITCAML-1315' cannot be created from it` – Jing He Apr 02 '22 at 13:10
72

This should work:

git checkout --track origin/<REMOTE_BRANCH_NAME>

Tim
  • 5,435
  • 7
  • 42
  • 62
ali jafargholi
  • 899
  • 8
  • 6
26

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
Vladimir Dimitrov
  • 1,008
  • 7
  • 21
  • 12
    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`. – jpaugh Oct 13 '20 at 14:54
20

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.

OM Bharatiya
  • 1,840
  • 14
  • 23
15

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.

yksolanki9
  • 429
  • 2
  • 7
  • 14
  • 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. – tzee Feb 07 '23 at 14:51
6

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.

Kipr
  • 806
  • 10
  • 13
  • Finally nice syntax to what is done frequently without million commands and million parameters. Someone should give a prize to git UI developer, finally. – midenok Aug 11 '23 at 14:47
5

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
parastoo
  • 2,211
  • 2
  • 21
  • 38
  • 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. – wintermute Nov 03 '22 at 20:01
-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
ionutioio
  • 218
  • 1
  • 6
  • From git documentation: "git pull --rebase" is a shorthand for "git fetch" followed by "git rebase". See https://git-scm.com/docs/git-pull – J.P. Tosoni Oct 23 '18 at 07:50