Friday, January 10, 2025
HomeTechHow to undo the last commit on GitHub?

How to undo the last commit on GitHub?

To undo the last commit on GitHub, you can use the following steps based on your situation:


If You Have Not Pushed the Commit:

  1. Remove the Last Commit but Keep Changes:
    bash
    git reset --soft HEAD~1

    This will undo the last commit and leave your changes staged for a new commit.

  2. Remove the Last Commit and Discard Changes:
    bash
    git reset --hard HEAD~1

    This completely removes the last commit and any changes made in it.


If You Have Already Pushed the Commit:

  1. Undo Locally and Force Push:
    If the commit has been pushed, use git reset followed by a force push:

    bash
    git reset --hard HEAD~1
    git push origin main --force

    Note: Force-pushing can overwrite others’ work if they have also made changes. Use it cautiously.

  2. Revert the Commit (Safe for Shared Repositories):
    If others might have pulled the changes, create a new commit to undo the changes:

    bash
    git revert HEAD

    This creates a new commit that reverses the last commit’s changes.


Choose the method based on whether you’ve pushed the commit and whether others are collaborating on the branch.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x