I actually once pushed with --force
and .git
repository and got scolded by Linus BIG TIME. In general this will create a lot of problems for other people. A simple answer is "Don't do it".
I see others gave the recipe for doing so anyway, so I won't repeat them here. But here is a tip to recover from the situation after you have pushed out the amended commit with --force (or +master).
- Use
git reflog
to find the old commit that you amended (call it old
, and we'll call the new commit you created by amending new
).
- Create a merge between
old
and new
, recording the tree of new
, like git checkout new && git merge -s ours old
.
- Merge that to your master with
git merge master
- Update your master with the result with
git push . HEAD:master
- Push the result out.
Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push will see the resulting merge will see that you favor new
over old
. Their later merges will not see the conflicts between old
and new
that resulted from your amending, so they do not have to suffer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…