Saltar al contenido ForoHackers

How I force-pushed over a coworker's work (and the command that would have saved me)

CI/CDDepuraciónControl de versiones
#979 · git_blame_myself · · Denunciar

Confession time, because my mistakes are the only lessons I reliably learn from.

Last month I rebased my feature branch and needed to push it. Git refused, as it should. So I did what everyone does:

git push --force

What I did not know: a colleague had pushed a commit to the same branch an hour earlier. My force push replaced the remote branch with my version and his commit disappeared from the branch. It was still in his local clone, thankfully, and in my reflog because I had fetched it, but we lost an afternoon working out what happened.

The command I should have used:

git push --force-with-lease

It means "overwrite the remote only if it is still at the commit I last saw". If someone pushed in the meantime, the push fails and I have to look at their work first. Costs nothing. Now I have it as the only force I allow myself: a shell alias, and no plain --force anywhere.

Lesson: the reflog is a seatbelt, not a safety plan.

#1314 · tabs_over_spaces_42 · · Denunciar

One more caveat: --force-with-lease can be fooled if your editor or a background tool does a git fetch automatically. Then your local "last seen" is already updated and the lease thinks all is well. Pass the expected commit explicitly (--force-with-lease=branch:sha) if you want it airtight.

#1315 · git_blame_myself · · Denunciar

That is a very good point and I did not know it. Adding it to my notes, along with the apology I owe my colleague.

#1316 · rubber_duck_supervisor · · Denunciar

Notice what you did right: you went from "what did I do wrong" to "what would have made this impossible". The second question is the useful one. Branch protection on shared branches takes care of most of this class of accident.

Inicia sesión o únete para responder.