Git - When to use force push [closed]

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

GIT - FORCE PUSH

Can anyone tell me about when to use git push and when to git push -f with an example?

回答1:

There is a case for push --force, even for beginners: when you are updating a pull request.

A pull request means

  • you fork a repo on GitHub (for instance)
  • clone it locally
  • make a branch and add some patches/new commits
  • push that branch to your fork (that you own)
  • triggers a Pull Request which notifies the owner of the original repo that you want your PR branch to be merged.

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!