How to Undo Pushed Commits Using Git

Avatar

By squashlabs, Last Updated: Oct. 28, 2023

How to Undo Pushed Commits Using Git

Option 1: Revert the commit

To undo a pushed commit in Git, you can use the "git revert" command. This command creates a new commit that undoes the changes made by the specified commit. Here's how you can do it:

1. Identify the commit you want to revert. You can use the "git log" command to view the commit history and find the commit hash.

   $ git log

2. Once you have the commit hash, use the "git revert" command followed by the commit hash to create a new revert commit.

   $ git revert <commit-hash>

This will open the default text editor for you to enter a commit message. Make sure to save and close the file to create the revert commit.

3. After creating the revert commit, Git will automatically update your branch with the new commit that undoes the changes made by the original commit.

   $ git push origin <branch-name>

This will push the revert commit to the remote repository, effectively undoing the changes made by the original commit.

Related Article: How to Make Git Stop Tracking a File in .Gitignore

Option 2: Reset the branch

Another way to undo a pushed commit is by using the "git reset" command. This command allows you to move the branch pointer to a previous commit, effectively discarding the commits that came after it. Here's how you can do it:

1. Identify the commit you want to reset to. You can use the "git log" command to view the commit history and find the commit hash.

   $ git log

2. Once you have the commit hash, use the "git reset" command followed by the commit hash and the "--hard" option to reset the branch and discard the commits that came after it.

   $ git reset --hard <commit-hash>

Caution: This command permanently discards commits, so make sure you have a backup if you need to recover them later.

3. After resetting the branch, you will need to force push the changes to the remote repository to update it with the new branch history.

   $ git push origin -f <branch-name>

Note: Force pushing can be dangerous, as it overwrites the remote branch with your local branch. Make sure you communicate with your team and ensure that no one else is working on the branch before force pushing.

Best Practices

Related Article: How to Merge One Local Branch Into Another in Git

When undoing pushed commits in Git, it's important to keep a few best practices in mind:

1. Communicate with your team: Before making any changes to a shared branch, communicate with your team to ensure that no one else is working on the branch or relying on the commits you plan to undo.

2. Create a backup: If you're uncertain about the changes you're about to make, create a backup of your branch before proceeding. This can be useful if you need to recover the discarded commits later.

3. Use "git revert" for public branches: If you're working on a public branch that other developers are relying on, it's generally recommended to use "git revert" instead of "git reset". This allows you to undo the changes while preserving the commit history and avoiding conflicts with other developers' work.

4. Use "git reset" for private branches: If you're working on a private branch and want to completely discard the commits, "git reset" can be a more straightforward approach. However, be cautious as it permanently discards commits and can cause data loss if not used correctly.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How To Cherry Pick A Commit With Git

Cherry picking a commit with Git allows you to selectively apply changes to your codebase. In this article, you will learn the meaning and process of… read more

How To Delete A Commit From A Branch

Deleting a commit from a branch in Git can be done using simple steps and commands. There are two methods you can use: git revert and git reset. But … read more

How to Merge Multiple Commits as Single Squashed Commit

Combining multiple commits into a single squashed commit can help streamline your Git workflow. This article provides a step-by-step guide on using G… read more

How to Force Overwrite During Git Merge

This article provides a step-by-step guide on how to force overwrite during a Git merge operation. It covers two methods: using the --strategy-option… read more

How to Stash Untracked Files in Git

Git stash is a powerful tool that allows you to store untracked files in your Git repository. With just a few simple commands, you can keep your repo… read more

How to Discard All Local Changes in a Git Project

Guide on reverting all local changes in a Git-managed project to its previous state. This article provides step-by-step instructions on how to discar… read more

How to Throw Away Local Commits in Git

Removing local commits in Git can be a simple process with the right methods. This article provides two methods, using git reset and git revert, to h… read more

How To Rename A Local Git Branch

Learn how to easily rename a local Git branch in a few simple steps. In this article, we will guide you through the process of renaming a local Git b… read more

How to Fix Git Permission Denied Publickey Error

Simple steps to rectify the Git error: permission denied (publickey). Check SSH Key Configuration, Verify SSH Connection, Contact Git Hosting Provide… read more

How to Rename Both Local and Remote Git Branch Names

Renaming Git branch names locally and remotely can be done with ease using a few simple methods. This guide provides step-by-step instructions on how… read more