DEV Community

Cover image for Git Checkout / Reset / Revert. When to use what?
5 Mins Learn
5 Mins Learn

Posted on • Edited on • Originally published at Medium

Git Checkout / Reset / Revert. When to use what?

We often come across some weird scenarios (especially my team) while committing our code changes. Some of them are,

  • How can I bring the file to working area, which I have added to the staging area by mistake?

  • OMG! I made a commit with production keys in the code but did not push the code yet. Is it possible to delete that commit? (Meanwhile, my tech lead be like 😡)

  • Can anyone say how to remove the changes which I’ve pushed already?

  • I logged onto Production machine and made a change to a file and fixed the production issue immediately 👏 (Can also be debugging on production 😜). Now, I want to remove that change using git cli for the CI/CD to pass. Any Git commands available for this purpose?

Head to the bottom of this page, if you need blind answers for these scenarios. For those who need to understand more, continue from here.

Let’s find answers to all of your such queries with just 3 commands.

  1. Checkout

  2. Reset

  3. Revert

What is Git Checkout?

git checkout removes your changes which are in the working directory of your project. Remember, neither staged nor committed changes will be removed.

I have added some random text on File1 and File2. We can see the changes with git status and git diff commands.

Changes made to files

Let’s say we need to remove all the changes from File1 file and bring it back to the original state. Let’s run git checkout command on File1 and see the changes.

Git Checkout a File

“Okay. That’s cool. Removing changes from 1 file is fine. How can I remove all the changes made in all files in our repo? ”, Naras from my team raised the question.

Oh! That’s simple. Just add a . (dot) after checkout command to remove changes from all files in our repo.

Git released restore command in it’s recent release as an alternative to checkout command

What is Git Reset?

git reset command moves the changes from staging area to working area.

I made changes to File2 and staged it.

Staged File2.txt

Running git reset on File2 will bring the changes to the working directory

Git reset on File2.txt

reset is a powerful command in git which can perform unbelievable actions based on the parameters passed. One of the powerful feature is it’s ability to remove a commit from repository provided some conditions.

Let’s explore them below

Types of Git Reset

Git Reset is divided into 3 types. They are,

  1. Mixed (Default)

  2. Soft

  3. Hard

Mixed Reset

The Mixed reset command brings back the committed changes to the working directory phase.

The syntax of mixed reset command is,

git reset HEAD~n
Enter fullscreen mode Exit fullscreen mode

n represents the number of commits to be removed (from the end).

Let’s remove the last 1 commit from our code.

Git Mixed Reset

As I ran git reset HEAD~1 , the last one commit on the repo has been removed and those changes are brought to the working directory.

Soft Reset

The Soft reset command is similar to mixed reset. This command brings back the committed changes to the staging area.

The syntax of soft reset command is,

git reset HEAD~n --soft
Enter fullscreen mode Exit fullscreen mode

n represents the number of commits to be removed (from the end).

Let’s remove the last 1 commit from our code.

Git Soft Reset

As I ran git reset HEAD~1 --soft, the last one commit on the repo has been removed and those changes are brought to the staging area.

Hard Reset

The Hard reset command is bit dangerous command. This command deletes the commits from git.

The syntax of hard reset command is,

git reset HEAD~n --hard
Enter fullscreen mode Exit fullscreen mode

n represents the number of commits to be deleted (from the end).

Let’s remove the last 1 commit from our code.

Git Hard Reset

As I ran git reset HEAD~1 --hard, the last one commit on the repo has been deleted completely from the repo.

“Why are stressing the word dangerous here. How’s this command so dangerous when compared with other reset commands? ”, intelligent Kumar interrupted with this question.

Well. That’s an interesting question.

The reason I’m stressing that it’s so dangerous is that, this commit has the power to delete all our changes. Remember this does not bring back the changes to either staging or working area. Running this command does not ask for any confirmation from the user.

Think of a scenario where you made a small mistake by adding a “0” next to “~1” in the above command and hit “Enter” without looking and confirming the command. So, the command you ran would be,

git reset HEAD~10 --hard

Can you imagine how dangerous it is now? You lost 10 of your most valuable commits. There’s no way to reverse this process unless you pushed the code to server. So, you have to make all your changes from the beginning again.

I could better explain this with a real scenario that happened between me and Aadhithyanath (One of my colleague).

