Okay, so this is a question after the fact, and I just want to know what I should've done so I can avoid this problem in the future. Yes, I realize force pushing is a dangerous command. Yes, I realize git reset HEAD --hard
can be a dangerous command too. Please help me avoid this problem that cost me ~4 hours of work today.
So here's what happened:
I had edited about 6 files on my current working branch. 3 of those files I wasn't ready to commit yet, so I staged and wrapped 3 of the files into a commit and pushed them to my remote. This left me with 3 unstaged files on my local that contained the bulk of my work.
Soon after pushing, I realized I made a minor typo in one of the files I had just pushed. So instead of adding another commit for the typo, I tried to amend my last commit and force pushed. I then received this error:
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
I believe this was a recent global change made by the maintainer of our enterprise GitLab servers.
Now, this is a personal repo I am the maintainer of; hence why I had no qualms to force push. Especially when it was just a tiny change, and no one else's local git history was going to be messed up due to the push.
But now I was left with a predicament. I have this amended commit on my local machine that I would never be able to push to the remote. So I figured I needed to reset so I could just add an additional commit for the typo. So I ran:
git reset HEAD~1 --hard
And, as I'm sure you've guessed, I lost all the files that I hadn't staged for commit yet. facepalm...
What should I have done in this situation? And while I'm pretty sure those files are gone forever, is there any slight hope I could recover those changes?