Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use git reset --soft?

Tags:

git

git-reset

When do you use git reset --soft? I use git reset --hard all the time but never seem to find a case to use git reset --soft.

like image 998
Boon Avatar asked Aug 27 '26 07:08

Boon


2 Answers

For one thing, it lets you squash commits together. Let's make three temporary commits while we're working on something:

... change files ...
git commit -m 'Temporary 1'
... change files ...
git commit -m 'Temporary 2'
... change files ...
git commit -m 'Temporary 3'

This gives us a history like this:

A ---> T1 ---> T2 ---> T3
                       ^master

Okay, now these changes are ready to go out, but I want to squish them into one commit because they comprise one logical change.

git reset --soft HEAD~3

This gives us a history like this:

A ---> T1 ---> T2 ---> T3
^master

But the index (changes to be commited) contain everything from T3. Note that T1-T3 are orphaned.

git commit -m 'Full commit message'

Now we have this:

A ---> T1 ---> T2 ---> T3
  \
   --> B
       ^master

B and T3 have the same contents, but different histories.

amend

The git commit --amend can be rewritten in terms of git reset, at least in simple cases:

git commit --amend

is often the same as...

git reset --soft 'HEAD^'
git commit
like image 193
Dietrich Epp Avatar answered Aug 29 '26 06:08

Dietrich Epp


Oops, I didn’t mean to commit just now.

git reset --soft HEAD^

(I tend to let it default to --mixed instead, but it’s pretty close.)

like image 31
Ry- Avatar answered Aug 29 '26 06:08

Ry-



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!