How to Revert to Previous Commits Using Git Reset and Revert

Learn how to roll back to previous commits in Git using git reset and git revert commands. Step-by-step guide to undo changes and manage your commit history effectively.

Introduction to Git Commits

In the world of software development, version control is essential to keep track of changes made to a project. Git, an open-source version control system, enables developers to manage different versions of their code through commits. Each commit is a snapshot of the project at a specific point in time, making it easy to roll back to previous commits if needed.

In this guide, we’ll discuss how to roll back to previous commits using two primary Git commands: git reset and git revert. We’ll explore different ways to go back to previous commits and understand when to use each approach.

Before we begin, you will utilize a Git repository to make some commits and set up a suitable environment for the demonstration of the Git commands.

Setting Up a Git Repository

To set up a Git repository, you first need to create a folder on your local machine, inside which you will initialize the Git repository.

Create a folder named demo on your local machine and navigate to it by using the following commands in a terminal:

mkdir demo
cd demo

Then, initialize a Git repository inside the folder using the git init command:

git init

This command sets up the necessary configuration files that track changes in our project.

Next, create a file named test.txt:

touch test.txt

After that, open the file and add the following text into it:

This is the first line.

You will then use the git add command to add the file to the staging area and commit the staged file using the git commit command:

git add test.txt
git commit -m "Initial commit: Set up test.txt with first line"

Note: By using the -m flag with git commit, we provide the commit message inline, avoiding the need for the default text editor.

Now, let’s repeat the process to add two more strings.

Add the following to the test.txt file:

This is the first line.
This is the second line.

Then, add and commit the modified file:

git add test.txt
git commit -m "Second commit: Added second line"

Again, add the following line to the test.txt file:

This is the first line.
This is the second line.
This is the third line.

Add and commit the modified file:

git add test.txt
git commit -m “Third commit: Added third line”

At this point, we have a series of commits, perfect for demonstrating the Git commands that allow us to roll back to previous commits. Let’s see how we can do this.

How to Roll Back to Previous Commits Using Git Reset

The git reset command allows us to undo changes made after a specific commit. The syntax for this command goes like this:

git reset <commit-hash>

Here, the commit-hash is a unique alphanumeric sequence that helps us identify a particular commit. We need to know this for the target commit which we want to roll back to before using the git reset command. Here, the target commit is the commit that comes just before the changes that are set to be removed. Moreover, your commits will likely have different commit hashes than the ones in this example.

We can find the commit hash of the target commit from the repository’s commit history and print it out in the terminal using the git log command with the --oneline flag. The --oneline flag restricts each commit entry to a single line:

git log --oneline
cdf7ba0 (HEAD -> master) Third commit
95419f7 Second commit
ffb6211 First commit

As we can see above, HEAD currently refers to the third commit, which is the most recent commit in the master branch. Suppose we want to reset or move HEAD back to the second commit. In that case, we need to find the commit hash of Second commit, which is 95419f7.

Now, let’s go back to the second commit using the git reset command:

git reset 95419f7

After resetting, if we run the git log --oneline command again, we’ll see the third commit removed from the commit history and HEAD pointing to the second commit:

git log --oneline
95419f7 (HEAD -> master) Second commit
ffb6211 First commit

However, we’ll not see any changes in the test.txt file:

This is the first line.
This is the second line.
This is the third line.

This happens because the git reset command only removes the commits that come after the target commit (the second commit in this case) from the commit history. It doesn’t remove the changes made in those commits. Instead, it takes all these changes and puts them in the unstaged area.

Let’s verify that using the git status command:

git status

Here is the output:

On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")

Next, let’s add and commit the unstaged changes:

git add test.txt
git commit -m “Third Commit: Added third line”

After committing, the commit history looks like this:

git log --oneline
4dd32d9 (HEAD -> master) Third commit
95419f7 Second commit
ffb6211 First commit

If we want to remove the changes as well, we can use the --hard flag with the git reset command. So, let’s use that to go back to the second commit again:

git reset --hard 95419f7

Now, let’s have a look at the commit history:

git log --oneline
95419f7 (HEAD -> master) Second commit
ffb6211 First commit

As expected, the third commit is removed from the commit history and HEAD now points to the second commit.

Then, let’s check out the test.txt file:

This is the first line.
This is the second line.

As we can see above, all the changes made after the second commit in the file have been removed.

How to Roll Back to Previous Commits Using Git Revert

The git revert command is another command that we can use to roll back to previous commits. This command produces a new commit that undoes the changes made in a specific commit, leaving the commit history intact. After undoing the changes, the branch looks exactly like it did after the commit that comes before the reverted commit.

Here is the syntax for git revert:

git revert <commit-hash>

In the command above, commit-hash allows us to identify the commit that we want to revert.

Currently, the commit history in our repository looks like this:

git log --oneline
95419f7 (HEAD -> master) Second commit
ffb6211 First commit

Let’s use the git revert command to revert the second commit in our example repository:

git revert 95419f7

This will launch the default text editor on the machine prompting us for the commit message. Here, we’ll just close the editor to go with the default one.

After this, we’ll find a new commit in the commit history that rolls back the changes in the second commit:

git log --oneline
f45b583 (HEAD -> master) Revert "Second commit"
95419f7 Second commit
ffb6211 First commit

After this process, the text file will look like this:

This is the first line.

The file looks the same as it did after the first commit, which comes before the reverted commit, i.e., the second commit.

Review and Next Steps

In this article, we’ve learned how to roll back to previous commits using git reset and git revert. By understanding these commands, we’ll be able to easily manage our repository’s commit history and roll back to previous commits whenever necessary.

To learn how to set up Git and GitHub, check out this course on Codecademy.

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team