If you’re using Git during development, sometimes you might accidentally commit files locally that you did not intend to or maybe you made a mistake in your code that you want to unstage the file(s) before pushing. In this tutorial I will show you how to do this using Git on the CLI.

  1. In the CLI, type git commit. This should open a text editor. After the comments at the beginning of the file, type your commit message (e.g. “Undoing commit."). Save and exit. If your commit message is one line, you can use git commit -m "Undoing commit."

  2. Next, type git reset HEAD~. This will undo the accidental commit you just did and put your files back to the point where they need to be committed once again. You can check the status of the files by typing git status.

  3. After making the necessary changes, type git add file1 file2 along with the files that you want to commit.

  4. Finally, type git commit which will open the text editor where you can write your commit message or use git commit -m "New commit message." if your commit message is short. If you want to keep the same message as Step 1, simply type git commit -c ORIG_HEAD.