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

后端 未结 30 3054
太阳男子
太阳男子 2020-11-27 23:54

I\'m not sure why I\'m unable to checkout a branch that I had worked on earlier. See the commands below (note: co is an alias for checkou

相关标签:
30条回答
  • 2020-11-28 00:52

    I got the same problem because I used git clone --depth=1, which implies --single-branch.

    Do a completed git clone will fix it.

    0 讨论(0)
  • 2020-11-28 00:52

    In my case I had renamed a file changing the case of the file, i.e. SomeFile.js -> someFile.js

    I think that was related to the problem. Doing a git fetch didn't fix the issue.

    I moved the files out of my project, did a fetch, and did a push without them. Then I did a fetch, added them back, and did a push, and it worked. I don't know if all those steps were needed, but it did ultimately work.

    0 讨论(0)
  • 2020-11-28 00:53

    I got this when I did the following:

    • Used IntelliJ IDE, connected to git
    • Created a new file, and added to git
    • Renamed the new file

    When I tried to check in the directory, I got this error.

    To fix:

    I opened the repo in git extensions. I saw that the file (with the old name) was staged. But since it didnt exist anymore, it could not be committed.

    I simply unstaged this file.

    Then I re-added the file (this time correctly named) into git and committed without errors.

    0 讨论(0)
  • 2020-11-28 00:53

    I had made a silly mistake of not providing -m flag while committing (lol happens)

    git commit -m "commit message in here"
    
    0 讨论(0)
  • 2020-11-28 00:54

    First, checkout parent branch.Then type

    git fetch --all --prune 
    git checkout <your branch>
    

    Hope it helps!.

    0 讨论(0)
  • 2020-11-28 00:54

    I had a different root cause

    I had a script that basically searches all branches matching jira issue key in the for "PRJ-1234" among all branches to execute a git branch checkout command on the matching branch

    The problem in my case was 2 or more branches shared the same jira key and hence caused my script to fail with the aforementioned error

    By deleting the old unused branch and making sure only a single branch had the jira key reference fixed the problem

    Here's my code in case someone wants to use it

    git remote update
    git fetch --all --prune 
    git branch -r --list *$1* | xargs git checkout --force
    

    save this as switchbranch.sh

    Then use it from the terminal ./switchbranch.sh PRJ-1234

    0 讨论(0)
提交回复
热议问题