Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Undo a Commit that has Already Been Pushed

    Chris AchardChris Achard
    gitgit

    Once a commit is pushed, you do NOT want to use git reset to undo it - because reset will rewrite the history tree, and anyone who has already pulled that branch will have a bad tree.

    Instead, we'll use git revert to make a "revert commit" (like a merge commit), which will "undo" a specific commit. So the syntax is:

    git revert [HASH-TO-UNDO]

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: In our project, we've just made a push to GitHub. If we do a git log oneline, we see that our HEAD is pointing to our local branch master, which is also where our origin master and origin HEAD are, which means our local branch is up to date with our origin.

    Say we have this take three commit here where we edit this function and we want to change something about that. We will not use git reset here though, because we've already pushed this and someone else may have pulled it. If we reset, that means we're re-writing history and we don't want to do that once it's already pushed.

    Instead, we are going to do a git revert. We can do a git revert. Here we want to give it the hash of the commit that we want to revert. That's this one right here. Let's revert this hash, and it brings up a text editor here and it wants us to make a revert commit.

    This is like a merge commit, meaning it adds another commit to the tree, and we have to give it a message. Revert take three, that's a fine message. I'm going to hit :wq to save that.

    Now, if I do a git log oneline, we can see that it has reverted take three. If I check out app.js, that function is gone. What happened is, between here and here, I added the function. The revert of that is to remove the function.

    The really important thing here is that the history is still there. If we went back to this take three commit, we would get our function back. Crucially, anyone who's already pulled the origin/master branch will have a clean history tree because we used revert here instead of reset.

    We can git push this, and now all of our collaborators will have this file empty, as well.