0

I have unintentionally deleted a file in the .git folder. The repository seems to still work (commit, push, pull, ... are working without problems).

Could a deletion of a file inside the .git folder affect the files outside this folder?
Could the deletion undo some changes in the repository files?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Simone
  • 19
  • 1
    @Yatin I believe the question is not about regular tracked files, but about intrinsic files of `.git` directory – Alexey S. Larionov Aug 02 '20 at 12:58
  • Deletion of a file inside of the `.git` folder shouldn't immediately affect any files outside of that folder. Depending upon what file was deleted, it might affect repository status information about certain file(s) when you do a `git status`, for example. There are a lot of different files under `.git` (config, etc). If your commit, push, pull, etc work, then your config seems Ok. You don't know what file you deleted? – lurker Aug 02 '20 at 13:01
  • Thank you for your answer. After the deletion I have committed, pushed and pulled the repository. Could it have affected the files outside the git. folder? – Simone Aug 02 '20 at 13:06
  • If the question is whether you may have accidentally lamed your repository: Yes, of course. – matt Aug 02 '20 at 13:41
  • Does this answer your question? [Restore a deleted folder in a Git repo](https://stackoverflow.com/questions/30875205/restore-a-deleted-folder-in-a-git-repo) – Daniel Widdis Aug 03 '20 at 03:30

2 Answers2

4

Could a deletion of a file inside the .git folder affect the files outside this folder?
could the deletion undo some changes in the repository files?

It really depend on what exactly was deleted.
Deleting .git/index for instance is actually not critical, and would for GIt to rebuild automatically said index.

To be on the safe side, I would simply:

  • rename my current repo folder in myrepo.old
  • re-clone my repository in my existing folder myrepo
  • resume my work
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Could a deletion of a file inside the .git folder affect the files outside this folder?

No, git won't touch the working directory files unless you tell it to.

Could the deletion undo some changes in the repository files?

Yes. If you delete a reference in .git/refs you will lose that branch or tag, but the commit it points to will still be there and you can recover its location with git reflog.

If you delete a hook in .git/hooks that hook is gone.

If you delete an object file that could be the content of a file, directory, or commit. This would leave you with a corrupted history. git fsck will check the integrity of your repository. If you have a remote repository, you should be able to use it to replace the missing object.

If you delete .git/index you will lose your staged changes. You can re-add them.

See the Git Internals chapter of the Git Book for more detail.

Schwern
  • 153,029
  • 25
  • 195
  • 336