Different ways of Git reset
Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.
In simple words, reset helps you to undo changes in the repo.
If you mention any commit, it resets or undoes all the changes till the specified commit
We have three options to use in the reset which we will cover in this post.
Syntax
git reset <commit-hash>
The syntax is straightforward. It resets all commits till the mentioned commit. The mentioned commit is excluded
--hard
git reset <commit-hash> --hard
It deletes all the commits till the specified commit hash excluding the mentioned commit.
All the changes in the commit are gone forever. You can’t get it back. So be very careful when using this one.
All the staged changes and unstaged changes are also deleted forever.
--mixed
This is the default option for resetting. Git will use this strategy when you don’t mention anything.
This one deletes the commits. But the changes will be unstaged. You can use these changes to create a new commit or create a new branch from it or whatever suits you.
Unlike --hard the changes are not deleted. Only the commits are deleted.
And if you have any changes staged, it will move those changes to unstaged as well
So if you want to unstage all your changes, you can just make use of this one
git reset
The above command takes --mixed as the default and the HEAD as the default. So any staged files will be unstaged. And the HEAD commit is untouched.
--soft
This one deletes all the commits but moves all the changes in the commit to staged.
Any unstaged files are untouched. Only the reset commit changes are staged.
This will be useful if you want to make a single commit out of many commits which are already present
Use reset for only your local changes. If the changes are already pushed, use Git revert which will be clear.
That’s all for today.
See you next week :)