Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does git commit --amend work, exactly?

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.

like image 601
Shaun Luttin Avatar asked Sep 26 '14 00:09

Shaun Luttin


People also ask

What happens if you -- Amend modify a commit after a push?

If you amend the HEAD commit and push usually (without --force) then surprisingly it does not fail. HEAD commit message is updated with the changed commit Id. It means other commit IDs except HEAD remains intact.

What is commit staged amend?

The git commit –amend command lets you modify your last commit. You can change your log message and the files that appear in the commit. The old commit is replaced with a new commit which means that when you amend your old commit it will no longer be visible in the project history.

What does git commit -- amend -- no edit do?

Use the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message. Replace the tip of the current branch by creating a new commit.


1 Answers

Assume that you're in a clean working state and that your repository 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 repository (hence its "transparent" style on my graph). It still lives in your repository's object database, but it 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 repository 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.


like image 159
jub0bs Avatar answered Oct 09 '22 00:10

jub0bs