Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe force push procedure?

Tags:

git

git-push

push

I hear that force pushing (git push -f) is a dangerous practice to be avoided if possible. That being said, what would be a safe procedure for doing so in the case of a private repo shared among a small team? I imagine it would be something like this:

  1. I ask team members not to push until I'm done.
  2. I fetch.
  3. I update the branch's history as needed (amend, rebase, etc.).
  4. I force push.
  5. I tell them I'm done.
  6. They do ___ to acquire and integrate the new history with their code without anything being lost.

Can someone complete this procedure or scrap it and offer a better one? I'm looking for the simplest safe procedure.

like image 681
MarredCheese Avatar asked Jul 15 '26 06:07

MarredCheese


2 Answers

You can do git push --force-with-lease It will push your changes only when the remote branch is in the same state in the remote as you can see it locally. See https://git-scm.com/docs/git-push#git-push---no-force-with-lease for more details.

like image 185
jbialobr Avatar answered Jul 20 '26 20:07

jbialobr


The following approach looks kind of fragile but will work assuming all other team members don't have any local commits on the said branch.

You add code to the branch and push force it (steps 1-5 in your question):

git checkout feature
# Add code
git push --force-with-lease

Other developers on your team should do the following (Step 6):

git fetch
git branch -D feature
git checkout feature

After this has been executed, all team members will have the exact same branch as you.

If any of them had any non-pushed commits, they will be lost!

like image 26
KidCrippler Avatar answered Jul 20 '26 21:07

KidCrippler