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.
-
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 usegit commit -m "Undoing commit."
-
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 typinggit status
. -
After making the necessary changes, type
git add file1 file2
along with the files that you want to commit. -
Finally, type
git commit
which will open the text editor where you can write your commit message or usegit commit -m "New commit message."
if your commit message is short. If you want to keep the same message as Step 1, simply typegit commit -c ORIG_HEAD
.