Skip to content
👉All tips here

Git uncommit alias: A Quick Tip for Undoing Commits

Git uncommit alias frontend tips

If you are like me, working via the terminal and not used to any Git GUI client, you’ve probably had a situation where you made a commit and immediately regretted it. We’ve all been there, right, right? Maybe you forgot to add a file, made a typo in your commit message, or realized you included some debug code you meant to remove. That’s why I want to share my favorite Git uncommit alias – a simple command that’s saved me countless times.

Here’s the command I used to create this Git alias, the lifesaver:

git config --global alias.uncommit 'reset --soft HEAD~1'

This creates a global Git alias called uncommit that reverts the last commit in the current repository using a soft reset, putting the changes back into the staging area.

Let me explain what this alias does:

  • reset: This Git command moves the current HEAD to a specified state.
  • --soft: This flag keeps your changes staged, so you don’t lose any work.
  • HEAD~1: This refers to the commit before HEAD, effectively targeting the last commit.

If you are not familiar with Git aliases please read more here: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

Use the git uncommit alias

So when you need to use it, just type:

git uncommit

That’s it! Your last commit vanishes, and all the changes are back in your staging area, ready for you to modify or recommit.

I find the uncommit alias most useful in situations like:

  • Fixing an embarrassing typo in my commit message (we’ve all been there!)
  • If I have to be picky and manually add new files and that one “a must” file I somehow forgot to include in the commit.
  • Breaking down a massive commit into smaller, more logical units (because future I will appreciate it during code reviews).

Bonus aliases from my Git toolkit:

Undoing multiple commits – when you’ve gone too far, you can undo multiple commits:

git reset --soft HEAD~3 # Undo last 3 commits

Undoing a commit that already went into the remote branch – the darker scenario, assume you made some changes to your project, staged, committed, and pushed them to a remote branch:

git revert <commit_hash> --no-edit

Tweaking the last commit – If you need to modify the last commit (like changing the commit message), use:

git commit --amend

Please remember, while uncommit is a lifesaver, use it wisely, especially if you’ve already pushed commits to a shared repository. Always keep your team in the loop when modifying shared history.

Ok buddies, commit with confidence knowing you can always uncommit! 🙂