简体   繁体   English

如何在git rebase之后推送到远程?

[英]How to push to remote after a git rebase?

Hi I successfully removed a vile commit using: git rebase -p 0df8fg5^ 0df8fg5 您好我成功切除一个vile承诺使用: git rebase -p 0df8fg5^ 0df8fg5

I am working on the master branch. 我正在master分公司工作。 However now when I: 但是现在当我:

git push

It gives: 它给:

To git@github.com:my_user/my_repo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:my_user/my_repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git status gives: git status给出:

$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 3 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean

It's saying that you have diverged from master, because it appears to git as though there is a point where you have commits that origin/master doesn't have and origin/master has commits that you don't have. 它说你已经脱离了master,因为它似乎是git,好像有一个点你有源origin/master没有的提交而origin/master有你没有的提交。 That point is the point at which you removed the commit from your local copy of master (The rebase changes the sha's of the following commits). 这一点是您从本地master副本中删除提交的点(rebase更改了以下提交的sha)。

To fix this you have to force push . 要解决这个问题,你必须强制推动 BUT by default, pushing pushes all your local branches to a branch with the same name in the remote, which means doing a force push could overwrite other people's changes. 但是 ,默认情况下,推送将所有本地分支推送到远程中具有相同名称的分支,这意味着强制推送可能会覆盖其他人的更改。

You can change the default behaviour of git in this regard with a config setting: 您可以使用配置设置更改git的默认行为:

git config --global push.default <setting>

where <setting> can be upstream , current , or simple to prevent pushing to all branches you have local versions of. 其中<setting>可以是upstreamcurrentsimple以防止推送到您拥有本地版本的所有分支。 The default before Git 2 is matching which has the overwriting behaviour I was talking about, whereas the default behaviour from Git 2 onwards is simple . Git 2之前的默认matching具有我正在谈论的覆盖行为,而Git 2以后的默认行为很simple Here's an excerpt from the git man-page describing what each of these settings actually does: 这是git man-page的一段摘录,描述了每个设置实际上做了什么:

  • current - push the current branch to update a branch with the same name on the receiving end. current - 推送当前分支以更新接收端具有相同名称的分支。 Works in both central and non-central workflows. 适用于中央和非中央工作流程。

  • upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). upstream - 将当前分支推回到分支,该分支的更改通常集成到当前分支(称为@ {upstream})。 This mode only makes sense if you are pushing to the same repository you would normally pull from (ie central workflow). 如果您要推送到通常从中拉出的相同存储库(即中央工作流),则此模式才有意义。

  • simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch's name is different from the local one. simple - 在集中式工作流程中,像上游一样工作,如果上游分支的名称与本地分支不同,则可以更加安全地拒绝推送。

    When pushing to a remote that is different from the remote you normally pull from, work as current. 当推送到与您通常拉出的遥控器不同的遥控器时,请作为当前工作。 This is the safest option and is suited for beginners. 这是最安全的选择,适合初学者。

    This mode will become the default in Git 2.0. 此模式将成为Git 2.0中的默认模式。

-- git-config man-page - git-config man-page

The command for force pushing is: 强制推动的命令是:

git push -f

You have to force the push 你必须强迫推动

git push -f

If others are also using the remote's master branch this will surprise them a great deal. 如果其他人也使用遥控器的主分支,这将给他们带来很多惊喜。 Not necessarily in a positive way. 不一定是积极的方式。

After a rebase, you need to generally do a force push to remote. 在一次改变之后,你需要一般强行推动远程。

$ git push -f origin master:master

What this will do is forcibly update master without caring about fast-forwarding 这样做会强制更新master而不关心快进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM