Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git commit --amend - commit hash is changed when no changes are made

I open for some correction of my understanding of this, but I am not sure why this happens.

When using git, I understand that if I make a change to a file and then commit it I get a new hash because the file has changed.

My understanding of git commit --amend is that I can make changes to the last commit.

Now I can understand that if I change the commit message, the hash will change.

But when I do not make any changes and just save and exit out of the editor (I may have changed my mind on needing a change) why does the hash change? All my files and everything is the same, but I have saved it. When I of a git log the time of the commit hasn't changed, it just appears twice in the log with the same time, the same message, same files except a different commit hash.

Why does it change if no alterations have been done??

like image 646
jwknz Avatar asked Sep 08 '15 22:09

jwknz


People also ask

Does amending a commit change its hash?

If you amend the commit message, or the files in a commit, this will change the git hash.

What happens when you amend a commit?

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.

Does rebase change commit hash?

Git Rebase could update the hash of a commit mentioned in the message of another commit, if that hash changes during rebase as well. 1.

Can you make a commit with no changes?

You can start your build without making any modifications to the project by pushing an empty commit.


1 Answers

Git hashes are calculated using a number of items, including the author name/date, commit name/date commit message, tree, and parent SHA, among others. When you amend a commit, the commit name and date are updated. (You generally don't see the commit name and date unless you pass additional formatting options to git log.) Because that has been updated, the commit hash will change when a commit is amended.

like image 55
mipadi Avatar answered Sep 19 '22 21:09

mipadi