Earlier we learnt how to fetch, pull and push the changes. We also learnt how to stash and clean your current working tree. But what if we want to reset the current HEAD to the original state and start from the beginning?
Today we are going to discuss how to do this with the ‘git reset’ command. Continue learning more about Git on my ‘Git Tutorial’ page.
I hope you are reading all my posts from my ‘Git Tutorial Series’ and following the important commands and parameters used while working with Git Repositories. Don’t forget to discuss about your doubts, ask any queries and share your tips on the said page to help the others to make their life easier.
Let’s start our today’s discussion. For example, on a bright sunny morning you are working on something and then you realized that, you did some mistakes and have to start from the beginning or just have to do a undo of the local changes that you are working on a file/folder. What to do? You can directly use a ‘Git clean’ command to undo the changes, but to move the HEAD to original state or to unstage the current changes you have to reset it.
Git provides a command ‘Git Reset’ which will help you to easily undo the local staged changes and move your HEAD to it’s original snapshot (i.e. the last commit state).
In addition to moving the current branch, you can also use ‘Git reset’ to alter the staged snapshot and/or the working directory by passing it one of the following flags: --soft, --mixed, --hard, --merge, --keep. Let’s discuss all the parameters and flags to use in the command:
$ git reset
$ git reset <path>
$ git reset <commit>
$ git reset --soft
$ git reset --soft <commit>
$ git reset --mixed
$ git reset --mixed <commit>
$ git reset --hard
$ git reset --hard <commit>
$ git reset --merge
$ git reset --merge <commit>
$ git reset --keep
$ git reset --keep <commit>
Was this post useful? If you came to this page by searching online about “git basics” or “git tutorials”, please have a look into my other blog posts. Subscribe to the RSS feed or the email newsletter to keep yourself updated.