Home > Software engineering >  git ignored file preventing switching branch
git ignored file preventing switching branch

Time:07-15

I got the impression that any ignored files are irrelevant in a git repo. But I ran into the situation below:

The .vs folder is listed in .gitignore:

$ cat .gitignore
.git/
.vs/
bin/
My Project/
obj/
packages/
App_Data/

But when I tried to switch to another branch, a file in .vs folder is blocking:

$ git checkout test
error: Your local changes to the following files would be overwritten by checkout:
        .vs/myproject/v16/.suo
Please commit your changes or stash them before you switch branches.
Aborting

And git will complain if I tried to add this file:

$ git add .vs/myproject/v16/.suo
The following paths are ignored by one of your .gitignore files:
.vs
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

And advise is appreciated.

CodePudding user response:

Ignored files are untracked, and git checkout will not overwrite untracked files. This is a safety thing to avoid permanently deleting content.

You can force the checkout with -f...

-f, --force

When switching branches, proceed even if the index or the working tree differs from HEAD, and even if there are untracked files in the way. This is used to throw away local changes and any untracked files or directories that are in the way.

Or, since it will be overwritten by the checkout anyway, you can delete the offending file then checkout.

I'd recommend deleting the file because its specific to one file, while -f could overwrite more content.

CodePudding user response:

Let git untrack files and folders listed in .gitignore

git rm -r --cached .
git add .
git commit -m "Remove ignored files"

details

How do I make Git forget about a file that was tracked, but is now in .gitignore?

git rm

CodePudding user response:

As you have added .vs/ to .gitignore but it is still in git cache. To get out of this situation. You have to remove it from the cache and then try changing the branch.

Please follow the below steps:

git rm -r --cached .vs

If it shows new changes being added to .gitignore then commit those changes and change the branch otherwise just change the branch and it will be all fine.

git checkout test
  •  Tags:  
  • git
  • Related