JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Revert to a previous commit in a Git repository

Author:JIYIK Last Updated:2025/04/01 Views:

In this article, we will learn how to revert to a previous commit in a Git repository.

Git is a version control system used in collaborative development environments to track changes made to files.

Git is used to capture snapshots of file changes in a project directory and associate them with commits.

In Git, commits allow you to navigate and view the history of changes made to files.

We can also use Git to reset or revert the files of a project directory in a Git repository to a previous commit (ie) the state of the files in the directory at the time the commit was created.

We will now illustrate this with an example.


git resetReset to a previous commit in the Git repository using

In a collaborative development environment, we use Git to track changes made to files in a project directory in a Git repository.

When we create a commit to save our work, Git creates a unique ID (also called a commit SHAor 哈希) that allows us to record the specific changes that were committed, as well as who made the commit and when.

A commit is an individual change to a file (or set of files). A commit usually contains a commit message, which is a brief description of the changes made.

Sometimes, we may need to restore or reset the repository of our project directory to a previous commit.

Assume that we have the following commits, as shown by the command in our repository git log.

$ git log --oneline
e4cd6b4 (HEAD -> main, origin/main) my change
99541ed second change
41f1f2a first change
...

Now, suppose we want to reset the repository to 41f1f2aa previous commit given by the SHA, with a comment first change.

One way is git checkoutto temporarily switch to the previous commit using the command.

Therefore, we will do the following.

$ git checkout 41f1f2a

We can also create a new branch using the previous commit so that we can commit new changes in that branch.

Therefore, we will do the following.

$ git checkout -b first-change-branch 41f1f2a

If, instead, we want to discard the changes since the last commit, we will use git resetthe command.

The syntax for the command to reset the repository back to a previous commit git resetis git reset -hard <commit-sha-id>.

So in our case we will do the following.

$ git reset --hard 41f1f2a

Please note that this should be used with caution; this will also discard any local modifications. Any uncommitted changes will be lost.

Alternatively, we can store the changes before resetting as shown below.

$ git stash
$ git reset --hard 41f1f2a
$ git stash pop

After executing the above command, the local changes are saved in stash; then, after resetting to the last commit, these changes are reapplied to the repository.


git revertRevert to a previous commit in a Git repository using

When we want to preserve the history of the repository, we use git revertthe command.

After executing the command git revert, Git will create a commit with the reverse patch to invalidate the previous commit. This way, we will not rewrite any history.

The syntax for the command to revert a repository to a previous commit git revertis git reset <commit-sha-id1> <commit-sha-id2> ....

Therefore, we want to revert the first two commits to restore the repository to 41f1f2athe commit given by the SHA.

$ git revert e4cd6b4 99541ed

It will restore the repository with the given two commits.

We can also execute git revertthe command as follows.

$ git revert HEAD~2..HEAD

The above git revertcommand will revert the last two commits.

Finally, as mentioned before, git revertthe command creates a commit that cancels the previous commit. Therefore, we now need to save this commit.

We need to do as follows.

$ git commit -m "reverted commits e4cd6b4 99541ed"

So now the reverted commit is now saved as a new commit in the repository.

In some cases, there is a merge commit and we might want to revert that as well.

We can use the command with the -p option -m parent-number; git revertthis option specifies the parent number of the mainline (starting from 1) and allows revert to reverse the changes relative to the specified parent.

Merge commits have multiple parents. git revertThe command needs additional information to decide which parent of the merge should be considered the mainline.

The syntax of the command is , git revert -m 1 <commit-sha-id>, 1which is used as the first parent of the main line.

So, assuming that commit SHA e4cd6b4is a merge commit. Then we can proceed as follows.

$ git revert -m 1 e4cd6b4

Therefore, we explain in detail how to revert to a previous commit in your repository in Git.

For more information, visit below.

  1. git-reset
  2. git-revert

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Moving commits to another branch in Git

Publish Date:2025/04/01 Views:200 Category:Git

Git is a very useful and powerful tool in the modern software world. Many types of files and codes can be stored through branches and commits in Git. Branches are a different concept depending on the version control system you use. Many dev

Git push using SSH keys

Publish Date:2025/04/01 Views:94 Category:Git

SSH stands for Secure Shell. It is the key that provides us with the credentials to access the SSH network protocol. It provides access to remote servers between engines on an unsecured open network. It is used for transferring data, files,

Delete commits but keep changes in Git

Publish Date:2025/04/01 Views:179 Category:Git

This article outlines the steps necessary to undo a Git commit while preserving the changes introduced by the same commit. We'll cover two commands we can use that have the same effect. Without further ado, let’s jump right in. Remove com

Different ways to commit untracked files in Git

Publish Date:2025/04/01 Views:198 Category:Git

This article discusses the different methods we can use to commit untracked files in Git. If you introduce new files in your project in Git, these files will fall under the category of untracked files. With respect to the Git version contro

Git add all but one file to commit

Publish Date:2025/04/01 Views:73 Category:Git

This article explains how to add all files to commit while excluding selected files. This comes in handy when you have many files to include in a commit and must leave out one file. Instead of adding files one at a time, you can follow thes

Git exits the commit message editor

Publish Date:2025/04/01 Views:91 Category:Git

This article outlines the steps to exit the commit message editor in Git. When you merge or make a commit in Git, the console prompts you to provide a commit message that briefly describes the new commit. Git opens your default text editor,

git add, git commit and git push are combined into one command

Publish Date:2025/04/01 Views:142 Category:Git

This article discussed two methods that you can use to add, commit, and push files to a remote repository with a single command. When making small changes to a single file, you still need to follow the three-stage process of publishing chan

Git Add and Git Commit merged into one command

Publish Date:2025/04/01 Views:184 Category:Git

This article discusses combining the git add and git commit commands into one command line. Combining the two commands into one can save you time. When combining these two commands, you have to keep in mind what you are committing to. Let's

Git list commits

Publish Date:2025/04/01 Views:66 Category:Git

Git is the most common, free and open source distributed version control system. It has repositories that play an important role in the Git world. As a result of this feature of Git, repositories hold a huge significance in the life of deve

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial