As a programmer we make mistakes and then learn from them. Every programmer knows the feeling of creating a feature and then having to rebase, doing the wrong thing unknowingly; weither it be out of focus or don't realising you made a mistake. Then force pushing the rebase and overwriting a whole weeks worth of work. That happened to me last month.
Now. To start off. Yes. It was a very stupid mistake to make. You should take extra care when you use the
-f|--force argument with anything really. It never hurts to double check what you are about to do and think about what you expect will happen before pushing through with it. This is a typical mistake "experienced" power user tend to make because they've become comfortable enough to just run it.
It even happened to me a couple times. Just not to this extend. I decided to create a script that would guard me from making this mistake. The name of this script is
git-backup. It's a small wrapper script that warns when you are about to
-f|--force push to remote. It will also recommend to use
--force-with-lease, an option that you should always use when you are working with multiple people on the same feature.
Writing this wrapper wasn't all that hard. I did make sure that all original options sayed available through the wrapper.
git-backup kicks into action when you:
- Start a rebase. It will put your current work in a backup branch. This ensures that your current work as-is, is kept secure in another branch. In the case that you do make a mistake you can use the backup branch to recover.
- When you try to force push. It will warn you and give you a command for when you really want to push that through. This extra step makes you think about what you are about to do.
It never hurts to be safe. Better safe than sorry. Scripts like this are a friendly reminder to prevent the user from making a mistake.
The script will be available on my Github soon. I will also be making a video about this over on my new Youtube Channel.