JIYIK CN >

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

Undoing Pushed Commits in Git with reset and restore

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

We showed three ways to undo commits pushed from a remote repository in Git. To do this, we used the git reset, , revertand checkoutcommands.

When we use git reset, we also remove any traces of the unwanted commit from the repository history. But with git revert, both the original commit and the undo commit remain in the history.

If we use git checkout, we undo the changes in the new branch.


Use git resetthe command to undo the pushed commits

We created a undo_pushed_commits_localrepository and populated it with some healthy (good) commits.

mkdir undo_pushed_commits_local
git init

Then
add/modify files.

git add --all
git commit -m "Make healthy commit"

Add/modify more files.

git add -all
git commit -m "Make another healthy commit"

Make healthy commits

We will now push these commits to the repository on GitHub undo-pushed-commits-remote.

git remote add undo-remote git@github.com:danielturidandy/undo-pushed-commits-remote.git

git push undo-remote master

Create an alias for the remote repository

Push to remote repository

Our remote repository now has two commits.

The remote repository has two commits

Now, let's make a bad commit.

echo "This is a bad addition" >> file1.txt
echo "This is a bad addition" >> file2.txt
git add -all
git commit -m "Make a bad commit"

Making the wrong submission

We push this commit to the remote repository.

git push undo-remote

Pushing the wrong commit

Our remote has the wrong commit:

Remote commit with errors

We are now 重置at the last good commit in our local repository. We use --hardthe -p option to remove any traces of the bad commit from our commit history.

git log
git reset --hard  <SHA of the last good commit>

Reset to last good commit

We use the -f option 推送to force push the local repository to the remote repository.

git push -f undo-remote

Removed the bad commit from the remote repository

This method is best suited for private repositories or repositories for small teams.

Large teams may run into problems if some developers pull remote to local environment after pushing bad commits before we reset to a good state. They have no commit history with 纠正bad commits for their repositories.


Use git revertthe command to undo the pushed commits

Here we push four wrong commits to the remote repository.

4 incorrect commits pushed to remote

We can use revertto undo a single bad commit or a series of bad commits.

revertMake a new commit that reverses the unwanted commit. Both the original commit and the reversed commit are preserved in the repository history.

revertTo undo a single pushed commit, use

git revert <SHA of the commit we want to revert>

New commit that reverses an old bad commit

We will now push this change to the remote repository. Remember to use the -f flag to ensure there are no conflicts.

git push -f undo-remote

Our remote repository now has a new commit that reverses the bad commit.

New Revert Commit in Remote Repository

revertTo undo a series of pushed commits, use

git revert <SHA of the oldest commit to revert>..<SHA of the newest commit to revert>

Revert a range of commits

This will revert all commits in the provided range (excluding the oldest commit, but this may also depend on the version/platform you are using.)

The commits in the provided range are reversed by the new commit

We will now push the changes to the remote using the -f flag.

git push -f undo-remote

The remote repository has a reply commit

This approach is great for public repositories with large teams. Since both bad commits and undo commits are kept in the history, developers can update their local repositories with all bad pushes/reverts.


Use git checkoutthe command to undo the pushed commits

This technique is a quick hack that creates a new branch without the bad commits. It's great for small codebases and if you're pressed for time.

We first checkoutget to the last good commit.

git checkout <SHA of last known good commit>

We then reach the detached HEAD state at the last known good commit in the repository's history.

We now fork a new branch from this state.

git checkout -b branch_without_badcommits

We then push it to the remote into a new branch.

git push -f undo-remote branch_without_badcommits

Create and push a new branch without bad commits

The remote repository now has a new branch without any bad commits.

The remote repository has a new branch with no error commits

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