简体   繁体   中英

git commit --amend in detached HEAD state

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>
  • change something in a file
  • add the changed file to the index
  • and then git commit . --amend 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.

Is this just GIT's way of telling me that I cannot amend a commit that already already has children commits?

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.

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.

As an example, let's assume that, after running git checkout B , you're in the following situation:

在此处输入图片说明

(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:

在此处输入图片说明

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.

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,

在此处输入图片说明

where B' is even only slightly different from B , you should use git rebase -i , not git commit --amend .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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