Move some commits to a separate branch that I have accidentally committed to master

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

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

Instructor: [00:00] 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.

[00:14] 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.

[00:25] 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.

[00:38] 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.

[00:56] 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.

[01:12] 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.

[01:26] 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.

[01:38] 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.

[01:54] 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.

[02:09] 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.

Adrien  Giboire
Adrien Giboire
~ 5 years ago

Where is the snippet you invite people to copy/paste to format the output? ;)

Adrien  Giboire
Adrien Giboire
~ 5 years ago

Forget what I just said, it moved to the next video and I got confused with this transcript overlooking for something that looked like it without really reading the content, which would have make me realize I was in the wrong place.

Markdown supported.
Become a member to join the discussionEnroll Today