使用 git 修改旧提交时会发生什么?
我真的不明白如果我检查旧的提交,进行一些修改,然后使用 git commit --amend 进行提交,会发生什么。
该更改会自动传播到未来的提交吗?它是如何运作的?
I don't really understand what happens if I check out an old commit, do some modifications, and commit using git commit --amend
.
Will that change automatically propagate to future commits? How does it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 git 中,提交只是对象。当您
git commit --amend
时,您只是创建具有相同父级的新提交。最初看起来像这样:现在您修改
C
,创建一个新的提交D
:旧的
C
目前仍然存在。但是,它不再被任何分支引用,因此下次发生垃圾收集时,它将被清除。In git, commits are just objects. When you
git commit --amend
, you're just creating a new commit with the same parent. Initially that looks like this:Now you amend
C
, creating a new commitD
:The old
C
is still there for the moment. However, it is no longer referenced by any branch, so the next time a garbage collection occurs, it's going to be swept away.为了补充约翰的答案,如果您修改旧的提交,其子项不会发生任何事情。
也许你想做的事情可以通过 压缩提交的交互式变基。
To complement John's answer, if you ammend an old commit, nothing happens its children.
Maybe what you want to do can be accomplished with a interactive rebase that squashes commits.
创建的新提交与旧提交具有相同的父级,并且您当前的分支现在引用新提交。旧的提交仍然在对象数据库中,可以使用 git reflog 找到。
A new commit is created with the same parent as the old commit, and your current branch now refers to the new commit. The old commit is still in the object database and can be found with
git reflog
.