Boyang
Boyang

Reputation: 2566

Can't checkout to other branches because ignored file has local changes?

I get:

$ git checkout mybranch
error: Your local changes to the following files would be overwritten by checkout:
        .idea/workspace.xml
Please, commit your changes or stash them before you can switch branches.
Aborting

But .idea/ is already in my .gitignore. If I do git status, there is also no changes showing up.

Please advice.

UPDATE:

Thanks for the advice. I wish I could go to mybranch and check, but I couldn't, because it wouldn't allow me to switch! Provided that it is the correct behaviour to ignore .idea/ (they are project config files from IDE), what should I do now? My goal is to go to mybranch and merge it with master. You can think of mybranch as a production branch and master as dev branch. Now I want to update prod as master is stable enough.

Upvotes: 7

Views: 13528

Answers (6)

Bob Bruce
Bob Bruce

Reputation: 1

I had the same issue with a remote GitHub Enterprise. I went to the GitHub web interface and created a pull request to merge my branch into master. Since it didn't have my local workspace.xml changes because they were ignored, it allowed the pull request and merge. After the merge and pull, I was able to switch branches again locally.

Upvotes: 0

Binyamin Regev
Binyamin Regev

Reputation: 1030

I got the same message about a file I didn't change: $ git checkout thatbranch error: Your local changes to the following files would be overwritten by checkout: wwwroot/js/intake/some-file.js Please, commit your changes or stash them before you can switch branches. Aborting

I have no problem to checkout any other branch, but not thatbrach. I went to the path and did git status the result was that it's up to date. I did git stash and got a message that there are no local changes.

Finally, I did what every software engineer does: Reboot my machine - that also didn't help.

I went again to that path and deleted some-file.js and did the checkout again - this time it worked. Just in case I did pull to verify everything is ok and up to date.

That solved my problem.

Upvotes: 1

Patrizio Bertoni
Patrizio Bertoni

Reputation: 2712

In my case, it was due to my former

git update-index --assume-unchanged <filename>

that was conflicting on that file. Undoing this situation via a

git update-index --no-assume-unchanged <filename>

And committing the resulting new status, everything fixed up.

Update: fix typo in parameter.

Upvotes: 6

Boyang
Boyang

Reputation: 2566

Both answers are correct about the cause. But git stash doesn't work because either it won't stash changes made to workspace.xml which is ignored, or workspace.xml has been changed somewhere else down the tree.

What I did is to git clone the repo to somewhere else, checkout to mybranch, edit gitignore, commit and then merge from master.

Upvotes: 1

gravetii
gravetii

Reputation: 9624

Check if .idea/ is also present in the .gitignore of the branch you are checking out apart from that of the branch you are currently on.

A solution is to just stash the changes made to the .idea/workspace.xml file, checkout to your mybranch and then add the .idea/ folder to .gitignore, switch back to master branch and then apply the stashed changes, then merge both the branches.

Upvotes: 1

simpleigh
simpleigh

Reputation: 2894

.idea/ is in the .gitignore in your current branch. Data given by git status is consistent with that. Are you sure that .idea/ is in the .gitignore of mybranch?

If mybranch includes .idea/workspace.xml then this file will be overwritten when you checkout - even if it's currently ignored.


EDIT

You can work around the restriction by temporarily moving .idea/workspace.xml elsewhere - e.g. move it to .idea/workspace.xml.old. Then checkout mybranch and see what happened - what's in .gitignore, what's in .idea/workspace.xml. You can easily move the file back once you see what's happened.

Alternatively you can try git stash, but it's probably sensible to avoid adding extra complexity until you've figured out what's going on!

Upvotes: 0

Related Questions