问题
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:
- I ask team members not to push until I'm done.
- I fetch.
- I update the branch's history as needed (amend, rebase, etc.).
- I force push.
- I tell them I'm done.
- 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.
回答1:
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.
回答2:
Seems you understand what you're doing and why, and why it's unsafe. Let me add two points:
They do force-fetch/pull (
git fetch/pull -f
).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.
回答3:
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!
来源:https://stackoverflow.com/questions/44664456/safe-force-push-procedure