Home » Git Reset: Delete Commit in Soft, Mixed, or the Hard Way

Git Reset: Delete Commit in Soft, Mixed, or the Hard Way

by Imran Shaikh
1418 views
Git reset is used for resetting the changes you made in the git branch. You can soft, mixed or hard reset the changes.

Hey, Tea Lover! Today’s post is about how you can reset the changes in Git. We will be talking about the different types of resets in git. It will be a small how-to guide on using the git reset command. We can either fully roll back the changes, or we can simply discard the latest commit(s).

Let us jump to it right away.


You can follow me on social media via @coderstea on TwitterLinkedinFacebook, or Instagram. We also share high-quality videos about programming on our Youtube channel. You can also publish your own post on CodersTea.com, just share your thought on Contact Us or let us know in the comments.


When To Use Git Reset

Git reset is used for discarding your changes. You may want to do this for multiple reasons. One can be you don’t need those changes anymore or there was conflict and you did some mistake while resolving it, don’t worry To err is human.

How to use Git Reset Command

Git reset is very easy to use. You only need to type git reset --<type of reset>. There are multiple types of git reset such as, hard, mixed, and soft. We will look at them one by one. But to be honest, I mostly use soft reset and sometimes hard reset. I will tell you why in a bit.

Commit ID or HEAD

There is another argumanet we can pass to the command, which is the commit id. This commit id is the point until when you want to discard the changes. You need to pass the hash of the commit. Commonly, the mistake is done in the latest commit, right? So you can simply use the HEAD. Where HEAD is shortcode for the latest commit id. It will always point to the latest commit. See the following example.

$ git log
commit 231a (HEAD -> master)
my 3rd commit
commit a2ad
my second commit

commit ae93
my first commit

Now, what if you want to reset the commit before the latest commit. In our case, we want to reset to my second commit. Then we can use the HEAD~1. The sign ~ represents the before, so HEAD~1 can be read as the 1st commit before HEAD. Similarly, we can point to the my first commit via HEAD~2.

Soft Git Reset

This is the most common and safest git reset argument. The syntax is git reset --soft <commit id>. What it does is that it resets the commit to your desired commit, but it does not delete changes made after the destination commit. It simply moves them to the staging area, how convenient.

For example, I have a file with 3 commits as shown in earlier example. I chanegs the file 3 times with the same content as commit message. Now, my final file will be like the following after 3 comits.

my first commit
my second commit
my third commit

And my git log looks like the following ( I reduced the size of the commit ids for readability).

$ git log
commit 231a (HEAD -> master)
my 3rd commit
commit a2ad
my second commit

commit ae93
my first commit

Now, if I do soft reset to the first commit with the following command, my log will contain only the first commit, marked as HEAD. But the content would be the same as the 3rd commit.

The Commnd:

git reset --soft HEAD~2
OR
git reset --soft ae93

The log:

$ git log
commit ae93
my first commit

The content of the file after soft reset in staging area.

my first commit
my second commit
my third commit

Mixed Git Reset: The Default One

the --mixed argument is similar to --soft on, except the changes are not staged. In the soft git reset, the changes made after the destination commit are kept in the staging area. But in the mixed reset, which is the default argument if you don’t pass any, make these changes to the working directory.

Hard Git Reset

Hard git reset is the opposite of hard and mixed one. It actually deletes the data. So in our previous example, the content of the file after the reset would only contain the data from my first commit. To use it we need to pass the parameter as --hard.

Git hard reset command for the same example we used earlier would be like this.

git reset --hard HEAD~2
OR
git reset --hard ae93

The log will contain the the first commit only for HEAD~2 of 3 commit branch.

$ git log
commit ae93
my first commit

And the content of the file will only contain the first commit, discarding all the changes came after that.

my first commit

One of the most common things we developers do is play around with the code. But after playing we need to clean the playground i.e. your code. So every time you need your code in its prime and final form just use the hard reset command without any commit id. And it will be reset to the head commit.

Conclusion

That’s it for this post. I just wanted to show you some different ways of resetting changes in Git. Git is a very powerful tool to make the developer’s life very easy. And it’s a must for any developer. I hope you got the idea about how soft, mixed or hard reset works in git. I will be writing a post on the GitLab review request using git and also how to create the GitLab pipeline very soon. So please subscribe to Coderstea.com.

See you in the next post. HAKUNA MATATA!!!

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Martin
Martin
10 days ago

What if I want to reuse these deleted changes later? I mean I want to delete it now but I need to undo these delete after I am done with the changes?

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy
2
0
Would love your thoughts, please comment.x
()
x