Move a Commit that was Committed on the Wrong Branch

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

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.

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

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

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

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

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

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

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

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

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