Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't git amend warn you when there's nothing to amend?

I oftentimes make the mistake of making a change to a file right after a commit, then running git commit --amend followed by a quick git push -f origin master

The issue, obviously, is that I never ran git add foo.md. So my amend didn't actually amend any of my committed code.

Question is: Why doesn't git warn you when amending a commit with no changes? Better yet, why does it even complete successfully? Is it merely behaving like a "touch" in changing the timestamp of a given commit?

like image 408
acco Avatar asked Oct 23 '25 00:10

acco


2 Answers

The committer date (%ci) keeps being reset, even if the commit author date (%ai) remains the same:
For instance, if I commit --amend -no-edit the last commit on the Git repo, the author date remains unchanged, but I just created a new commit: replacing the old one with the same content, but a different commit date:

P:\git\git>git show -s --format="%ci" e0d6576639261beefb40afa52696d02f16c490ed
2012-12-11 03:42:23 +0100  <== just now

P:\git\git>git show -s --format="%ai" e0d6576639261beefb40afa52696d02f16c490ed
2012-12-07 14:18:55 -0800  <== original date when the author did commit

So any amend will result in a different SHA1.

From the Pro-Book:

  • The author is the person who originally wrote the work,
  • whereas the committer is the person who last applied the work.

A commit --amend will always recreate a new commit, if only because the committer date is changing.

like image 199
VonC Avatar answered Oct 25 '25 17:10

VonC


Amend is useful for just changing the commit message or other details of the commit. So running it without any changes to the working dir makes a lot of sense. To prevent problems like this in the future, I would recommend using

git commit -a --amend
like image 45
Chronial Avatar answered Oct 25 '25 18:10

Chronial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!