In addition to the accepted answer, I found this good practice we can follow to prevent checkout
accidents from happening again.
This stuck out most to me from this answer.
"Here's another crazy idea: don't run 'git checkout ...' on a dirty work tree. Problem solved."
I stopped ever since using git checkout .
to discard any unstaged/uncommitted changes. I now use the command which was made to be more suitable for this: git reset
.
git reset
and git reset --hard
when you're absolutely sure you want to discard your changes permanently. git stash
is also your friend, just before you discard those changes, doesn't hurt to stash it before you delete. Making these two a habit and using git checkout
only for switching between branches (mostly) works really well for me.