JIYIK CN >

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

Rolling back to an old commit in a public Git repository

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

This article explains how we can roll back a public Git repository to an old commit. When using Git version control, we can go back to any desired point.

Without further ado, let’s get into today’s agenda.


Rolling back to an old commit in a public Git repository

As always, we will use an example to explain this concept.

We have a Git repository whose commit history is shown below.

Submission History

At this point in our repository, HEAD is at commit e65841a . We can also refer to it as HEAD@{0}.

If we want to rollback three commits, such that HEAD is at commit 7c5a7dbHEAD@{3} , i.e. , how would we go about doing it?

We could run the git reset command, but this is a public repository. It is not recommended to use the command in a public repository git reset.

You may end up disrupting the timeline of your project.

Remember that git resetthe command will cancel the commits specified on the command line.

A safer and more concise way is to use git checkoutthe command. Here is the simple syntax of the command.

$ git checkout <SHA-1>

In our example, we will run:

$ git checkout 7c5a7db .

Note the dot at the end of the command .. We instruct Git to apply the changes to the entire working tree.

Always run this command at the root of your project; otherwise, it will only take effect in the subdirectory you checked in.

git checkout

This does not remove commits after the specified commit; instead, it updates all affected files on the working tree.

We can try out the code and commit the changes. After committing the changes, let's check our commit history to verify our situation.

Submit history to verify

There you have it. Our commit is still intact, but there is a new commit, the rollback.

After commit, the affected files should be HEAD@{4}identical to those in .

In short, public repositories need to be very careful when rolling back to a specific commit. Avoid using git reset --hardthe command, and use git checkoutthe command instead.

It's safer and cleaner.

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 shows remote tracking branches

Publish Date:2025/04/03 Views:125 Category:Git

Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot

View merged and unmerged branches in Git

Publish Date:2025/04/03 Views:96 Category:Git

This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig

The difference between Fork and Branch on GitHub

Publish Date:2025/04/03 Views:157 Category:Git

This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar

How to determine the current branch in Git

Publish Date:2025/04/03 Views:164 Category:Git

Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams

Difference between Git Merge Origin/Master and Git Pull

Publish Date:2025/04/03 Views:195 Category:Git

This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha

Git Pull Origin branch overwrites Master branch

Publish Date:2025/04/03 Views:143 Category:Git

This article explains how we can revert the changes made to the master git pull origin branch branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories . You pull chan

Git Pull and Merge Commands

Publish Date:2025/04/03 Views:56 Category:Git

In the vast world of version control systems, Git is the only fast, well-organized, in-demand, and easily accessible version control system that enables users to work on various projects simultaneously in an efficient manner without any col

Copy files from another branch in Git

Publish Date:2025/04/03 Views:52 Category:Git

In Git, merging various files can lead to a lot of conflicts. Through these merge conflicts, our files may be compromised, so we have to copy these files or folders from one branch to another to keep them safe. A popular approach is cherry

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial