Ah ah! Checkout the previous commit, then checkout the master.
git checkout HEAD^
git checkout -f master
Answer from Jason on Stack OverflowAh ah! Checkout the previous commit, then checkout the master.
git checkout HEAD^
git checkout -f master
As others have pointed out one could just delete all the files in the repo and then check them out. I prefer this method and it can be done with the code below
git ls-files -z | xargs -0 rm
git checkout -- .
or one line
git ls-files -z | xargs -0 rm ; git checkout -- .
I use it all the time and haven't found any down sides yet!
For some further explanation, the -z appends a null character onto the end of each entry output by ls-files, and the -0 tells xargs to delimit the output it was receiving by those null characters.
Why does doing 'force checkout' to some previous commit destroy the file that isn't commited?
How to keep changes if I wanna checkout to another branch but I don't wanna commit them yet?
Pulling a branch locally without merging with my branch.
git fetch origin branch-b is probably what you want. It fetches branch b into a remote branch locally in origin/branch-b which you can do a git merge --no-commit origin/branch-b to merge in the change without committing the merge.
To repair your branch I would suggest branching off from the commit before you did the pull. You can do this by doing git checkout new-branch-name commit-hash this way you don't need to muck around with rewriting history or reverting merges.
Best way to update my local master after pushing to remote
Let's say we have a text file with some text: boo. We commit that change and say it has a hash 398d. Now we make changes to that text and add 'foo' but we don't commit it yet. Instead we do git checkout -f 398d and all the changes get destroyed. Why it makes sense? Wouldn't it make more sense if instead we keep the uncommited text in case I switch to master branch again?