DEV Community

Gaëtan Redin
Gaëtan Redin

Posted on • Originally published at Medium on

How to Undo a GIT Commit

Understand the basics of version control

Hey, I’m sure I’m not in this case. You know you make your git add and then your git commit -m "..." But oh no I forgot a file… Well rather than create a second commit, why not just undo the previous one and recreate it?

Here’s a git alias I use every day:

alias.undo=reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Here’s how to create it:

git config --global alias.undo 'reset --soft HEAD^'
Enter fullscreen mode Exit fullscreen mode

Tips : If you have already pushed your commit into your PR, you will have to use git push -f .

Thanks for reading.

How to undo a commit

Hey, I’m sure I’m not in this case. You know you make your git add and then your git commit -m "..." But oh no I forgot a file… Well rather than create a second commit, why not just undo the previous one and recreate it?

Here’s a git alias I use every day:

alias.undo=reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Here’s how to create it:

git config --global alias.undo 'reset --soft HEAD^'
Enter fullscreen mode Exit fullscreen mode

Tips : If you have already pushed your commit into your PR, you will have to use git push -f .

Thanks for reading.

Learn More

Discussion (0)