Git - When to Use Force Push

GIT - FORCE PUSH

Can anyone please tell me about when to use git push

and when git push -f

with an example?

+2


source to share


1 answer


There is a case for push --force

, even for newbies: when updating a pull request.

Pull Request Tool

  • you are using the foro repo on GitHub (for example)
  • clone it locally
  • create a branch and add some fixes / new commits
  • push this branch to your fork (which you have)
  • triggers a pull request that notifies the owner of the original repo that you want your PR branch to be merged.

BUT: if this original repo made new commits of its own, you need to re-install (rewrite your commits) on top of the updated "upstream" repository

git remote add upstream /url/original/repo
git checkout my_pr_branch
git rebase upstream/master
# test everything is still working

      



When you rebase, you change the SHA1 of your new commits: you need to replace the published (pushed) commits of your PR branch with your rebased commits:

git push --force

      

This will update the existing Pull request to account for new versions of those commits.
Since you are pushing to your own repo (fork) and your branch (PR branch), you can use --force

as many times as you like.


I introduced lease force in 2013 to determine if something happened to the remote repo you want to force push.
Please note that it has become more reliable lately since Git 2.13 .

+1


source







All Articles