Selecting a remote branch with the same name

I want to get the branch from the remote, but the name of my branch does not start with the remote name, for example:

git checkout -b BRANCH origin/BRANCH 

It works in some cases, but in some cases I get this error:

 fatal: git checkout: updating paths is incompatible with switching branches. 

while I'm sure the remote has this branch, and this works:

 git checkout -b origin/BRANCH 

After that I need to switch to another branch and rename the origin / BRANCH branch to BRANCH, and then switch back to BRANCH ... I want to know what this error means.

+4
source share
2 answers

This is why you get an error message.

git checkout can do one of two things. If you simply specified a branch and did not specify any paths, it will switch your current branch to the specified branch.

 git checkout mybranch # switch to branch 'my branch' 

If you specify several paths, then git will check these paths either from the index, or, if you specify a branch, from this branch.

 git checkout myfile # checkout 'myfile' from index 

As you can see, there is a potential ambiguity. What happens if you have a branch named myfile or a file named mybranch ?

The way git removes this ambiguity is that it checks the parameter to see if it matches the branch, and if not, it is assumed that the parameter refers to the file. If you have a branch and a file with the same name, you can force git to treat the parameter as a file with this syntax.

 git checkout -- myfile # treat 'myfile' as a file 

The -b option, which creates a new branch, is only valid when using the checkout branch switch form, and not when you check the specified files from the index.

 git checkout -b newbranch myfile # Illegal. I can't use `-b` when # I'm checking out files. 

If you try git checkout -b newbranch origin/BRANCH and you get this error, it means that origin/BRANCH does not match the name of any branch you have, so git suggested that you should reference the file.

To show what remote branch links you can do, git branch -r . If you don't have a link to a branch that you think should exist, you might need to do git fetch to extract it from the remote.

If you supply -b but don’t have any branch name to establish a new branch, git uses HEAD by default, that is, the commit on which your current branch is enabled.

 git checkout -b origin/BRANCH 

This creates a new local branch named origin/BRANCH based on your current commit. This, at best, can cause you some confusion and does not look like what you want at all.

+10
source

git checkout switches to any branch, it does not extract it from the remote repository. git checkout -b also creates a branch. To get a branch from a remote repository, use git fetch .

+2
source

All Articles