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.


    Move a Commit that was Committed on the Wrong Branch

    Chris AchardChris Achard

    We'll do some work and commit it - but then realize it was on the wrong branch!

    To move a commit from one branch to another, we'll first git cherry-pick the commit we want, and then use git reset to remove the commit from the master branch.

    Then we'll be able to manually delete the changes that we don't want on the master branch.



    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 have two branches here and we are currently on our master branch. Let's write a function like second function. That's just going to alert this is number two. Save that. If we do a git status, we have modified app JS, so let's add that and commit that as my second function.

    Before pushing, we do a git branch and realize we made this change on the master branch and we meant to do it on the JS changes branch. If we do a git log --oneline now, we can see that we want to move this commit from master over to JS changes. The problem is master is pointing here. We have to be able to move master back here as well.

    There are several ways we can do this. Many of them involve a git reset hard at some point. Beware of those because if you do reset hard incorrectly, you can lose data. We're going to use git reset, but not a git reset hard.

    Let's look at our branches again. We're going to switch to the JS changes branch, so git checkout JS changes. What we want to do to get this function into it is copy this commit hash. We're going to cherry pick that commit hash. We're going to cherry pick the commit hash that we want to move over.

    Now, on the JS changes branch, we have the code we want so we can push that. Now JS changes is correct. Let's switch back to master.

    If we look at master, we still have the code here. If we do a log one line, we still have that commit because cherry pick just copies over the commit. It doesn't remove it from the other branch. What we want to do is switch master to be back at this commit.

    We're going to do a git reset here, but not a reset hard, back to this commit hash. We could do that commit hash or we could go to the head and go back one. We'll do that. Now if we do a git status, we have app.js, which is still modified. This is because we didn't do a reset hard.

    Now what we can do, since we don't want this function in here, is we can manually do this remediation, which is git checkout app.js. That will reset app.js to whatever it is at the hash that we reset to.

    If we do a git status now, app.js is not changed. If we do a git log --oneline, that commit is gone. This is like manually doing a reset hard, but I like this better because we get to choose exactly what files we get to reset.