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

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

Published: Last Updated on 2 comments

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.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


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 argument 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 the shortcode for the latest commit id. It will always point to the latest commit. See the following example.

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

commit ae93
my first commitCode language: HTML, XML (xml)

Now, what if you want to reset the commit before the latest commit? In our case, we want to reset it 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 the earlier example. I changes the file 3 times with the same content as the commit message. Now, my final file will be like the following after 3 commits.

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)
<code>my 3rd commit</code>
commit a2ad
my second commit

commit ae93
my first commitCode language: HTML, XML (xml)

Now, if I do a 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 Command:

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

The log:

$ git log
commit ae93
my first commit
<code class=""></code><code class=""></code>Code language: HTML, XML (xml)

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

my first commit
my second commit
my third commit
<code class=""></code><code class=""></code>Code language: HTML, XML (xml)

Mixed Git Reset: The Default One

the --mixed the 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

A hard git reset is the opposite of a hard and mixed one. It 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 first commit only for HEAD~2 of 3 commit branches.

$ git log
commit ae93
my first commit
<code class=""></code><code class=""></code>Code language: HTML, XML (xml)

And the content of the file will only contain the first commit, discarding all the changes that 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 social handles.

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


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

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