I\'m working in a branch (i.e. design) and I\'ve made a number of changes, but I need to discard them all and reset it to match the repository version. I thought
I found this question after after making a merge and forgetting to checkout develop immediately afterwards. You guessed it: I started modifying a few files directly on master. D'Oh! As my situation is hardly unique (we've all done it, haven't we ;->), I'll offer a reversible way I used to discard all changes to get master looking like develop again.
After doing a git diff to see what files were modified and assess the scope of my error, I executed:
git stash
git stash clear
After first stashing all the changes, they were next cleared. All the changes made to the files in error to master were gone and parity restored.
Let's say I now wanted to restore those changes. I can do this. First step is to find the hash of the stash I just cleared/dropped:
git fsck --no-reflog | awk '/dangling commit/ {print $3}'
After learning the hash, I successfully restored the uncommitted changes with:
git stash apply hash-of-cleared-stash
I didn't really want to restore those changes, just wanted to validate I could get them back, so I cleared them again.
Another option is to apply the stash to a different branch, rather than wipe the changes. So in terms of clearing changes made from working on the wrong branch, stash gives you a lot of flexibility to recover from your boo-boo.
Anyhoo, if you want a reversible means of clearing changes to a branch, the foregoing is a less dangerous way in this use-case.