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.
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.
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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With