Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 3, 2020 11:35 am GMT

World's Easiest Guide to GitReset

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 inittouch file.txtecho -n "a" >> file.txt// file.txtagit add .git commit -m "A"echo -n "b" >> file.txt// file.txtabgit add .git commit -m "B"echo -n "c" >> file.txt// file.txtabcgit 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

Alt Text

  1. Changes from commit C (added letter c) still in file.txt and has status staged
// file.txtabc
  1. 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

Alt Text

  1. Changes from commit C (added letter c) still in file.txt and has status unstaged
// file.txtabc
  1. 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

Alt Text

  1. Changes from commit C (added letter c) deleted from file.txt
// file.txtab
  1. 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!


Original Link: https://dev.to/bulljsdev/world-s-easiest-guide-to-git-reset-47mi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To