How to Undo/Revert a Git Commit Before Push

Avatar

By squashlabs, Last Updated: October 27, 2023

How to Undo/Revert a Git Commit Before Push

To undo a Git commit before push, you can use the “git reset” command or the “git revert” command. Both commands allow you to undo the last commit, but they have different effects on your Git history.

Using “git reset”

The “git reset” command allows you to move the branch pointer to a previous commit, effectively undoing the last commit. This command is useful if you want to completely remove the last commit from your Git history.

Here are the steps to undo a Git commit before push using “git reset”:

Step 1: Open a terminal and navigate to your Git repository.
Step 2: Run the following command to see the commit history:

git log --oneline

Step 3: Identify the commit hash of the commit you want to undo. The commit hash is a long string of characters that uniquely identifies a commit.
Step 4: Run the following command to undo the last commit:

git reset HEAD~1

This command moves the branch pointer to the commit before the last commit, effectively removing the last commit from your Git history. The “–hard” option can be added to discard the changes made in the last commit.

Step 5: Run the following command to verify that the last commit has been undone:

git log --oneline

You should see that the last commit has been removed from the Git history.

Related Article: How to Delete a Remote Tag in Git

Using “git revert”

The “git revert” command allows you to create a new commit that undoes the changes made in a previous commit. This command is useful if you want to keep a record of the undone commit in your Git history.

Here are the steps to undo a Git commit before push using “git revert”:

Step 1: Open a terminal and navigate to your Git repository.
Step 2: Run the following command to see the commit history:

git log --oneline

Step 3: Identify the commit hash of the commit you want to undo. The commit hash is a long string of characters that uniquely identifies a commit.
Step 4: Run the following command to undo the last commit:

git revert <commit_hash>

Replace “” with the actual commit hash of the commit you want to undo. This command creates a new commit that undoes the changes made in the specified commit.

Step 5: Run the following command to verify that the last commit has been undone:

git log --oneline

You should see a new commit that undoes the changes made in the previous commit.

Best Practices

When undoing a Git commit before push, it’s important to consider the impact on your Git history and the collaboration with other developers. Here are some best practices to follow:

1. Communicate with your team: If you’re working in a team, it’s important to communicate with your team members before undoing a commit. Make sure everyone is aware of the changes you’re making to avoid conflicts and misunderstandings.

2. Use “git revert” for shared branches: If you’re working on a shared branch that other developers are also using, it’s recommended to use the “git revert” command instead of “git reset”. This way, you create a new commit that undoes the changes, allowing other developers to easily understand and collaborate on the changes.

3. Use “git reset” for local branches: If you’re working on a local branch that is not shared with other developers, you can use the “git reset” command to completely remove the last commit from your Git history. This can be useful if you made a mistake in the last commit and want to start over.

4. Be cautious with force pushing: If you have already pushed the commit you want to undo, be cautious when using the “git reset” command with the “–force” option. Force pushing can overwrite the commit history of other developers, potentially causing conflicts and data loss. It’s recommended to avoid force pushing unless you’re sure it won’t impact other developers.

5. Review your changes before pushing: Before pushing your changes to a remote repository, it’s a good practice to review your changes using the “git diff” command. This allows you to double-check your changes and make sure you’re not undoing any important changes unintentionally.

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 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

How To Find The Original URL of a Local Git Repository

In this article, we will guide you on how to find the original URL of a local Git repository. By using the 'git show origin' command or checking the Git Config file, you... read more