62

I am a new user of Git. I have forked a repository called Spoon-Knife (available for practicing forking with Git). Then, I cloned it locally by running

git clone https://github.com/rohinichoudhary/Spoon-Knife.git 

This repository contains three branches, i.e.

  • master,
  • test-branch,
  • change-the-title.

When I run git branch, it only shows *master, not the remaining two branches. And when I run

git checkout test-branch 

I get the following error:

error: pathspec 'test-branch' did not match any file(s) known to git.

Why is this happening? How can I solve this problem?

1
  • Make sure your branch name is written correctly of same repository. Commented Mar 10, 2017 at 12:57

14 Answers 14

69

The modern Git should able to detect remote branches and create a local one on checkout.

However if you did a shallow clone (e.g. with --depth 1), try the following commands to correct it:

git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' git fetch --all 

and try to checkout out the branch again.

Alternatively try to unshallow your clone, e.g. git fetch --unshallow and try again.

See also: How to fetch all remote branches?

Sign up to request clarification or add additional context in comments.

2 Comments

None of the other answers worked for me (with Git 2.7.4) but this one did, thanks! I think I must have cloned this repository with --depth 1, and thus ended up with a weird setting of remote.origin.fetch.
Yes this answer worked for me and was much straightforward compared to others. Thank you!
44

When I run git branch, it only shows *master, not the remaining two branches.

git branch doesn't list test_branch, because no such local branch exist in your local repo, yet. When cloning a repo, only one local branch (master, here) is created and checked out in the resulting clone, irrespective of the number of branches that exist in the remote repo that you cloned from. At this stage, test_branch only exist in your repo as a remote-tracking branch, not as a local branch.

And when I run

git checkout test-branch 

I get the following error [...]

You must be using an "old" version of Git. In more recent versions (from v1.7.0-rc0 onwards),

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat [git checkout <branch>] as equivalent to

$ git checkout -b <branch> --track <remote>/<branch> 

Simply run

git checkout -b test_branch --track origin/test_branch 

instead. Or update to a more recent version of Git.

7 Comments

I am using git version 1.9.1
@rohinichaudhary are you sure you are using 1.9.1? It doens't/shouldn't behave like that gist.github.com/AD7six/7ee86b44804319979dff
@rohinichaudhary Run git branch -r. If you see origin/test-branch listed and if you're really using v1.9.1, then git checkout test_branch should work as explained in my answer.
@Jubobs when i tried your solution, again a error was encountered. then i again tried with git checkout test-branch. it worked.
I get fatal: Cannot update paths and switch to branch 'V1' at the same time. Did you intend to checkout 'origin/V1' which can not be resolved as commit? from that.
|
27

My friend, you need to create those corresponding branches locally first, in order to check-out to those other two branches, using this line of code

git branch test-branch 

and

git branch change-the-title

then only you will be able to do git checkout to those branches

Also after creating each branch, take latest changes of those particular branches by using git pull origin branch_name as shown in below code

git branch test-branch git checkout test-branch git pull origin test-branch 

and for other branch named change-the-title run following code =>

git branch change-the-title git checkout change-the-title git pull origin change-the-title 

Happy programming :)

2 Comments

your answer can be another approach. But direct checkout to remote branch also works (as suggested in above answer), if you have clone full repository, not a particular branch.
@rohinichaudhary : absolutely correct. In older version of Git, it didn't worked for me. I were using an older version of Git, now going to update :)
17

You can also get this error with any version of git if the remote branch was created after your last clone/fetch and your local repo doesn't know about it yet. I solved it by doing a git fetch first which "tells" your local repo about all the remote branches.

git fetch git checkout test-branch 

1 Comment

Simple & easy explanation about git fetch. +1
10

just follow three steps, git branch problem will be solved.

git remote update git fetch git checkout --track origin/test-branch 

1 Comment

Works for me as well. I might add that if you have old changes preventing you to switch branch, you can discard them with "git reset --hard"
3

Solution:

To fix it you need to fetch first

$ git fetch origin $ git rebase origin/master 

Current branch master is up to date.

$ git checkout develop 

Branch develop set up to track remote branch develop from origin.

Switched to a new branch ‘develop’

Comments

0

This error can also appear if your git branch is not correct even though case sensitive wise. In my case I was getting this error as actual branch name was "CORE-something" but I was taking pull like "core-something".

Comments

0

I got this error because the instruction on the Web was

git checkout https://github.com/veripool/verilog-mode

which I did in a directory where (on my own initiative) i had run git init. The correct Web instruction (for newbies like me) should have been

git clone https://github.com/veripool/verilog-mode

Comments

0

Following worked for me

git pull 

Then checkout the required branch

Comments

0

git fetch && git checkout branch-name

1 Comment

Hi priyanka, please consider the elaboration for your answers as they do appears more helpful
0

Check first if any file is modified in you current branch using

git status

if any add or remove it then try

git checkout branchName

Comments

0

If it is a tag, you can do git fetch --all --tags and then git checkout <TAG_NAME>

Comments

0

As already mentioned, in many cases, this is because you're trying to checkout a remote branch that has been created more recently than you've fetched from remote.

You can eliminate this problem altogether with a helpful alias that will run the git checkout command and if it runs into any errors, fetch the latest changes from origin, and retry the checkout.

Just add the following alias to your gitconfig

[alias] co = "!f() { \ git checkout \"$@\" 2>/dev/null || { \ echo 'Branch not found, fetching remote branches...'; \ git fetch && \ git checkout \"$@\"; \ }; \ }; f" 

And run like this:

git co my-branch git co -b my-new-branch 

In the example above, if you don't have a reference to my-branch locally, it'll fetch and then try checking it out again

See Also: Git: cannot checkout branch - error: pathspec '...' did not match any file(s) known to git

Comments

-2

Try cloning before doing the checkout.

do git clone "where to find it" then after cloning check out the branch

1 Comment

The poster clearly showed the git clone command used as well as the command to attempt to checkout the branch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.