Can anyone tell me about when to use git push
and when to git push -f
with an example?
There is a case for push --force
, even for beginners: when you are updating a pull request.
A pull request means
BUT: if that original repo has made new commits of its own, you need to rebase (replay your commits) on top of the updated "upstream" repo
git remote add upstream /url/original/repo
git checkout my_pr_branch
git rebase upstream/master
# test everything is still working
By rebasing, you are changing the SHA1 of your new commits: you need to replace the published (pushed) commits of your PR branch by your rebased commits:
git push --force
That will update the existing Pull Request, which will take into account the new versions of those commits.
Since you are force pushing to your own repo (the fork), and your own branch (the PR branch), you can use --force
as many time as you want.
I presented force-with-lease in 2013, as a way to detect if anything happened to the remote repo you want to force push.
Note that it became more robust recently with Git 2.13.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With