Reputation: 1361
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.
Upvotes: 13
Views: 26902
Reputation: 30287
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?
Upvotes: 0
Reputation: 19
As of Git version 2.37.2 you can now use $ git switch -c
as shown in the steps below.
$ git fetch <remote>
$ git checkout <remote>/<branch>
$ git switch -c <branch>
Upvotes: 0
Reputation: 41
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>
Upvotes: 4
Reputation: 1015
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.
Upvotes: 3
Reputation: 1059
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.
Upvotes: 7