简体   繁体   English

将Git存储库还原到特定日期

[英]Restoring a Git Repository to a particular date

I would like to restore my local git repository to a particular date. 我想将我的本地git存储库恢复到特定日期。 By restoring, I would like to keep all the commits and history which occurred till that date and time. 通过恢复,我想保留所有提交和历史,直到那个日期和时间。

I am all okay to loose changes and commits which were performed after that date and time. 我可以在这个日期和时间之后执行松散的更改和提交。 (Assuming Jan 1st, 2014 11:30 AM) (假设2014年1月1日上午11:30)

Is this possible ? 这可能吗 ?

git reset --hard 'master@{2014-01-01 11:30:00}'

那将在那之前将主分支重置为最接近的提交。

To update all of your refs, you can use git for-each-ref which can generate the command to run on each ref, then pipe the output of that to your shell. 要更新所有引用,可以使用git for-each-ref ,它可以生成在每个引用上运行的命令,然后将其输出传递给shell。

git for-each-ref --shell \
    --format="git update-ref %(refname) %(refname)@{2014-01-01 11:30:00}" | sh

Any time you run a command like this, make sure you have a backup so you can restore the repository if something goes wrong. 每次运行这样的命令时,请确保备份,以便在出现问题时可以恢复存储库。 Also, try it once without the pipe to sh to see the commands that will be executed and make sure they look reasonable. 另外,尝试一次没有管sh以查看将要执行的命令,并确保他们看起来是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM