bolbol
bolbol

Reputation: 345

Git status reports deleted files which still exist

My Git status is showing all files deleted in my working tree which is incorrect, they are still there.

To resolve it, I should execute git reset --hard which reset all my modifications to other files.

Any idea how to solve it or how did it happen?

git status output:

On branch develop
Your branch is behind 'origin/develop' by 15 commits, and can be fast-forwarded.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted:    .gitignore
deleted:    v3/.gitignore
deleted:    v3/index-test.php
deleted:    v3/index.php
deleted:    v3/protected/.gitignore
deleted:    v3/protected/.htaccess
deleted:    v3/protected/Gruntfile.js

Edit: The above is not the complete git status output. Those files that I was working on are not staged for deletion.

Upvotes: 7

Views: 5257

Answers (4)

Nathanael Istre
Nathanael Istre

Reputation: 125

I had a similar issue where I have my git repo in a folder synced by OneDrive. I'm guessing it was caused by some syncing process. I tried to no avail:

  • Stashing the changes
  • Committing the changes and then git reset --hard HEAD^
  • Myriad other variations of git reset...

Then I tried:

  • Reverting the restoring the "deleted" files via git but it couldn't because the gitlog said "files already exist".
  • Next, in the file explorer I cut all my files in the repo and pasted them somewhere else on my machine outside of the repo, then I let git restore the files and it was happy.

This was manageable for me as this repo is my Dendron repo so it was about 450 simple text files. So, this approach may not be feasible for all.

Upvotes: 0

Litmus
Litmus

Reputation: 10996

if you deleted a file from local disk using

rm file.name

and if file.name was already checked into git. Then you should actually issue this command

git rm file.name

to tell git that you don't want it to track that file anymore

Upvotes: 2

John Szakmeister
John Szakmeister

Reputation: 47122

If the only issue is that the some files have been removed from the index, but are still in the working tree, then all you need to do is:

git reset

By default, git reset defaults to mixed mode which does this:

--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

So it will leave your changes intact but unstage everything. If you find that the files are actually missing, then you'll need to check them out to restore them:

git checkout -- path/to/file/or/dir

You could parse the short form of git status using cut and xargs to help do this if you have lots of files. I'll leave that as an exercise for the reader though, since you're claiming that all the files are there.

Upvotes: 6

equivalent8
equivalent8

Reputation: 14237

  git co my_annoying_file
  git rm my_annoying_file
  git commit -m "removing annoying file"

now they are truly deleted from git tracking

...also git reset --hard may be to brittle for this scenario, consider git reset HEAD my_annoying_file

Upvotes: 1

Related Questions