Pregunta

I sometimes use the checkout -b option to create a new branch, check it out at the same time and set up tracking in one command.

In a new environment, I get this error:

$ git checkout -b test --track origin/master
fatal: Cannot update paths and switch to branch 'test' at the same time.
Did you intend to checkout 'origin/master' which can not be resolved as commit?

Why does Git not like it? This used to work with the same repo.

¿Fue útil?

Solución

'origin/master' which can not be resolved as commit

Strange: you need to check your remotes:

git remote -v

And make sure origin is fetched:

git fetch origin

Then:

git branch -avv

(to see if you do have fetched an origin/master branch)

Finally, use git switch instead of the confusing git checkout, with Git 2.23+ (August 2019).

git switch -c test --track origin/master

Otros consejos

If you have a typo in your branchname you'll get this same error.

You can get this error in the context of, e.g. a Travis build that, by default, checks code out with git clone --depth=50 --branch=master. To the best of my knowledge, you can control --depth via .travis.yml but not the --branch. Since that results in only a single branch being tracked by the remote, you need to independently update the remote to track the desired remote's refs.

Before:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master

The fix:

$ git remote set-branches --add origin branch-1
$ git remote set-branches --add origin branch-2
$ git fetch

After:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/branch-1
remotes/origin/branch-2
remotes/origin/master

This simple thing worked for me!

If it says it can't do 2 things at same time, separate them.

git branch branch_name origin/branch_name 

git checkout branch_name

You could follow these steps when you stumble upon this issue:

  1. Run the following command to list the branches known for your local repository.

git remote show origin

which outputs this:

 remote origin
  Fetch URL: <your_git_path>
  Push  URL: <your_git_path>
  HEAD branch: development
  Remote branches:
    development                             tracked
    Feature2                                tracked
    master                                  tracked
    refs/remotes/origin/Feature1         stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    Feature2     merges with remote Feature2
    development  merges with remote development
    master       merges with remote master
  Local refs configured for 'git push':
    Feature2     pushes to Feature2     (up to date)
    development  pushes to development (up to date)
    master       pushes to master      (local out of date)
  1. After verifying the details like (fetch URL, etc), run this command to fetch any new branch(i.e. which you may want to checkout in your local repo) that exist in the remote but not in your local.
» git remote update

Fetching origin
From gitlab.domain.local:ProjectGroupName/ProjectName
 * [new branch]      Feature3    -> Feature3

As you can see the new branch has been fetched from remote.
3. Finally, checkout the branch with this command

» git checkout -b Feature3 origin/Feature3

Branch Feature3 set up to track remote branch Feature3 from origin.
Switched to a new branch 'Feature3'

It is not necessary to explicitly tell Git to track(using --track) the branch with remote.

The above command will set the local branch to track the remote branch from origin.

If you got white space in your branch then you will get this error.

In my case, I accidentally had a space in my branch name:

git checkout -b my-branch-name-with a-space
                                   ▲
        accidental space ──────────┘

Deal with the space.

It causes by that your local branch doesn't track remote branch. As ssasi said,you need use these commands:

git remote update
git fetch
git checkout -b branch_nameA origin/branch_nameB

I solved my problem just now....

In my case, I had to update my local git repository with latest tags from remote repository using below command:

git fetch --tags

The command to fetch remote repository tags may differ based on your organization's Git setup.

After doing this, git checkout worked.

First You need check your remote : It should be *master

git pull (up-to-date)
git checkout -b branch-name (branch name without any spaces)
git status
git add .
git commit -m "comments goes here"
git push branch-name

For me I needed to add the remote:

git remote -add myRemoteName('origin' in your case) remoteGitURL

then I could fetch

git fetch myRemoteName

First you need to Fetch the remote (the specific branch), then you can create a local br and track it with that remote branch using your command (i.e. checkout with -b and --track).

You should go the submodule dir and run git status.

You may see a lot of files were deleted. You may run

  1. git reset .

  2. git checkout .

  3. git fetch -p

  4. git rm --cached submodules //submoudles is your name

  5. git submoudle add ....

You can use these commands: git remote update, git fetch, git checkout -b branch_nameA origin:branch_nameB

I think maybe it's because of your local branch can not track remote branch

I got the same issue when I tap this line to create a new branch from my remote branch:

git checkout -b newbranch origin/ remotebranch

To fix that you can first update your local repository by:

fetch --all

Then correctly enter your command again :

git checkout -b newbranch origin/remotebranch

Please make sure to spell everything correctly and dont put space between origin/ and remotebranch. Those steps helped me to solve my issue.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top