何时使用强制推送

何时使用强制推送

本文介绍了Git - 何时使用强制推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GIT - FORCE PUSH



任何人都可以告诉我何时使用

即使是 push --force 初学者:当您更新拉取请求时。



拉取请求意味着


  • 您在GitHub上分发回购(例如)

  • 在本地克隆它

  • 创建分支并添加一些补丁/新提交

  • 将该分支推送到您的分支(您拥有的)
  • 会触发一个Pull Request,通知原始仓库的所有者您希望将您的PR分支合并。



但是:如果原始仓库自己提交了新的提交,则需要重新绑定(重放提交)更新的上游回购

  git remote添加上游/网址/原始/回购
git结帐my_pr_branch
git rebase upstream / master
#测试一切仍然正常工作

通过重新绑定,您更改新提交的SHA1:您需要替换e通过您的重新发布的提交发布(推送)您的PR分支提交:

  git push --force 

这将更新现有的Pull Request,它将考虑这些提交的新版本。

由于您正在强制推送您自己的repo(fork)以及您自己的分支(PR分支),因此您可以使用 - force 尽可能多的时间。






我介绍了,作为检测您是否想要强制推送的远程repo发生任何事情的一种方法。

请注意,它已成为。

GIT - FORCE PUSH

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

  • 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.

这篇关于Git - 何时使用强制推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 04:55