Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

git - Why must I force push after changing a commit message?

I read this about how to amend commit messages. The accepted answer says:

If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with git push <remote> <branch> --force.

It's my understanding (also from the accepted answer) that git push --force will overwrite all data on the remote branch with the local one.

Why is force-pushing after changing a commit message necessary? What happens if I amend a commit message and try to push without -f or --force?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By amending commits, you are changing their SHA1, which means the local and remote history are no longer the same.

If you want to replace the remote history by your (amended) local one, you will need to force push.
If you don't, Git will refuse the push, and ask you to pull (which in this case is not helpful, as you would merge with identical content but different commit messages)

Force pushing can be dangerous as it forces other collaborators to reset their own local history to the new (forced pushed) one.

As commented, --force-with-lease is safer (if the remote branch you are changing was itself changed since your last pull, meaning other were actively using it and pushing it back, then the forced push is denied).
Combine that with a sensible pull policy (where you always rebase what you have not yet pushed), and force pushing becomes less needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...