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
Become a member
to unlock all features

Level Up!

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


    Remove Changes from a Commit Before Pushing

    Chris AchardChris Achard

    If we've already committed changes that we don't want to push, we can also remove those with git reset, but we're going to reset back to a specific commit, either with:

    git reset HEAD~1


    git reset [HASH]

    and that will effectively "undo" the commits before that hash.

    Then we can add and commit only the changes that we want.



    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




    Instructor: We're in @.js here and let's make a function called hello world and that function is just going to alert hello. Then sometime later we close this file and open our index.html file and we're going to put a description in here. That will say, "Here's how to fix some common git mistakes."

    What we want to do is commit just this index to html file, but if we're careless and we just do a Git Add-A without looking and we commit that, then we have Git Commit-M adds a description to index. Now if we do Git Status, we can see we're ahead by two commands.

    We almost PUSH, but before pushing we're smart and what we want to do is see what we're about to PUSH. We do a diff on origin master with our current head. What we see is that we have app.js changes that are about to go up, as well as our HTML changes. We realize that we want to get rid of this JavaScript before we push.

    Let's take a look at the log with git log, one line. What we want to do is undo this commit because this commit is the one with the mistake in it. To do that, we're going to use git reset. Git reset takes us back to where we want to reset to. There's two ways we could specify that.

    We want to reset back to this commit here. We could say head and then go back one. That's head ~ 1 and that will go to head and then back on the 301 or we could say git reset and then we could copy this hash exactly so we know what we're resetting to. Now we have reset and we have unstaged changes.

    If we do a git status, now it says we have these changes that are not staged for commit. It's like we didn't even commit them in the first place. For this reason, you really want to be careful. If we had pushed this commit already and then we reset it, then that means we are changing the history that other people may have already downloaded.

    We only want to use reset on branches that we haven't pushed yet. Now, we could git add index.html, which is the only change we want to add and then double check with git status. We have the index to be committed and we are not committing app.js.

    Now we can commit a description for index. If we do a git status now, we have app.js not staged for a commit. We are two commits ahead of origin/master. We can do git log --oneline now. We've gotten rid of that old commit. We are replacing it with this new one.