Undo my last commit and split it into two separate ones

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

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.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today