git commit --amend在分离的HEAD状态 [英] git commit --amend in detached HEAD state
问题描述
我知道修改旧的GIT提交的正确方法是使用 rebase --interactive
,但是为了弄清楚概念,我想理解
I understand that the correct way of amending an old GIT commit is to use rebase --interactive
, but just to get clear on the concepts, I would like to understand what happens when I do
-
git checkout< commit>
- 更改文件中的内容
- 将更改后的文件添加到索引中
- ,然后
git提交。 --amend
git checkout <commit>
- change something in a file
- add the changed file to the index
- and then
git commit . --amend
执行此操作时,它没有修改提交,而是分支了一个新的提交
When I do this, instead of amending the commit, it branches a new commit off of the PARENT of that same commit.
这只是GIT告诉我无法修改已经有子项提交的提交的方法吗?
Is this just GIT's way of telling me that I cannot amend a commit that already already has children commits?
推荐答案
在Git中,一旦创建了提交,就将其固定在石头上;您无法更改。您可以做的所有事情-通过对其进行修改,精心挑选等-创建一个新的类似于目录的提交。
In Git, once a commit is created, it's set in stone; you cannot change it. All you can do—by amending it, cherry-picking it, etc.—is create a new commit that "resembles" it.
我了解您的困惑:修改有点用词不当;这有点令人误解,因为它建议修改某些内容。在Git中,修改提交实际上包括创建一个全新提交,该提交具有与原始提交相同的父级。
I understand your confusion: "amend" is a bit of a misnomer; it's somewhat misleading, as it suggests modifying something in place. In Git, amending a commit actually consists in creating a brand new commit that has the same parent(s) as the original commit.
作为示例,假设运行 git checkout B
之后,您处于以下情况:
As an example, let's assume that, after running git checkout B
, you're in the following situation:
(您的 HEAD
是分离的,但这不重要。)无论您是否进行阶段更改,运行 git commit --amend
都会使您处于这种情况:
(Your HEAD
is detached, but that's beside the point.) Whether or not you make and stage changes, running git commit --amend
will put you in this situation:
提交 D
可能非常非常类似于 B
;特别是,它可能具有与 B
完全相同的补丁,完全相同的提交消息,等等。但是,(提交,作者)时间戳通常会有所不同(除非您可以根据以下内容修改提交),这意味着 D
的SHA-1与 B
的SHA-1不同;并且如果两个提交不具有相同的SHA,则它们也不是相同的提交。
Commit D
may be very, very similar to B
; in particular, it may have exactly the same patch, exactly the same commit message as B
, etc.. However, (commit, author) timestamps will usually differ (unless you can amend a commit under a second!), which means the SHA-1 of D
will differ from that of B
; and if two commits don't have the same SHA, they're not the same commit.
当我们说 B
是 C
的父提交,我们的意思是 commit C
引用提交 B
通过其SHA 。
但是,提交 C
无法知道有关提交 D
的SHA的任何信息,因为提交<在 C
之后创建code> D 。因此, D
不能是 C
的父母。这就是为什么commit D
在切线上消失并且没有任何后代的原因。
When we say B
is a parent commit of C
, we mean commit C
references commit B
by its SHA.
However, commit C
has no way of knowing anything about the SHA of commit D
, because commit D
was created after C
. Therefore, D
cannot be C
's parent. That's why commit D
goes off on a tangent and doesn't have any descendants.
如果您要以以下状态着陆,
If you want to land in the following state,
其中, B'
甚至与 B
只是稍有不同,应该使用 git rebase -i
,而不是 git commit --amend
。
where B'
is even only slightly different from B
, you should use git rebase -i
, not git commit --amend
.
这篇关于git commit --amend在分离的HEAD状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!