我可以用备份替换Git存储库吗? [英] Can I replace a Git repo with its backup?

查看:70
本文介绍了我可以用备份替换Git存储库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Git版本控制下(带有.git子文件夹)制作了MyDirectory的副本,然后进行了提交,是否全部表现"为自己的小版本,以及版本历史记录等?

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?

然后我可以用该副本替换以前的MyDirectory并继续工作吗?

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

背景: 我的Git回购犯规了.我希望我可以将其替换为前一天 entire 目录的备份,然后从那里继续前进.

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.

推荐答案

是的,您可以执行此操作,并且不会造成任何问题.

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

Git用来跟踪版本历史记录的所有内容都位于.git文件夹中.因此,用该文件夹的旧版本替换该文件夹将使您的存储库回滚到该旧状态. 不同于使用git checkoutgit revert进行Git回滚,您还将丢失此后已添加到存储库中的所有历史记录.

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.

为了安全起见,例如 @Danh建议,您应在还原备份之前删除该文件夹的现有版本. (实际上,由于Git对象是不可变的,所以我不确定这是否有必要.尽管这样也不会造成伤害.)如果您不想冒丢失现有文件夹版本中数据的风险,也可以移动或重命名它,然后还原备份.

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.

根据全部失败"的含义,仍然可以在不从备份中恢复的情况下进行恢复(尤其是如果您没有运行任何危险"命令,例如使用--force进行的任何操作).随时搜索在此处询问如果您需要帮助!

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!

这篇关于我可以用备份替换Git存储库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