Git: Delete Commits using CLI

Photo by Yancy Min on Unsplash

Git: Delete Commits using CLI

ยท

3 min read

Intro

With the fancy UIs that we have these days, it's very easy to use git in our day-to-day lives. A few clicks & the job's done.

Let's ask ourselves a question. Do we understand what happens in the background?

For most of us, the honest answer would be NO!!. Also to be really honest, we don't care most of the time.

As long as we're able to complete day-to-day tasks, we don't dive deep into concepts.

But let's try to understand a few common situations in git.

I'll be covering how to delete commits using CLI in this blog.

Prerequisites

Basic understanding of git.

Willingness to understand how git works ๐Ÿ˜‰

Let's get started

To explain the concept, We'll need some boilerplate code. Let's create that code first.

Open any IDE of your choice, create a folder, cd into it & run the command git init

As you all know, this will initialize an empty git repository.

Let's add some random files to our folder.

  1. touch test-file-a.txt

  2. git add test-file-a.txt

  3. git commit -m "added test-file-a.txt"

  4. touch test-file-b.txt

  5. git add test-file-b.txt

  6. git commit -m "added test-file-b.txt"

  7. touch test-file-c.txt

  8. git add test-file-c.txt

  9. git commit -m "added test-file-c.txt"

We've created 3 files in total and added a commit after creating each file.

At this point, we should have 3 commits in git with HEAD pointing to the latest commit.

Let's confirm the same by running the command git log

If all goes well, you should see this in your terminal.

Now I want to delete my last commit i.e. added test-file-c.txt

There can be 2 scenarios here.

1] I want to delete the commit but retain the changes in the staging area.

Run the command git reset --soft HEAD~1

--soft flag retains the changes in the staging area

HEAD~1 essentially means moving the HEAD back by 1 commit.

--soft flag is applied to the reset command by default.

You can run git reset HEAD~1

This is the same as running git reset --soft HEAD~1

Post running this command, you'll see that the commit went away & files in that commit are in the staging area.

You can confirm this by running git log to verify commits & git ls-files to check the staging area.

2] I want to delete the commit & also the changes included in that commit.

Run the command git reset --hard HEAD~1

--hard flag will delete the commit as well as the files in that commit.

Post running this command, you'll see that the commit went away & also the files in that commit.

You can confirm this by running git log to verify commits & git ls-files to check the staging area.

๐Ÿ’ก
Post running git ls-files you'll see files of previous commits in the staging area. Don't panic, this is how git tracks changes

Want to bring your changes back?

Let's say we run the command git reset --hard HEAD~1. Now the commit is gone & so are the changes.

What if you want them back? No worries! It's possible!

Type the command git reflog It will list out all the recent activities in your local.

Now copy the hash of the commit you just deleted & run git reset --hard <paste-copied-hash>

Voila! You have your commit & changes back๐Ÿ˜Ž

๐Ÿ’ก
git reflog will only show the activities up to the last 30 days.

I hope you were able to understand the concept.

Cheers!๐Ÿฅณ

ย