Updated March 31, 2023
Introduction to Git Reset Hard
Git Reset is the magic command by which we can go back and observe the previous version of the code and use it as the current code version.
Git reset comes in various flavors:
- Soft
- Mixed
- Hard
Each of them providing their own usage and each comes with its own dangers.
- Soft: This command git reset -soft is used to unstage the files which we have staged using the git add command.
- Mixed: This command git reset -mixed is used to remove the file which we have committed using the git commit command.
- Hard: This command git reset -hard is used to remove all things which we have pushed in our code.We can further specify the point to which we want the code to be removed.
Git Reset Hard Working
Now consider we have repository with the name Test Repository and we have few files in it shown below:
As you can see we have a file called githardexample. Now we will look at the content of the file.
As you can see the file is empty and we have no content in it.
We will now add text in this file.
This is the text we have entered in the text file and saved it.
Our next text will be to add the changes and commit them to our repository. So in order to do this use our normal commands.
After pushing those changes we will now run the git reset -hard command and as per the command all the changes which we have made to our prehistory should be removed.
But first in order to do so we have to specify the point to which we want the changes to be effective.
Let’s run git log to check number of commit which are present in the repository right now.
As you can see there are 2 commits and we want to move our HEAD to the first commit when we added the file.
So we will specify the particular commit id after writing git reset —hard. You only need to write the first 4 letters of the commit id.
You should get a message that the HEAD is at the specific commit which is 20c9.
Now go back to your original file and check its content.
You will observe that the file is empty and it has reached to its original state in which we have added.
Now run the git log command .What you will observe is the commit in which we have added the content to the file has been removed and we only have one commit. The commit corresponds to the addition of the file.
Examples of Git Reset Hard
Given below are the examples:
Example #1
In this example instead of modifying the content of the file we will be adding several files and go to the initial point when we have only one file.
I have added a new file in the repository .The name of the file is File1. I will add and commit the file.
You can see we have a commit for addition of a new file which is commit idbcec6102.
Now we will add one more file named File2 to the repository and do the add, commit and push command.
Now you can see have a new file named File 2 in our repository. We have performed our add commit and push routine.
You can see in the above image that we have anew commit which starts with commit id 7bb592.
To give more clarity on the concept lets edit one of the file named File1 for some data. It was empty earlier and now we will add some text to it.
Observe we have added the text to the file and now lets check out git log.We have a new git commit with id bc5a.
Example #2
Now for example consider I have made and pushed File2 but because of this my code has stopped working so in order to remove File2 I will first have to check the commit at which I have added the File2 and use git reset hard command along with the commit id.
The commit at which I have added File2 is with id 7bb59.So I will go to a commit before it.
The command which I will use now is git reset —hard 00efc
If you check the repository now you will observe that the File2 has been removed.
As you can observe the File2 has been removed and along with it all the changes which we have made to File 1 have also been removed.
And our commits have been reset to the point 00efc21 commit id as shown in the image above.
This is the main disadvantage of git reset -hard that all the changes are removed upto a certain point even though you did not want to remove them. Like in above example we did not want to remove the changes in File 1 but it got removed
Conclusion
Git reset hard is a command which should be used carefully as to can delete your entire code commit history and what you have done till a particular commit. Therefore it should be used with certain caution. Apart from git reset hard there are other commands also which can be used to backtrack your changes such as git checkout. Depending on your requirement make use of the appropriate command to backtrack your changes
Recommended Articles
This is a guide to Git Reset Hard. Here we discuss the introduction, git reset hard working with respective examples. You may also have a look at the following articles to learn more –