Home > Mobile >  Under what circumstances will git delete untracked files?
Under what circumstances will git delete untracked files?

Time:09-16

I want to be sure that I don't accidentally delete some untracked files when working with a git repository.

The commands I am aware of that can delete untracked files are:

  • git clean obviously, its purpose is to delete untracked files.
  • git stash -u, the files are deleted from working directory, but kept in the stash.

Are there other circumstances under which git will delete untracked files?

CodePudding user response:

Although it's not technically deleting the untracked file, if an untracked file is marked with .gitignore and you check out a commit that contains a file with that name, Git will sometimes feel free to overwrite that file with the tracked file. For a concrete example, see Stop git from silently clobbering ignorefiles (bug in git?)?

Having turned the ignored file into a tracked file extracted from the chosen commit, going back to some other commit in which the file doesn't exist will now remove the (tracked) file that came out of the chosen commit. So now the file is gone for real, though it took two git checkout or git switch operations to get here.

CodePudding user response:

Torek's example is pretty much the example I was going to give. When you say dangerous stuff like git checkout <filename> from a commit that contains it, Git will happily let you wipe out your version. Basically, any time you are as specific as to name the specific file you want to introduce, Git will just let you do it.

Plus of course git reset --hard can wreak all kinds of havoc.

  •  Tags:  
  • git
  • Related