Checking out a remote git branch that does not exist locally?
Asked Answered
L

5

13

Let's say there is a remote clone of the same git repository that my local repository is cloned from - and there is a branch on that remote clone that isn't present in my clone (not the default branch).

Is there a way to clone that remote branch into my local copy? I don't want to merge it with the current branch or anything like that. I just want to start a local copy of the remote branch. Is there a way to do this?

I would also like to know (once this is done) how to add the mentioned branch also to the default remote-copy that my local clone checks in with by default.

Leprous answered 17/11, 2014 at 23:39 Comment(0)
S
7

You're going to want to run the following:

# Fetch everything from the other remote
git fetch <remote name>
# Check out the remote version of the branch
git checkout <remote name>/<branch name>
# Create the new local branch
git checkout -b <branch name>

That gives you a local, workable copy of your branch. Then, to push it back to the original remote (assuming it is origin), we just run

git push origin <branch name>:<branch name>

This will push your new branch up to your original remote.

Slavocracy answered 18/11, 2014 at 1:10 Comment(2)
I'm not sure I follow. There is something I don't understand in your instructions. If you look at this pastebin pastebin.com/kujnaY9e maybe you can tell me what I am doing wrong, what I am misunderstanding in your suggestion.Leprous
Ah yes okay. Use git remote add <name> <url> first, then fetch using the remote name you provided. This way you can fetch later changes from that repository easily later as well. If you don't want to do this, git fetch <remote> <branch> and then git checkout FETCH_HEAD should do what you want, but you need to know what the name of the branch on the other remote is.Slavocracy
L
4

One command will do this work:

git checkout -b <local_branch_name> origin/<remote_branch_name>

Edit: If an error occurred: fatal: 'origin/<branch_name>' is not a commit and a branch '<branch_name>' cannot be created from it.

You may try:

git pull
git checkout -b <local_branch_name> origin/<remote_branch_name>
Lashondalashonde answered 23/7, 2020 at 4:58 Comment(0)
E
3

Just use next:

git fetch <remote repo name>
git checkout -b <local branch name> <remote repo name>/<remote branch name>

It was helpful in my case and this procedure is identical to automatically generated action through TortoiseGit on my Windows machine.

Electrocute answered 20/6, 2018 at 7:35 Comment(2)
Is git fetch always required?Resistant
@KonradViltersten yesElectrocute
Q
0

As of Git version 2.37.2 you can now use $ git switch -c as shown in the steps below.

Fetch everything from the remote repository.

$ git fetch <remote>

Checkout the remote branch

$ git checkout <remote>/<branch>

Create the new local branch

$ git switch -c <branch>

Quern answered 30/8, 2022 at 6:8 Comment(0)
S
0

You can also set a default remote for all checkout operations like this:

git config --global checkout.defaultRemote origin

*use --global to update everywhere

Which adds the following section to your profile / repository's .git config file:

[checkout]
     defaultRemote = origin

Now you should just be able to run checkout or switch like this:

git checkout <something>

See Also: How to set default remote in git?

Singh answered 29/7 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.