How Can I Undo Pushed Commits Using Git?

Better Stack Team
Updated on July 25, 2024

To undo pushed commits in Git, you need to follow a careful process because changing history that has already been shared with others can cause problems. Here are the steps to effectively undo pushed commits depending on your situation:

Undoing Pushed Commits - Two Scenarios

Scenario 1: Undoing Commits Without Force Push

If the commits you want to undo have been pushed recently and you have not yet force pushed:

  1. Use git revert:

    • Revert the commit you want to undo. This creates a new commit that undoes the changes introduced by the original commit.

       
      git revert <commit-hash>
      

      Replace <commit-hash> with the hash of the commit you want to revert. You can specify multiple commits if needed.

      Example:

       
      git revert abc123def
      
  2. Push the Revert Commit:

    • Push the revert commit to the remote repository to undo the changes in the shared history.

       
      git push origin <branch-name>
      

      Replace <branch-name> with the name of the branch where you reverted the commit.

Scenario 2: Undoing Commits with Force Push (Use with Caution)

If you need to undo commits that were pushed earlier and you want to rewrite history, which requires a force push:

  1. Use git reset (Soft or Mixed):

    • Reset the branch to the commit before the commit you want to undo. This moves the branch pointer without changing the working directory or staging area.

       
      git reset --soft HEAD~N
      

      Replace N with the number of commits you want to undo.

 
- Alternatively, use `-mixed` if you also want to unstage the changes:

    ```bash
    git reset --mixed HEAD~N
    ```
  1. Force Push the Updated Branch:

    • Force push the updated branch to overwrite the history on the remote repository. Be cautious as this can disrupt others collaborating on the repository.

       
      git push origin <branch-name> --force
      

      Warning: Force pushing can cause problems for collaborators who have based their work on the original commits. It should be used carefully and communicated to your team.

Additional Considerations

  • Collaboration: Communicate with your team before force pushing to avoid conflicts or disruptions.
  • Backup: Before making any significant changes, ensure you have backups or copies of important changes.
  • Best Practices: Rewriting history should be avoided on branches that are shared and actively worked on by others unless absolutely necessary.

Conclusion

Undoing pushed commits in Git requires careful consideration of your project's collaboration needs and version history. Using git revert is generally safer as it keeps a record of the undo action without altering history, whereas git reset followed by force push can rewrite history and should be used with caution. Choose the method that best fits your situation and follow Git best practices to maintain a clean and collaborative repository.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github