What happens when amending an old commit with git?
I don't really understand what h开发者_JAVA百科appens 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?
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:
{HEAD}
{master}
---[A]---[B]---[C]
Now you amend C
, creating a new commit D
:
{HEAD}
{master}
---[A]---[B]---[D]
\
\
[C]
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.
old commit
v
o--o--o--o--o--o--o--o--o < original branch tip
\
o < ammended old commit & new branch tip
Maybe what you want to do can be accomplished with a interactive rebase that squashes commits.
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
.
精彩评论