Undo my last commit and split it into two separate ones

Share this video with your friends

Social Share Links

Send Tweet

When you accidentally committed some changes to your branch you have various possibilities to “undo” that operation and add some more changes. One is to use git amend to change the commit message or add new files. But what we want to take out all of the committed changes again and maybe even split them into separate commits. Let’s see how we can achieve that. Note: This only holds if you did not yet push your changes to the remote repository

Instructor: [00:00] Let's assume we have the following situation. I have here one commit, this one, which has not already been pushed to the origin master, which we can see here. Origin master is one commit behind actually. If I want to modify that commit here, like adding a different file, I could just create that file and then use the git commit --amend to add that file again.

[00:22] What if I would like to change the entire commit, like I would exclude a file which I accidentally committed into that commit as well? To take that last commit again back into our staging area and out of the committed set of commits, we can use the following command. We can use git reset. We go one commit back.

[00:44] If you now do git status, we can see that I took out the entire commit. It's still in the staging area as if I had never committed it. Also, in the git log, it is no more there. What I can do now is to simply change the commit as I want.

[01:00] I could, for instance, say that I want to commit here the src\app\shared folder. Let's add that to the staging area, to my whole full component and create a new commit, like this. Finally, then, I want to commit that app component. You can just do git add status. Here, we have the app component now in the staging area and create a new commit for that one.

[01:31] In the git log output, we can now see that we split it up one commit into two. We have now that add new [inaudible] component and then on top of it, the apply [inaudible] component in our app component.