是否有可能撤销提交? [英] Is it possible to revoke commits?

查看:76
本文介绍了是否有可能撤销提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个仓库和5个提交:
$ b

  • commit 1

  • commit 2

  • 提交3

  • 提交4

  • 提交5



现在我意识到提交4和5是一个坏主意。我想完全删除提交4和5中提交的所有更改。如何执行? 如果提交4和5是提交只在你的版本库中,并且没有被任何其他版本库推送或拉出,你可以简单地: / p>

您可以使用 git log 或你可以使用更高级的命名技巧,参见 git help rev-parse ,特别是'指定修订'部分。

使用此命令将使分支尖端的提交4和5不可用。然而,提交将不会丢失,因为这些提交保存在分支的reflog中。您可以使用 git reflog 来识别无法访问的提交。然后可以用另一个 git reset --hard 来完成恢复。此页此处描述了这一切很好。



建议您定期运行 git gc ;一些命令也会自动为您执行此操作。这本质上是对存储库执行内务管理,比如压缩文件修订(以减少磁盘空间并提高性能)并删除不可达的对象。无法访问的对象将在30天后(缺省值)从存储库中删除。这可以使用配置选项 gc.reflogExpireUnreachable 进行更改。


Assume we have a repository and 5 commits:

  • commit 1
  • commit 2
  • commit 3
  • commit 4
  • commit 5

And now I realize commits 4 and 5 are a bad idea. I want to completely remove all changes committed in commit 4 and 5. How to do it?

解决方案

If commits 4 and 5 are in your repository only and haven't been pushed to or pulled by any other repository you can simply:

git reset --hard SHA1_HASH_OF_COMMIT_3

You can find out the SHA1 hash of a commit by using git log or you could use more advanced naming techniques, see git help rev-parse in particular the section 'SPECIFYING REVISIONS'.

Using this command will leave commits 4 and 5 unreachable from the tip of the branch. The commits will, however, not be lost as those commits are kept in the branch's reflog. You can use git reflog to identify an unreachable commit. Restoring can then be done with another git reset --hard. This page here describes it all pretty well.

It is recommended that you run git gc on a regular basis; some commands also do this for you automatically. This essentially performs 'housekeeping' on the repository, such as "compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects". Unreachable objects are pruned from the repository after (a default of) 30 days. This can be changed using the configuration option gc.reflogExpireUnreachable.

这篇关于是否有可能撤销提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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