I was panicking as usual because I accidentally committed my changes on to the master branch. We’re supposed to just
git checkout and create a new branch, opening a pull request before merging to master.
So i was furiously googling, and this came up:
git checkout - How to revert a Git repository to a previous commit - Stack Overflow
git reset --soft <commit>
- Revert back while keeping the changes.
- Does not change the working tree.
- Goes back to commit status such as unstage files.
I tried using
git reset --soft HEAD^ . I’m still not sure what’s the difference between HEAD^ and HEAD. Apparently:
Use ~ most of the time — to go back a number of generations, usually what you want Use ^ on merge commits — because they have two or more (immediate) parents
So it worked, my changes got unstaged. But when i switched to a new branch, it seemed to have disappeared?? So i was panicking because usually i use
git status and
git log to check for modified files, staged / unstaged etc. I was so scared that all my code went missing.
Thankfully, after googling, i found this command:
git reflog — Recovering a git reset —soft - Stack Overflow — which logs out commits that are not in my working tree anymore. Lo and behold, my commits are still there. Phew, all i have to do is to just reset back to that commit and make sure to push it up now.
Kinda weird why
git log doesn’t show the commits.
One of the most consolidated misconceptions about programming, since the early days, is the idea that such activity is purely technical, completely exact in nature, like Math and Physics. Computation is exact, but programming is not.