DEV Community

Ingo Steinke
Ingo Steinke

Posted on • Updated on

Don't force push to git!

This was not new to me anymore, but still...

Recently, I had to do some fixes in a code base that I used to edit without any collaborators, so I got a little sloppy about proper collaboration workflows.

I thought let's reuse yesterday's branch name (bad idea!)

Same branch that had been deleted on GitHub after successful merge. I created another branch of the same name for my fixes, and after doing some more coding than I had expected, I did a local rebase and proceeded to push to the remote repository.

What to do when branches have diverged?

git push -f ? (no! Don't!! Bad idea!!!)

Because now a coworker was working on the same code, and they actually had the same idea to use the same branch name, and they had already pushed their code to GitHub.

After force-pushing my code changes, I wanted to review his pull request. Only to find nothing but my own code inside the pull request.

If I had used

git push --force-with-lease

my push would have been rejected, and I would have seen the information about another divergent change right on my command line (and probably in my IDE as well, if it allows force pushing at all).

We were lucky, as git as decentralized system, that my coworker still had the code and could force-push over mine.

Unnecessary trouble: I should have known better! I learned about --force-with-lease years ago. See: git-scm.com/docs/git-push I just didn't care at that moment.

Lesson learned

Lesson learned: if you have to force push, do it with lease!

While it does not prevent you messing up your own work, at least you won't destroy other people's code mistakenly.

Top comments (0)