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

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial