What to Do if Git Loses Files When You Move a Folder

While refactoring the folder structure of a project I run into something strange. Git suddenly stopped to track a few files and marked them as deleted. That was not my intention then those files are needed. How can we fix this?

 

The direct approach

The not working approach was this:

  1. Open the project in Windows Explorer (the file manager)
  2. Move the folder at its new location
  3. Give the folder a new name
  4. Open the Git console
  5. Mark files as moved with git add -A .
  6. Notice the deleted files
  7. Abort the change

 

The longer way

To get a working approach I needed a few steps more:

  1. Open the project in Windows Explorer (the file manager)
  2. Move the folder at its new location
  3. Give the folder a new name
  4. Open the Git console
  5. Mark files as moved with git add -A .
  6. Commit everything except the deleted files
  7. Throw away all uncommitted changes with git clean -dfx
  8. For each missed file run an explicit git mv old/path/fileA new/path/fileA
  9. Commit the now correctly moved files

The 4 extra steps where manageable with the number of affected files. Yet I am still surprised that I had to take this detour.

 

Parting thoughts

I could mitigate the situation and find a solution that keeps the file around. However, I still have no idea why this happened. The strange thing was that all the problematic files were created by the same commit. Please post a comment if you have an idea how this could happen.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.