Nick Bull Blog

Nick Bull Blog

World’s Easiest Guide To Git Reset

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.

Image for post

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"

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

What happened?

  1. HEAD is on commit B
Image for post

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

// file.txt

abc

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

What happened?

  1. HEAD is on commit B
Image for post

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

// file.txt

abc

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

What happened?

  1. HEAD is on commit B
Image for post

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

// file.txt

ab

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

Image for post

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 check me on Twitter.

🔴 🔴 Every week, I send out my “3-2-1” newsletter with 3 tech news, 2 articles, 1 advice, and 1 Secret BONUS for you. Join my 3-2-1 Newsletter

#git#github#newbie#developer#software-development
 
Share this