Can I replace a Git repo with its backup?

2019-09-14 22:14发布

If I make a copy of MyDirectory which is under Git version control (with a .git subfolder) and then do a commit, does this all "behave" as it's own little repo, with version history and such?

Could I then replace the previous MyDirectory with this copy and just continue working?

Background: My Git repo got all fouled up. I'm hoping I can just replace it with a backup of the entire directory from the previous day and just move on from there.

1条回答
不美不萌又怎样
2楼-- · 2019-09-14 22:43

Yes, you can do this, and it shouldn't cause any problems.

Everything that Git needs to keep track of your version history resides in the .git folder. Thus, replacing that folder with an older version of that folder will roll back your repo to that older state. Unlike doing a Git rollback with git checkout or git revert, you will also lose all history that has been added to the repo since then.

To be safe, as @Danh suggested, you should delete the existing version of the folder before you restore the backup. (Actually, since Git objects are immutable, I'm not sure if this will be necessary. It shouldn't hurt though.) If you don't want to risk losing data from the existing version of the folder, you can also move or rename it and then restore the backup.

Depending on what you mean by "all fouled up", it may still be possible to recover without restoring from backup (especially if you haven't run any "dangerous" commands, such as anything with --force). Feel free to search around or ask here if you need help!

查看更多
登录 后发表回答