Clay Nichols Clay Nichols - 11 days ago 6
Git Question

Can I replace a Git repo with its backup?

If I make a copy of

which is under Git version control (with a
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
with this copy and just continue working?

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.


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!