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 authentication

Publish Date:2025/03/28 Views:163 Category:Git

This article demonstrates connecting a local repository to a remote repository on GitHub/Gitlab without getting 身份验证失败 error messages. Creating a local repository from scratch in Git To create a local repository from scratch, fo

Log graphs in Git

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

This article shows you how to use git log the command to graphically view the commit history in Git. Viewing log graphs in Git The command git log displays all the repository history at once snapshots(commits) . This command has a default f

Git refresh remote branch

Publish Date:2025/03/28 Views:93 Category:Git

Git is considered to be the most accurate and the most used software by developers in their projects and can be operated by multiple developers simultaneously. It provides many unique and quirky features to the developers which are very dif

Updating Git on Mac

Publish Date:2025/03/28 Views:181 Category:Git

When working on Git, you should stay updated with the latest version to get its latest features. This article will discuss how to install and update the latest version of Homebrew and Git on your personal computer. Homebrew on Mac Homebrew

Enable Git Tab Auto-Complete

Publish Date:2025/03/28 Views:109 Category:Git

This tutorial demonstrates how to enable git tab autocompletion. Importance of enabling Git Tab auto-completion When developers work with source code, they mostly prefer Git as it is a very familiar and convenient platform for developers th

Restoring a repository in Git

Publish Date:2025/03/28 Views:158 Category:Git

Sometimes while using Git, we come across a situation where we want to pull the latest changes from the remote repository and it conflicts with the existing modifications or files, then we have to push those files to the storage. Git provid

Undo Git Stash Pop conflicts

Publish Date:2025/03/28 Views:178 Category:Git

You can undo this using the solutions in this article git stash pop with merge conflicts . We show you how to abort an erroneous stash pop operation and return to a clean state. But we also demonstrated a git stash pop way to resolve the co

Resolving Git stash conflicts without committing

Publish Date:2025/03/28 Views:72 Category:Git

This article outlines the steps you should follow to resolve Git stash conflicts without reverting or creating commits. For a simpler context, we will simulate a situation where the git stash pop command results in a conflict and try to res

Git Stash needs to be merged

Publish Date:2025/03/28 Views:128 Category:Git

Git is a stylish platform that provides us with many features, one of the main ones is storage. With this unique feature, we can accumulate a lot of unchanged work that we don't want to commit to our repository when the code is checked in;

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial