Git not removing files when switching branch

GitMacos

Git Problem Overview


Sometimes when switching branches using Git (version 1.7.2.1) it does not seem to remove the files/directories I created specific to the branch I switched away from. Neither does it list it as untracked when running git status or any log entries for those files.

This only happens occasionally and I'm not sure why or how to reset it so the files not belonging to the current branch gets deleted. If I delete the files manually, it gets in sync again (as in gets deleted/revived when switching branch).

Anyone experienced this?

Git Solutions


Solution 1 - Git

I have seen this too. I usually just do a git reset --hard followed by a git clean -f -d and it usually does the trick.

It seems to definitely happen the most often when my IDE has a lock on one of the files in the branch i'm switching from.

Solution 2 - Git

First:
git reset --hard

Reset the repository to the state of the last commit.
Since git normally does not remove files it is not tracking those could still cause issues.

Then:
git clean -d --dry-run

See what files would get deleted. We don't want to loose valuable work. and if that is ok:

git clean -d

Solution 3 - Git

I had the same problem because I did not commited files before switching branch !

More explanations here : https://stackoverflow.com/questions/5531362/why-git-keeps-showing-my-changes-when-i-switch-branches-modified-added-deleted

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbalooView Question on Stackoverflow
Solution 1 - GitChris KookenView Answer on Stackoverflow
Solution 2 - GitAnzumanaView Answer on Stackoverflow
Solution 3 - GitOusmaneView Answer on Stackoverflow