Reputation: 41510
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.
Upvotes: 3
Views: 4628
Reputation: 15164
While several developers work at the same time on the same git repository, it is very common when someone asks you to review their PR (Pull Request).
That happens to me very often. It used to be a bit painful to have to switch from my feature branch to the PR branch, when I had uncommitted WIP (work in progress).
I used to use git stash
, but now I use git reset --soft
.
This is how the flow looks like when someone asks me to review their PR
git commit -m 'WIP whatever' # instead of git stash
git checkout develop
git pull
git checkout pr-branch
Then, after reviewing and merging in GitHub…
git checkout my-branch
git rebase develop # to sync my feature branch
git log # to see which commit hash was the one from the PR
git reset --soft 2cf4332 # resets from my WIP commits
# to the PR commit.
I wrote a whole blog post on the topic.
Upvotes: 0
Reputation: 213837
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
Upvotes: 8
Reputation: 225281
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.)
Upvotes: 2