Checking out a remote git branch that doesn't exist locally?

Excuse me if the answer already exists - I searched as well as I could, but without any search phrase I can think of. If the answer to this question is already complete, it is not for lack of trying that I was unable to find it.

Anyway, that's my question. Let's say (and we're talking git here) there is a remote clone of the same repository from which my local repository is cloned - only there is a branch present on that remote clone that is not present in my clone. Is there a way to import this remote branch into my local copy? NOTE. I don't want to merge it with the current branch or anything like that. I just want to create a local copy of the same branch that has everything in the remote copy. Is there a way to do this?

Once again - the remote branch I'm talking about is not the default default that my local clone runs from, but a separate one (i.e. the one I should point to on the command line).

Thank.

By the way, I would also like to know (once this is done) how to get the specified branch added to the default remote copy which my local clone runs by default, but if necessary, I can ask a separate question for this, so please if you can only answer one of these two questions, answer the first question.

+3


source to share


1 answer


You need 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>

      

This gives you a local, workable copy of your branch. Then, to drop it back to the original remote (assuming it is origin

), we'll just run



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

      

This will nudge your new branch back to the original remote.

+4


source







All Articles