문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top