thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

git reset --keep

• 136 words • 1 min • updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

In all these years of git, I’ve only heard about it now: via Adam Johnson:

Git: avoid reset –hard, use reset –keep instead

But whilst researching for my book on Git, I discovered git reset –keep in the documentation. The description there is brief and oblique, but after figuring it out, I realized that –keep is way more preferable to –hard!

I have a git uncommit alias in my ~/.gitconfig:

uncommit = !git reset --soft HEAD^ && git restore --staged .

…which is akin to “undo”.

And there’s the nuclear git reset --hard + git clean -x -f -d to “reset everything”.

git reset --keep seems like a safer git reset --hard. Adam did a good job describing how it works, so go read it directly there.

This has the same spirit as git push --force-with-lease.