DEV Community

Cover image for World's Easiest Guide to Git Reset
Nick Bull
Nick Bull

Posted on • Updated on • Originally published at Medium

World's Easiest Guide to Git Reset

For some reason, I was always afraid of this command.

git reset

Even though I always knew what git reset does, every time I ran it in the console I was scared that something would go wrong until…

I made a guide to myself on how to use it.

Let me quickly and clearly explain how git reset works.

Git Lifecycle

To understand git reset we need to learn more about the git lifecycle.

When you make some changes, they have 3 statuses:

  1. Unstaged
  2. Staged
  3. Commit

You created a file and wrote abc there, these changes have unstaged status.

Then you do git add myfile command, your changes go to git "index" (place where the files we're going to commit to the git repository are) and status of your changes become staged.

Then you do git commit command and status of your changes become commited.

It's a simplified version of the git lifecycle.

Our setup

We have a master branch with 3 commits: A, B, C.

Alt Text

And file in our working directory file.txt with such history:

git init
touch file.txt

echo -n "a" >> file.txt

// file.txt
a

git add .
git commit -m "A"

echo -n "b" >> file.txt

// file.txt
ab

git add .
git commit -m "B"

echo -n "c" >> file.txt

// file.txt
abc

git add .
git commit -m "C"
Enter fullscreen mode Exit fullscreen mode

Initially, we added the letter a to file.txt and made a commit A.

Then we added the letter b to file.txt and made a commit B.

Then we added the letter c to file.txt and made a commit C.

Ok, let's start.

git reset --soft

We are now on commit C. Let's do git reset.

git reset --soft b
Enter fullscreen mode Exit fullscreen mode

What happened?

1) HEAD is on commit B

Alt Text

2) Changes from commit C (added letter c) still in file.txt and has status staged

// file.txt
abc
Enter fullscreen mode Exit fullscreen mode

3) If you do git commit -m "C" right now, you will get identical commit C.

git reset --mixed

We are now on commit C. Let's do git reset.

git reset --mixed b
Enter fullscreen mode Exit fullscreen mode

What happened?

1) HEAD is on commit B

Alt Text

2) Changes from commit C (added letter c) still in file.txt and has status unstaged

// file.txt
abc
Enter fullscreen mode Exit fullscreen mode

3) If you do git add file.txt then git commit -m "C" right now, you will get identical commit C

The only difference between --soft and --mixed is that changes get different status staged vs unstaged

git reset --hard

We are now on commit C. Let's do git reset.

git reset --hard b
Enter fullscreen mode Exit fullscreen mode

What happened?

1) HEAD is on commit B

Alt Text

2) Changes from commit C (added letter c) deleted from file.txt

// file.txt
ab
Enter fullscreen mode Exit fullscreen mode

3) 🔴 All uncommitted changes will be deleted from your working directory. So if you'll add any other changes to file.txt, don't commit them and do git reset --hard they'll be deleted from the working directory.

Summary

Alt Text

In the end...

I hope now you can understand the difference between different git reset commands and will use it without any fear.

🔴 If you like this article share it with your friends and follow me on Twitter

🔴 Get job interview tips, coding guides, and the latest Frontend insides 👉 Join my Newsletter

That's all. Thanks!

Top comments (1)

Collapse
 
jessekphillips profile image
Jesse Phillips

I think git reset --hard is the only destructive command which does not require --force. Even clean I believe needs force.