Safe force push procedure?

2019-07-31 15:21发布

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.

3条回答
乱世女痞
2楼-- · 2019-07-31 16:17

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.

查看更多
欢心
3楼-- · 2019-07-31 16:18

Seems you understand what you're doing and why, and why it's unsafe. Let me add two points:

  1. They do force-fetch/pull (git fetch/pull -f).

  2. They verify carefully that any code they've already pushed didn't get lost. In case something is lost they use reference log (git reflog) to recover lost commits and cherry-pick/rebase them to the current branches.

查看更多
淡お忘
4楼-- · 2019-07-31 16:26

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!

查看更多
登录 后发表回答