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.

 

Reset your local Git repository branch (www.kunal-chowdhury.com)

 

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:

 

Reset the current HEAD to last commit state:

$ git reset

 

Reset the current HEAD of the specified path to last commit state:

$ git reset <path>

 

Reset the current branch to a specific commit:

$ git reset <commit>

 

Reset the HEAD without touching the index file or the working tree:

$ git reset --soft

 

Reset the HEAD to a commit without touching the index file or the working tree:

$ git reset --soft <commit>

 

Reset the index but not the working tree (preserving the changes):

$ git reset --mixed

 

Reset the index to a commit but not the working tree (preserving the changes):

$ git reset --mixed <commit>

 

Reset the index and working tree:

$ git reset --hard

 

Reset the index and working tree to a specified commit:

$ git reset --hard <commit>

 

Reset the index and update the files in the working tree that are different:

$ git reset --merge

 

Reset the index to a commit and update the files in the working tree that are different:

$ git reset --merge <commit>

 

Reset the index and keep the files in the working tree that are different:

$ git reset --keep

 

Reset the index to a commit and keep the files in the working tree that are different:

$ 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.

 

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.