Difference between Checking out/revering/resetting

Learn via video courses
Topics Covered

Overview

When using Git, there may be times when you want to reverse the changes made to your local repository or to your workspace, or you may want to erase the commits made locally or remotely, or both. Then in these situations, the Git reset, Git checkout, and the Git revert are the 3 methods we can use to undo the changes made to our repository. In reality, you can edit commits or files using the first two commands. It's critical to understand how these commands differ from one another and which one to use when in order to avoid any confusion. We'll go through each of these three commands in detail in this article.

Introduction

The Git reset, Git checkout, and Git revert commands are the most helpful tools in your daily Git workflow. The first two commands can be used to manipulate either the commits or the content in the specific files, and all commands allow you to undo some type of change in your repository.

Because they are so similar, it can be difficult to know which command to execute in a given development environment. The most popular use for Git reset, Git checkout, and Git revert command are contrasted in this article.

To better understand the differences between these three commands, let's look at the three-state management mechanisms of a Git repository. Those three state management mechanisms include:

  • The local working directory
  • The snapshot of the staged environment
  • The commit history of the local repository

main component of git repository

The following section summarizes the examples of the most common use cases for all three of these commands.

Use of Git Checkout

This Git checkout command checks out the content from the repository and inserts it into your working directory. It may have other effects depending on how the command was invoked. For example, you can switch/change the current branch you are working on using the Git checkout command. The thing to note here is that this command does not change the history of commits in your Git repository.

The Git checkout command operates upon three different entities which are branches, files, and commits.

Note: The use of this command can be dangerous as it has no way to undo the operation.

We can perform many operations by Git checkout command as we can create a new branch, we can switch to a specific branch, we can check out to a remote branch, and many more. You can also use the checkout command along with the Git branch command.

Let's see a few commands using the checkout operation:

  1. The command used to switch to another branch:
    Here, <branch_name is the name of the branch on which you want to check out.
  2. You can use the below command to create a new branch and right away switch to it:
  3. Command used to revert back the unstaged changes in Git:

Example of Git Checkout

Let's see how you can use the checkout command when you want to discard the changes in your working directory. Let's create an example Git repository with an empty index.cpp file.

initial commit history of master branch

We have a single commit and there is nothing written in the index.cpp file. Now, let's add some code to the file. If you make this change, you must add the file to the staging area and commit it. These updates are now in your workspace. To check them, you can use the Git status command:

status of repo

Now, we have a change Hello World! which is untracked in our working repository and we need to revert this change. The command used to checkout the file is:

content in file index

Git checkout is used to discard the unstaged changes in the working repository.

When we write the Git checkout command and see the status of our Git repository and also the index.cpp file, we can see that our changes are being discarded from the working directory and we are back again to the empty file that we had before.

file checkout command

We can see our file is now empty and does not have any code in it.

empty file index

We can also use the checkout command to switch between the branches, let's say we have two branches in our local repository, master and dummy and we are currently on a master branch.

branches in git repository

To checkout to a dummy branch, we can use the command:

branch checkout command in Git

Now, we can see a branch has been switched and now we are on a dummy branch.

Explanation of the Output

The output of the command git checkout file name> shows that our changes are being removed from the working directory and we have returned to the empty file as we had earlier. You can also see that using the checkout command, we can also switch between the branches.

Use of Git Revert

Revert is the Git phrase for undoing some changes in our local repository. The revert process is carried out using the Git revert command. This command works in a undo style. It will make a new change having the opposite effect, by undoing the given commit.

This method does not remove any data.

Additionally, we can assert that Git revert adds new modifications that are exactly the reverse of the commits that have already been performed. We can run the below command to revert the changes:

Git offers a convenient mechanism to roll back commits, which is typically used when things don't go as planned or if you want to make extra modifications to the pushed commit. This is especially helpful when tracking down a bug and learning that a single change is what caused it. You can use the Git revert command to perform all of this automatically rather than manually repairing it and producing a new snapshot/commit.

In Git, the revert command generates a new commit that undoes the modifications made in the target commit (the commit which we want to revert).

By entering the commit id of the commit you want to undo, it can be used to roll back any commit.

where commit id is a special identifier assigned to each commit at that time that is produced automatically. An encrypted number created with the Secure Hash Algorithm is a commit ID (also known as SHA). To view the history of commits for a specific repository, use the Git log command.

