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
I got the same problem because I used git clone --depth=1
, which implies --single-branch
.
Do a completed git clone
will fix it.
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.
I got this when I did the following:
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.
I had made a silly mistake of not providing -m flag while committing (lol happens)
git commit -m "commit message in here"
First, checkout parent branch.Then type
git fetch --all --prune
git checkout <your branch>
Hope it helps!.
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