Skip to content

Comment on A safer git checkout

Comments

I've been using Git for a couple years now, and I don't think I've ever lost any work.

Even if you do `git reset --hard HEAD~5`, "throwing away" your last few commits, they aren't actually discarded yet. You can do `git reflog` to see them and then `git checkout -b some_commit_hash` to recover one to a branch, or `git reset --hard some_commit-hash` to set this branch back to that point.

Only if those commits stay orphaned for a while (a week or two?) will Git truly discard them.

It becomes dangerous if you have changes in your working tree. You truly lose those changes with a "git reset --hard HEAD" or the forced checkout.

Exactly. As mentioned in the article, `git reset --keep` is the preferred way to do it these days. It preserves uncommitted worktree changes.

Oh! I see. I would never do a `git reset` of any kind if I hadn't committed or stashed. Yikes!

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.