Let's look at the random example of some logs to see how to use the Git log command to retrieve the commit id:

The output of the above command will return the list of commits in the history of your Git repository as shown below:

The alphanumeric character at the beginning of these sentences stands in for the commit id for a certain commit.

use of revert command in git

The above Git revert command is used to revert and roll back a specific commit by its commit id. You can also use HEAD for the last commit in history to revert the last commit specifically:

The way the Git revert command operates by producing a new commit that includes the modifications made as a result of going back in time. This is due to the fact that rolling back truly undoes the committed change. You need to recommit your changes to push those reverted changes to the remote repository.

When you run the Git revert command, Git automatically opens a text editor to modify and apply your changes. Here, you will find the details of the commit you are trying to revert to, and you can give your new commit message for an existing commit here, and then you can save and close a file.

text editor to apply changes

Once the commit message has been processed, you will see a message containing the new commit id.

This action creates a new commit based on the commit specified in the revert command. This new commit is shown in the logs of Git, showing that a new commit was issued for rolling back the changes of the last commit. It shows the complete history of commits instead of pretending it never happened. The Git log will have all the history of commits. So, if you want to remove the commits from the history and do not want to show the commit created for reverting the changes, then the revert command is not a good choice for you, but if you want to maintain the history of committed changes, then revert is the suitable command for you instead of the reset command.

You can also edit the commit message directly and the command used to revert a specific commit while editing the commit message is:

Let's summarize some of the commands using the revert method in Git:

  1. Command used to revert or undo the operation and changes done in Git:
  2. Command used to revert and roll back a particular commit with its commit id:
    It works by creating a new commit that is the exact opposite of the work done in the given commit.
  3. To revert back a particular commit along with editing the commit message, you can use the:
  4. Command used to revert the latest commit:

This command creates a new commit that undoes the changes from a previous commit. This command adds a new history to the project on top of the existing commit history and it does not modify the existing commit history.

Example of Git Revert

In our dummy branch, we have created a few more files and the current history of the branch looks like this:

commit history of dummy branch

Now, we can undo the commit of adding the index3.cpp file.

git revert command

Now, the current commit history after reverting the commit looks like this for the dummy branch:

reverting commit history of dummy branch

Explanation of the Output

From the above output, we can see that the Git revert command removes the commit that we have asked but it adds one more commit which shows that the revert operation has been done on that specific commit (which we reverted).

Note: You can open the message in your preferred text editor for Git and commit it there. Alternatively, it may take you directly take you to your default editor. Because Git reverts creates a new commit with the removed changes from the original commit rather than deleting the original commit, so its asks you for the new commit message.

Use of Git Reset

The term reset refers to undoing the changes. The Git reset command is used to roll back the changes. The Git reset command has three main options available for resetting the commit and the changes in our local repository. These options are:

  • Soft
  • Mixed
  • Hard

In Git terms, Git reset is a tool that resets the current reference state to a new specified state.

components of git workflow

Git enables us to reset a certain commit when we need to. We can go back to a certain commit. Any parameter that is provided by the reset command may be used in conjunction with the Git reset command to reset it. It will use the command's default action (mixed option is the default) and reset the specified commit. The syntax for resetting the commit is given below:

Here, the different options can be

  • soft
  • mixed
  • Hard

This command is a little more complicated than the other two commands we saw above as it changes which commit a branch head is currently referring/pointing at and this command may alter the existing commit history by changing the commit that a branch references.

Example of Git Reset

Let's see the examples of using the Git reset command. Suppose you are on a master branch, and currently, you have the changes of the index.cpp file in the staging area, now you can use the Git reset command to remove files from your staging environment and push your changes back to your working directory.

status of the repository

The command used for unstaging the changes of a file is:

Git reset command for file

Now, we can again check the status:

status of the repository

Now, we are back to the working directory where the changes are, but the file is now unstaged. Now there are also some commits that we don’t want to commit and we want to remove them from our local repository. To see how to remove the commit from our local repository, let’s first stage and commit the changes and then remove that commit.

commit history of master branch

We have 2 commits now, with the latest updated index.cpp file commits which we are going to remove. The command used to reset the commit is:

Here, HEAD~1 means that we are going to remove the topmost commit or the latest commit that we have done.

