68

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?

baloo
  • 7,635
  • 4
  • 27
  • 35
  • This happened to me because of using [`vagrant-fsnotify`](https://github.com/adrienkohlbecker/vagrant-fsnotify) to forward filesystem events to a VM. Internally it calls `touch` in the VM to trigger incremental builds, etc. – jchook Jun 22 '21 at 15:42

3 Answers3

92

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.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
  • 3
    The `git clean` part was what I was looking for, thanks! I should also add that some of the files might have been open in TextMate for editing – baloo Oct 07 '10 at 21:25
  • 13
    Just be careful as this will delete any untracked files & folders that you may have.... – Chris Kooken Oct 07 '10 at 21:27
  • 4
    Yes, be careful with the `git clean -f -d`. It will delete anything you're ignoring too. – dmgig Nov 28 '17 at 16:58
  • 2
    @dgig I guess it is `git clean -x` that also deletes ignored files. – Guney Ozsan Mar 12 '18 at 23:24
  • If a file is locked by an IDE, why is git removing it?? Moreover, an IDE should never lock text files from writing (only on a save) :'-( – jaques-sam Oct 19 '18 at 14:30
  • 1
    The same thing happened to me with Visual Studio Code on MacOS: (i) stale files from the branch I switched away from that are not tracked in the current one, (ii) `git status` returns "working tree clean". Pretty strange. – floatingpurr May 15 '19 at 10:22
  • what was the fix @floatingpurr – itsjwala Apr 22 '20 at 09:38
  • Any fix found so far – floatingpurr Apr 22 '20 at 11:34
  • You saved my day. This behaviour is potentially dangerous. Any work arounds for ensuring your IDE (phpstorm) doesn't do this? – digout Jun 22 '20 at 06:42
  • I have multiple stashes in my repo. Will git clean or git reset do anything to the stashes? I'm afraid to try it out at this time. – Tony Sep 26 '22 at 08:37
12

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
Community
  • 1
  • 1
Anzumana
  • 361
  • 3
  • 8
  • @baloo Yeah but it's still quite useful (= SE may have it's inconsistent mod and policy problems across it's sites, but it's absolutely a worthy archive of esoteric Q&A. – monsto Oct 01 '18 at 18:01
7

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

More explanations here : Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?

Community
  • 1
  • 1
Ousmane
  • 2,673
  • 3
  • 30
  • 37