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 some commits to a separate branch that I have accidentally committed to master

    Juri StrumpflohnerJuri Strumpflohner

    Gosh no, I just added all of these commits to master. They were thought to be peer reviewed first in a dedicated branch! No worries, in this lesson we’re going to see a couple of commands that will help you move your commits into a dedicated feature branch.

    Note: This only holds if you did not yet push your changes to the remote repository



    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: I've done some work on my local repository here. I've created here a commit for extracting the styles into a separate file, as they have been hard-coded into the component. I've also fixed the broken test, due to some refactorings which happened earlier.

    Accidentally, however, I committed everything onto master. What I actually wanted is to have these commits on a dedicated branch, so that I could create a pull request, and others could review my changes.

    As a result, I would really like to take these away from the local master branch, move them into that dedicated branch, so that they can push it up then afterwards. There are different kind of possibilities to achieve that. We'll take a look at one of them.

    What is important before we start is that these commits should not have yet been synced to the remote repository. Once we have pushed inaudible remote repository, we cannot change them anymore in the way we are doing right now, because that would mess up the Git commit history, and therefore then create really, really awkward mergers.

    You can see that these commits have not been pushed yet, because the origin master, so our remote counterpart of the master branch, is on this commit here. These are new ones on top of that. In order to have them on a different kind of branch, what I do is simply create a new branch.

    You do that with a git checkout -b for creating a branch, and I call it appRefactoring. You can see we switched to that new branch. Now, if I got a git log again, you can now see that we are on that appRefactoring.

    These commits here are on that branch as well. However, master's also still there. That's what we actually wanted to avoid. The branch is created. Now we need to clean up our master, so let's jump back to master.

    Let's do a git log again. Now we would like to clean up master, so to reset it actually to this point here, where we have synced it to the remote repository. I just copy here the identifier of that commit, and we do a git reset --hard of that commit.

    What it means is that our master branch will now be changed, our local one, to that point here, and all of the commits above that point will be discarded. You can now see that the hat is now pointing at install Angular material, which is exactly what we want.

    Again, let's have a look. Let's do a git log. You can now see we are at this point here. However, we also have our app refactoring. Let's jump over to the app refactoring, let's do a git log, and we see the commits are still there on that app refactoring branch. We can now push it up, have it reviewed, and merge it back into master once we are fine.