206

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
marekful
  • 14,986
  • 6
  • 37
  • 59
  • 7
    The error message is telling you that `origin/master` is not a commit ID, i.e., not a valid remote branch. Does it show up in `git branch -r` output? – torek Apr 10 '14 at 10:55
  • It didn't. Turns out I didn't check out all remote branches during clone. – marekful Apr 10 '14 at 20:08
  • possible duplicate of [Git checkout on a remote branch does not work](http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work) – JuJoDi Jul 23 '14 at 21:36

15 Answers15

218

'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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 13
    I had a typo in my command which triggered this error; I wasn't spelling my remote correctly! – qix Oct 03 '14 at 22:13
  • 2
    It was a new branch and my local repository didn't knew about it. I had to do a `pull` and then this command worked. – coding_idiot Nov 17 '14 at 12:28
  • 2
    This answer was helpful because it showed me that there’s really something weird going on: Remotes are set correctly, but the new remote branch is simply not fetched. When I clone the remote in a clean directory, it works. Could my `.git` directory be somehow corrupt? – Konrad Rudolph Nov 11 '15 at 13:45
  • git remote -v returned this error fatal: Not a valid object name: 'remote'. – Karim Samir Nov 20 '15 at 15:36
  • @KarimSamir you could ask a separate question (with your OS and git version), as `git remote -v` executed in a git repo does work. – VonC Nov 20 '15 at 15:58
  • did a `git pull --rebase` on master branch to fix this – Amit Dec 09 '16 at 23:27
  • 1
    Basically you should `git fetch target-remote` before `git checkout -b branch target-remote/branch`. – Ruslan Makrenko Apr 26 '18 at 11:47
  • @RuslanMakrenko Yes, I always like to do a fetch: it does not change your index or working tree, and it makes sure your remote is up-to-date. – VonC Apr 26 '18 at 12:22
97

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

Ludder
  • 2,665
  • 1
  • 25
  • 26
  • 5
    Actually it would be nice if someone could take the time to tell Git developers about this, the error message is not very informative ;) – Scorchio Jun 26 '15 at 11:49
  • 9
    yes , this was my problem , I had a space in my branch name – Karim Samir Nov 20 '15 at 15:39
  • Same here, I was trying `git checkout -b origin mybranch` instead of `git checkout -b mybranch` (extra `origin`) – Guillaume Renoult Jan 11 '16 at 23:58
  • I copy-pasted my branch name and had trailing whitespace after the branch name which caused this error. – learningKnight Apr 28 '17 at 00:49
  • I was trying to checkout `orgin/my-branch` instead of `origin/my-branch`. Missed one `i` & I kept scratching my head for a while to understand why an old ally is refusing to get along. Typo indeed was the issue. – DDM Oct 29 '18 at 08:09
  • oversight is real. thank you! – jmcg Feb 16 '22 at 07:11
62

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
Bob Aman
  • 32,839
  • 9
  • 71
  • 95
  • 5
    Solved it for me. I used a shallow --depth=1 clone and this is what I needed to switch branches. – Kevin Ghadyani Dec 23 '15 at 23:48
  • 1
    Gah yes, the depth thing definitely results in this error! If possible, I would suggest that the first few sentences be edited to emphasize the depth thing. – Dubslow Nov 23 '17 at 04:18
  • From git-config side, you can achieve a similar effect by adding a new `[branch]` entry and a `fetch` line under the relevant `[remote]` entry in `.git/config` – Janaka Bandara Jan 08 '22 at 02:55
26

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ashwini Reddy
  • 269
  • 3
  • 3
10

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.

Community
  • 1
  • 1
ssasi
  • 1,758
  • 1
  • 12
  • 18
9

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

dansek
  • 129
  • 1
  • 5
4

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

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....

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
2

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.

Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40
2

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sumathi J
  • 180
  • 1
  • 7
1

For me I needed to add the remote:

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

then I could fetch

git fetch myRemoteName
RayLoveless
  • 19,880
  • 21
  • 76
  • 94
0

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).

Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70
0

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 ....

knight2016
  • 539
  • 6
  • 12
0

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

kang
  • 1
0

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.

Confiance
  • 706
  • 5
  • 7