How to Undo Pushed Commits Using Git

Avatar

By squashlabs, Last Updated: October 27, 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 Find The Original URL of a Local Git Repository

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

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.

Related Article: How to Git Pull from a Specific Branch

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

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. Additionally, there... read more

How to Login to a Git Remote Repository

Logging in to Git is an essential skill for any software developer. This article provides a step-by-step guide on how to navigate the login process, from installing Git... read more

How to Create a Tag in a GitHub Repository

Creating a tag in a GitHub repository is an essential step in code versioning. This step-by-step guide will walk you through the process, from cloning the repository to... read more

How to Clone a Git Repository Into a Specific Directory

Cloning a Git repository into a specific directory is a fundamental skill for software developers. This step-by-step guide provides two methods for accomplishing this... read more

How to Undo/Revert a Git Commit Before Push

When working with Git, it's important to know how to undo a commit before pushing it to the repository. This article provides a simple guide on removing a Git commit... read more

How to Revert a Pushed Merge Commit in Git

Reverting a pushed merge commit in Git can be a daunting task, but with the right approach, it can be done efficiently. In this article, we provide a step-by-step guide... read more