My problem is related to Fatal Git error when switching branch.
I try to fetch a remote branch with the command
git checkout -b local-name origin/rem
Not sure if this is helpful or exactly relevant to your question, but if you are trying to fetch and checkout only a single branch from the remote repository, then the following git commands will do the trick:
url= << URL TO REPOSITORY >>
branch= << BRANCH NAME >>
git init
git remote add origin $url
git fetch origin $branch:origin/$branch
git checkout -b $branch --track origin/$branch
For me I had a typo and my remote branch didn't exist
Use git branch -a
to list remote branches
none of the above worked for me. My situation is slightly different, my remote branch is not at origin. but in a different repository.
git remote add remoterepo GIT_URL.git
git fetch remoterepo
git checkout -b branchname remoterepo/branchname
tip: if you don't see the remote branch in the following output git branch -v -a
there is no way to check it out.
Confirmed working on 1.7.5.4
I suspect there is no remote branch named remote-name, but that you've inadvertently created a local branch named origin/remote-name.
Is it possible you at some point typed:
git branch origin/remote-name
Thus creating a local branch named origin/remote-name? Type this command:
git checkout origin/remote-name
You'll either see:
Switched to branch "origin/remote-name"
which means it's really a mis-named local branch, or
Note: moving to "origin/rework-isscoring" which isn't a local branch If you want to create a new branch from this checkout, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b
which means it really is a remote branch.
Alternate syntax,
git fetch origin remote_branch_name:local_branch_name
It's not very intuitive but this works well for me ...
mkdir remote.git & cd remote.git & git init
git remote add origin $REPO
git fetch origin $BRANCH:refs/remotes/origin/$BRANCH
THEN run the git branch --track command ...
git branch --track $BRANCH origin/$BRANCH