How does git commit --amend work, exactly?

Shaun Luttin

I have seen GIT commit --amend in detached HEAD state. The question requires the answer to be more complex than needs be. I'd like to understand just how git commit --amend works in a normal HEAD situation.

jub0bs

Assume that you're in a clean working state and that your repo looks as follows:

enter image description here

If you then run

git commit --amend

write a commit message, save and quit your editor, the following happens:

  1. Your staging area—which, if you haven't staged any new changes, will be identical to commit f42c5—is used to create a new commit: 31b8e. Its parent(s) will be the same as that(those) of the commit you're amending: f42c5.
  2. The master branch reference is moved to point to that new commit (31b8e).
  3. The HEAD reference follows master.

enter image description here

Note that the amended commit (f42c5) is now unreachable from any reference in your repo (hence its "transparent" style on my graph). It still lives in your repository's object database, but will eventually be deleted for good, when Git runs its periodic housekeeping, or if you trigger it explicitly by running git gc (garbage collection).


Addendum (based on Jason Baker's comment): Note that, as long as the amended commit, f42c5, still exists in your repo and you have a way of finding out its commit ID (for example, by fishing it out of the master branch's reflog), you can still check it out. Running

git checkout master # just to be sure that master is the current branch
git reset --hard f42c5

or (assuming you haven't, in the meantime, made any new commit on master, reset master, or otherwise moved the master branch reference)

git checkout master # just to be sure that master is the current branch
git reset --hard master@{1}

would put you in the following situation:

enter image description here

But now, commit 31b8e would become unreachable.


Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to undo a git commit --amend

From Dev

What exactly is the git extensions "Amend last commit" button doing?

From Dev

Does git commit --amend include the files that were in the last commit?

From Dev

Does git commit --amend overwrite the old commit completly?

From Java

How to undo "git commit --amend" done instead of "git commit"

From Dev

Git: How to reuse commit rev across commit --amend?

From Dev

How to git bzr push after git commit --amend?

From Dev

How can I amend a non-HEAD commit with Git?

From Dev

How to edit/amend commit messages for commits previous to the last in Git?

From Dev

How can I amend a non-HEAD commit with Git?

From Dev

Git : an Equivalent to an interactive commit amend?

From Dev

Git : an Equivalent to an interactive commit amend?

From Dev

Understanding git commit with amend option

From Dev

"git commit --amend -c <commit>" not saving changes

From Dev

Cannot git merge because git commit --amend

From Dev

How to remove ignored files from current commit using git commit --amend

From Java

How exactly does the callstack work?

From Java

How exactly does CMake work?

From Java

How does lock work exactly?

From Dev

How does keytab work exactly?

From Dev

How exactly does linking work?

From Dev

How exactly does the MenuInflater work?

From Dev

How does the << Operator exactly work?

From Dev

How does this for/in loop work exactly?

From Dev

How exactly does a proxy work?

From Dev

How exactly does linking work?

From Dev

How exactly does DNS work?

From Dev

How exactly does "/bin/[" work?

From Dev

How does this for/in loop work exactly?