I have been working with git for just over a month. Indeed I have used reset for the first time only yesterday, but the soft reset still doesn\'t make much sense to me.
While I really like the answers in this thread, I use git reset --soft
for a slightly different, but a very practical scenario nevertheless.
I use an IDE for development which has a good diff tool for showing changes (staged and unstaged) after my last commit. Now, most of my tasks involve multiple commits. For example, let's say I make 5 commits to complete a particular task. I use the diff tool in the IDE during every incremental commit from 1-5 to look at my changes from the last commit. I find it a very helpful way to review my changes before committing.
But at the end of my task, when I want to see all my changes together (from before 1st commit), to do a self code-review before making a pull request, I would only see the changes from my previous commit (after commit 4) and not changes from all the commits of my current task.
So I use git reset --soft HEAD~4
to go back 4 commits. This lets me see all the changes together. When I am confident of my changes, I can then do git reset HEAD@{1}
and push it to remote confidently.
Another use case is when you want to replace the other branch with yours in a pull request, for example, lets say that you have a software with features A, B, C in develop.
You are developing with the next version and you:
Removed feature B
Added feature D
In the process, develop just added hotfixes for feature B.
You can merge develop into next, but that can be messy sometimes, but you can also use git reset --soft origin/develop
and create a commit with your changes and the branch is mergeable without conflicts and keep your changes.
It turns out that git reset --soft
is a handy command. I personally use it a lot to squash commits that dont have "completed work" like "WIP" so when I open the pull request, all my commits are understandable.
I use it to amend more than just the last commit.
Let's say I made a mistake in commit A and then made commit B. Now I can only amend B.
So I do git reset --soft HEAD^^
, I correct and re-commit A and then re-commit B.
Of course, it's not very convenient for large commits… but you shouldn't do large commits anyway ;-)
"Oops. Those three commits could be just one."
So, undo the last 3 (or whatever) commits (without affecting the index nor working directory). Then commit all the changes as one.
> git add -A; git commit -m "Start here."
> git add -A; git commit -m "One"
> git add -A; git commit -m "Two"
> git add -A' git commit -m "Three"
> git log --oneline --graph -4 --decorate
> * da883dc (HEAD, master) Three
> * 92d3eb7 Two
> * c6e82d3 One
> * e1e8042 Start here.
> git reset --soft HEAD~3
> git log --oneline --graph -1 --decorate
> * e1e8042 Start here.
Now all your changes are preserved and ready to be committed as one.
Are these two commands really the same (reset --soft
vs commit --amend
)?
Any reason to use one or the other in practical terms?
commit --amend
to add/rm files from the very last commit or to change its message. reset --soft <commit>
to combine several sequential commits into a new one.And more importantly, are there any other uses for reset --soft
apart from amending a commit?
One practical use is if you have committed to your local repo already (ie. git commit -m ) then you can reverse that last commit by doing git reset --soft HEAD~1
Also for your knowledge, if you have staged your changes already (ie with git add .) then you can reverse the staging by doing git reset --mixed HEAD or i've commonly also just used git reset
lastly, git reset --hard wipes everything out including your local changes. The ~ after head tells you how many commits to go to from the top.