Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between commit --amend and reset --soft

Tags:

git

The idea of my question is researching...
What is the essential difference between commit --amend and reset --soft

Investigation process in steps:

vim index.js > edit > save
git add index.js
git commit -m '...'
git push origin

Now I need to rewrite the commit I've sent previously. For that matter I should use:

vim index.js > edit > save
git add index.js
git commit --amend --no-edit
git push --force origin

Essentially I've got another one SHA1 and for the sake of analogy three objects in .git/objects directory, but the git log shows me two SHA1 and I totally agree with it because the commit has been amended.

Let's go back a bit. Instead of git commit --amend I've executed the git reset --soft HEAD~, the HEAD pointer stands for stage version of the file, write some code and execute:

git add index.js > git commit -m '...' > git push --force origin

.git/objects directory contains one more SHA1, but the history has been amended with the new one.

So the way I'm telling that the commands git commit --amend and git reset --soft have the same behavior

Or I'm not right?

like image 379
yaro Avatar asked Jan 25 '26 13:01

yaro


1 Answers

Both commands can be used to achieve a similar end goal, but they don't do the same thing. git commit --amend makes a new commit with whatever be in the stage, and that new commit replaces whatever commit were the previous HEAD of your branch.

On the other hand, git reset --soft moves the HEAD pointer of the branch back one commit, but leaves both the stage and working directory intact. This has the effect that your branch now appears to be on the previous commit, but all the work you did to generate the old HEAD commit now appears as being staged. If you now change things in the working directory, stage those changes, and commit, you would also be making a new commit to replace the old HEAD, which is a similar result to git commit --amend.

One advantage which git reset --soft (which is identical to saying git reset --soft HEAD~1) has over git commit --amend, is that the former can reset across multiple commits. This can be useful if you want to rewrite say the latest 4 commits in your branch. On the other hand, git commit --amend is a one horse show, and only works on the HEAD commit.

like image 54
Tim Biegeleisen Avatar answered Jan 27 '26 03:01

Tim Biegeleisen



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!