How to Undo Pushed Commits in Git With Reset and Revert

Ankit Raj Goyal Feb 02, 2024
  1. Undo Pushed Commits With the git reset Command
  2. Undo Pushed Commits With the git revert Command
  3. Undo Pushed Commits With the git checkout Command
How to Undo Pushed Commits in Git With Reset and Revert

We show three methods to undo pushed commits from a remote repository in Git. We use the git reset, revert , and checkout commands for this.

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

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

Undo Pushed Commits With the git reset Command

We create a undo_pushed_commits_local repository and fill it with a few 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 undo-pushed-commits-remote repository on GitHub.

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

git push undo-remote master

Create alias for remote repo

Push to Remote Repo

Our remote repository now has two commits.

Remote repository has two commits

Now, let us make the 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"

Make Bad Commit

We push this commit to the remote repo.

git push undo-remote

Push the Bad Commit

Our remote has the bad commit:

The Remote has the bad Commit

We now reset to the last good commit in our local repo. We use the --hard option to remove any trace of the bad commits from the commit history.

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

Reset to Last Good Commit

We push the local to the remote repository with the -f option to force the push.

git push -f undo-remote

The Bad Commit is Removed From the Remote Repo

This method works best with private repositories or repositories with small teams.

Large teams might face issues if some developers pull the remote to their local environment after pushing the bad commits before we reset to a good state. They have no commits history with information of the bad commits to correct their repositories.

Undo Pushed Commits With the git revert Command

We have here pushed four bad commits to the remote repo.

4 Bad Commits Pushed to the Remote

We can use revert to undo either a single bad commit or a range of bad commits.

revert makes a new commit that reverses the unwanted commit. Both the original and reversed commits stay in the repository history.

Undo a Single Pushed Commit With revert

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

A new Commit That Reverses the Old Bad Commit

We now push this change into the remote repo. Remember to use the -f flag to make sure no conflicts arise.

git push -f undo-remote

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

The new Revert Commit in the Remote Repo

Undo a Range of Pushed Commits With revert

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 the commits in the range provided (Not including the oldest commit, but this may also depend on the version/platform you use.)

The Commits in the Range Provided Are Reversed With New Commits

We now push the changes to the remote with the -f flag.

git push -f undo-remote

The Remote Repo Has the Revert Commits

This method is great for public repositories with large teams working on them. As both the bad and the undo commits remain in history, developers can keep their local repositories updated with all the bad pushes/reverts.

Undo Pushed Commits With the git checkout Command

This technique is a quick hack that forks a new branch without the bad commits. It is great for small codebases and if you are short on time.

We first checkout to the last good commit.

git checkout <SHA of last known good commit>

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

We here now fork a new branch from this state.

git checkout -b branch_without_badcommits

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

git push -f undo-remote branch_without_badcommits

Make and Push a new Branch Without the Bad Commits

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

The Remote Repo has a new Branch Without the Bad Commits

Related Article - Git Undo