How to Undo Last Commits in Git

Avatar

By squashlabs, Last Updated: August 22, 2023

How to Undo Last Commits in Git

When working with Git, it is common to make mistakes or commit changes that you later realize are incorrect. Fortunately, Git provides several ways to undo the last commits and revert your code to a previous state. In this guide, we will explore different methods to undo the last commits in Git and discuss their use cases.

Method 1: Using git revert

The git revert command allows you to undo one or more specific commits by creating new commits that reverse the changes made in the original commits. This method is useful when you want to keep a record of the changes you are undoing.

To undo the last commit using git revert, follow these steps:

1. Open a terminal or command prompt.

2. Navigate to the repository directory using the cd command.

3. Run the following command to revert the last commit:

git revert HEAD

The HEAD argument refers to the most recent commit. By specifying HEAD, you are telling Git to revert the changes made in the last commit.

4. Git will open your default text editor to create a commit message for the revert commit. Modify the message if needed and save the file.

5. Git will create a new commit that undoes the changes made in the last commit. The commit message will indicate that it is a revert commit.

Note: If there are merge conflicts during the revert process, Git will prompt you to resolve them before creating the revert commit.

6. Verify that the last commit was successfully reverted by checking the commit history.

  git log

The commit history should now show the revert commit as the most recent commit.

Related Article: How to Undo a Git Rebase: A Tutorial

Method 2: Using git reset

The git reset command allows you to move the branch pointer to a specific commit, effectively removing the commits that come after it. This method is useful when you want to completely discard the changes made in the last commit and all subsequent commits.

To undo the last commit using git reset, follow these steps:

1. Open a terminal or command prompt.

2. Navigate to the repository directory using the cd command.

3. Run the following command to reset the branch to the commit before the last commit:

   git reset HEAD~1

The HEAD~1 argument refers to the commit before the most recent commit. By specifying HEAD~1, you are telling Git to move the branch pointer to this commit and remove the last commit and all subsequent commits.

Note: This operation is destructive, and the commits that are removed will be lost. Make sure to create a backup or copy of the commits if needed.

4. Verify that the last commit and subsequent commits were successfully removed by checking the commit history.

   git log

The commit history should now show the previous commit as the most recent commit.

Method 3: Using git cherry-pick

The git cherry-pick command allows you to apply the changes from one or more specific commits to the current branch. This method is useful when you want to selectively undo changes made in specific commits.

To undo the last commit using git cherry-pick, follow these steps:

1. Open a terminal or command prompt.

2. Navigate to the repository directory using the cd command.

3. Run the following command to apply the changes from the commit before the last commit:

   git cherry-pick HEAD~1

The HEAD~1 argument refers to the commit before the most recent commit. By specifying HEAD~1, you are telling Git to apply the changes from this commit to the current branch.

4. Git will apply the changes from the specified commit to the current branch. If there are no conflicts, the changes will be applied successfully. If there are conflicts, Git will prompt you to resolve them.

5. Verify that the changes from the last commit were successfully applied by checking the code and commit history.

   git log

The commit history should now show a new commit with the changes from the specified commit.

Best Practices and Considerations

When undoing commits in Git, it is important to consider the following best practices:

1. Commit regularly: By committing changes frequently, you can easily identify the commits that need to be undone and minimize the impact of undoing commits.

2. Review changes before undoing: Before undoing commits, review the changes carefully to ensure that you are undoing the correct changes and not losing any important work.

3. Communicate with your team: If you are working on a shared repository, communicate with your team members before undoing commits to avoid conflicts and ensure everyone is aware of the changes being made.

4. Backup important commits: If you need to undo commits that contain important work, create a backup or copy of the commits before undoing them. This way, you can restore the commits if needed.

5. Use interactive rebase for more complex scenarios: If you need to undo multiple commits or perform more complex operations, consider using interactive rebase (git rebase -i) to have more control over the commit history.

Remember that undoing commits in Git should be done with caution, especially if you are working on a shared repository or collaborating with other developers. Make sure to communicate and coordinate with your team to avoid conflicts and ensure a smooth workflow.

Related Article: How to Fix a Git Detached Head

Why would you want to undo the last commits?

There are several reasons why you might want to undo the last commits in Git:

1. Accidental commits: You may have accidentally committed changes that are not ready or should not be included in the project history.

2. Incorrect commits: You may have made a mistake in the changes you committed and want to revert them.

3. Unwanted changes: You may have made changes that you later realize are unnecessary or unwanted.

4. Conflict resolution: During a merge or rebase, you may have resolved conflicts incorrectly and want to undo the changes made during the conflict resolution.

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

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 Git's git merge... read more

How to Remove a File From the Latest Git Commit

Learn how to remove a file from the latest Git commit using a simple process. Discover two approaches: interactive rebase and amending the commit. Explore best practices... read more

How to Cherry Pick Multiple Commits in Git

Cherry picking multiple commits in Git can be a useful technique when you only want to apply specific changes to your codebase. This article provides a step-by-step... read more

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 cherry picking, as... read more

How To Combine My Last N Commits In Git

Learn how to combine multiple commits into a single one using Git's squash feature. Discover the potential reasons for combining commits and explore different methods to... read more

How To Change the Git Remote URL

Changing the Git remote URL for a repository is a simple process that can be done with a few easy steps. This article will guide you through the process, from checking... read more