I was working on a git branch and was ready to commit my changes, so I made a commit with a useful commit message. I then absentmindedly made minor changes to the code that
switching to a new branch losing changes:
git checkout -b YOUR_NEW_BRANCH_NAME --force
switching to an existing branch losing changes:
git checkout YOUR_BRANCH --force
If you want to discard the changes,
git checkout -- <file>
git checkout branch
If you want to keep the changes,
git stash save
git checkout branch
git stash pop
Note that if you've merged remote branches or have local commits and want to go back to the remote HEAD you must do:
git reset --hard origin/HEAD
HEAD
alone will only refer to the local commit/merge -- several times I have forgotten that when resetting and end up with "your repository is X commits ahead.." when I fully intended to nuke ALL changes/commits and return to the remote branch.