Note: You cannot use the Git reset command to remove specific commits like the way we did in the git revert command by providing the commit id of the commit. For example, you can't say that you want to delete the second commit or the third commit. You can only say that you want to delete the last one commit or the last two commits, specified by the number n in the HEAD~n command.

git reset command to remove topmost commit

Using the above command, we can see that the commit has been removed and the files have been unstaged again and moved back to the working directory. So the log doesn't show the latest commits.

commit history of the master branch

There are several ways to actually make changes permanent with the Git reset command.

  1. The command git reset –soft HEAD~1 will erase the commit, but it won't unstage a file. Still, the staging area would have your modifications.
  2. In the example above, we used the default command, git reset –mixed HEAD~1 or git reset HEAD~1 which removes the commit and unstages the file while still storing our modifications in the working directory.
  3. git reset -hard HEAD1 - This command wipes your working directory and cleans both the changes and the commit. Because we wouldn't be able to undo the modifications, this command is also known as a destructive command and should be carefully used.

Some of the points to note while using the Git reset command:

  • Git reset command can be used if our commits are not published to the remote repository.
  • You can use the Git reset command for removing the commits present on your local repository and not in the remote repository.
  • You cannot use the Git reset command to remove specific commits. For example, you can't say that you want to delete the second commit or the third commit. You can only say that you want to delete the last commit or the last two commits, specified by the number n in the HEAD~n command.

Explanation of the Output

As we saw in the output above, the Git reset command removes the commit as well as unstages the file, but our changes will still be stored in the local repository if we are using the default option of mixed resetting.

We can see from the below images that the file has been unstaged and the commit is no longer seen in the logs:

status of the repository

history of the master branch

We also saw in the above example that we can reset a file in Git using the git reset HEAD <name_of_file> command, which will bring the file back to the local repository, where our changes are present but the file will be unstaged now.

Difference between Git Revert, Checkout, Reset

Git RevertGit CheckoutGit Reset
Git revert command removes the commits from the remote repository.Git checkout command removes the modifications from the local repository.Git reset command unstages a file and returns our changes to the working directory (it will also depend on the option used for resetting in the command).
This is used for undoing the changes in the remote repository.This is used for undoing the changes in the local repository.This is used for undoing the changes in the local repository
This command works by adding a new commit to the existing commit history.This command does not make any changes to the commit history.This command alters the existing commit history.
This command rollbacks the modifications we've committed and modifies the current commit history.This command points to the HEAD pointer of a particular commit.This command removes the pending modifications.
This command does not alter the changes in your files or commits.This command can be used to edit files or commits.It is possible to alter files or commits using this command.

Conclusion

  • If you've changed a file in your local repo, but haven't committed your changes, then, you can use the Git checkout command to check out a new copy of the file from the repository.
  • If you made a commit but didn't share it with anyone and you decide to don't do it, then, you can use Git reset to rewrite/override the history and make it look like you never made that commit.
  • If a commit was made somewhere in the project's history and you later decide that the commit was wrong and shouldn't have been made, Git revert is the right tool for this situation as it can pick a specific commit and revert it.
  • It will undo the changes introduced by the wrong commit and will remove it.
  • Git reset command is used to undo the changes in your local repo that haven't been committed yet.
  • Git checkout command is used to copy a file from some other commits to your current working directory. It will not automatically commit the file.
  • These three commands are different, but they all provide the same functionality of undoing the changes in your Git repository.
  • The git reset –hard HEAD~1 command removes the commits and changes from your working directory. This command is sometimes called a destructive command because those changes cannot be restored. So you should be careful while using this reset command with the --hard option.
  • The git reset HEAD~1 command is the same command as the git reset –mixed HEAD~1 command and is used to uncommit and unstage the files and save the changes to your working directory.
  • The command git reset –soft HEAD~1 command will remove the commits but not the files and the changes will still be in the staging area.
  • Git revert should be used to undo the changes on a public branch to maintain the commit history for other team members working on the same project, and Git reset should be used on the private branch for undoing the changes.
  • The Git revert command with commit id is used to revert and roll back a specific commit identified by its commit id. You can also use HEAD to revert the last commit specifically.
  • The --soft option in the Git reset command means don't lose the changes that may not have been committed.
  • If you want to remove all the uncommitted and untracked changes, you can use the --hard option to revert to the last commit.
  • If you want to undo the last Git commit and keep the changes in your working directory, you should use the Git reset command with the –mixed option.