One day Aadhi approached my help to resolve a bug. I saw there were changes in 10+ files (around 14 files). I asked him to commit those changes before proceeding. He compressed all changes together in a single commit. We started fixing the bug. We fixed one scenario where the bug has happened. But, there are other scenarios too we need to handle. We made a commit. While heading to fix other scenarios we found that our last commit containing the fix for 1st scenario was implemented in the wrong way. So, we planned to remove that commit and go ahead with the correct implementation. I ran the following commit,

git reset HEAD~2 --hard

Aadhi suddenly shouted “OH MY GOD!!!!”

“What happened? ”, I asked him.

“You deleted all my changes 😠”, replied Aadhi.

“What are you saying? ”, I was asking him by looking at the terminal what I’ve executed. Do you notice that ~2 ?

Yes. I deleted the last 2 commits. To our bad luck, we deleted the commit which Aadhi has compressed his changes altogether in a single commit. It was 4 hours of his hard work. Adding up to our bad luck, we haven’t pushed that code to the remote repo. This happens often to those who type faster. I’m one among them 😜.

Do you understand how dangerous it is?

That’s the reason, I stressed the word **dangerous **here. Do not execute this command unless you’re 200% sure of what’s will be the result of it.

I remember you told,

One of the powerful feature is it’s ability to remove a commit from repository provided some conditions.

“What are those conditions from the above line?”, asked Udhaya, a developer cum marketer from our team.

Oh! I forgot to mention about that important condition. Thanks for reminding Udhaya.

The only one condition you need to keep in mind is that, reset command should not be run with the commits pushed to the remote repo.

“Why?”, I hope this strikes your mind.

This main purpose of reset command is to make changes to your commit. So, when you make a change on the commit / delete a commit which you already pushed to remote repo, the hash of that commit will change. This will not let you to push your changes further to the remote repo unless you force push it.

Note: Doing a force push in a repo is not a recommended way unless it is an unavoidable case

Because, git pushes the changes to remote by comparing it’s *tip *(tip is called the last / latest commit of the branch in a repo)

What is Git Revert?

Revert command removes the changes made on a commit, by creating a new commit. The new commit will contain the changes that’s the reverse of old commit.

git revert is the preferred command whenever you have to make the changes to the commit which you’ve already pushed.

The syntax of revert command is,

git revert

commit_hash is the hash of the commit which we want to revert.

Git Revert

We have reverted the top commit from our repo and you can see, a new commit has been created above the old commit saying it’s the revert of that commit.

Can you please explain one real time scenario for each of the above concepts?”, asked Krish, one of the leading developer in our firm.

Sure.

  1. You made few changes and messed up with the code and you need the old code back. Make use of git checkout to restore that file

  2. When you make a commit, but you feel that you need to include some more changes in those files in the same commit, use git reset HEAD~1 --mixed to bring the commit to the working directory

  3. When you made a commit, but you need to add few more files to the same commit, use git reset HEAD~1 --soft which will bring all the changes to the staging area

  4. When you made a commit that contains a production key in the code, use git reset HEAD~1 --hard to completely remove that commit

  5. When you made a commit and pushed the code, later you felt that the changes made in that commit are not valid / not required, use git revert to reverse the changes

Raman (little mischievous ) from my team asked me the following,

“Raising a question is simple, but returning the satisfying answer is the toughest job. Can you say the appropriate commands to use for the scenarios you mentioned at beginning?”.

“That’s the evaluation of your understanding folks. Can you all say the command for each scenario?”, I replied.

To my surprise, everyone has voted the right command for each scenario. Felt happy on seeing the success of my teaching in real time. Thank you folks.

Answers for my valuable readers & followers,

  • How can I bring the file to working area, which I have added to the staging area by mistake? — git reset

  • OMG! I made a commit with production keys in the code but did not push the code yet. Is it possible to delete that commit? (Meanwhile, my tech lead be like 😡) — git reset HEAD~1 --hard

  • Can anyone say how to remove the changes which I’ve pushed already? — git revert

  • I logged onto Production machine and made a change to a file and fixed the production issue immediately 👏 (Can also be debugging on production 😜). Now, I want to remove that change using git cli for the CI/CD to pass. Any Git commands available for this purpose? — git checkout

Give a like ❤️ if you like this article. Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.

Top comments (0)