如何正确强制 Git 推送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5509543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:29:50  来源:igfitidea点击:

How do I properly force a Git push?

gitpushgit-pushgit-non-bare-repository

提问by Spyros

I've set up a remote non-bare "main" repo and cloned it to my computer. I made some local changes, updated my local repository, and pushed the changes back to my remote repo. Things were fine up to that point.

我已经设置了一个远程非裸“主”存储库并将其克隆到我的计算机上。我做了一些本地更改,更新了我的本地存储库,并将更改推送回我的远程存储库。到那时一切都很好。

Now, I had to change something in the remote repo. Then I changed something in my local repo. I realized that the change to the remote repo was not needed. So I tried to git pushfrom my local repo to my remote repo, but I got an error like:

现在,我不得不更改远程仓库中的某些内容。然后我在本地仓库中更改了一些内容。我意识到不需要更改远程仓库。所以我试图git push从我的本地仓库到我的远程仓库,但我得到了一个错误:

To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of git push --helpfor details.

为了防止您丢失历史记录,非快进更新被拒绝在再次推送之前合并远程更改。有关git push --help详细信息,请参阅“关于快进的注意事项”部分。

I thought that probably a

我认为这可能是一个

git push --force

would force my local copy to push changes to the remote one and make it the same. It does force the update, but when I go back to the remote repo and make a commit, I notice that the files contain outdated changes (ones that the main remote repo previously had).

会强制我的本地副本将更改推送到远程副本并使其相同。它确实会强制更新,但是当我返回远程仓库并进行提交时,我注意到文件包含过时的更改(主远程仓库以前拥有的更改)。

As I mentioned in the comments to one of the answers:

正如我在对其中一个答案评论中提到

[I] tried forcing, but when going back to master server to save the changes, i get outdated staging. Thus, when i commit the repositories are not the same. And when i try to use git push again, i get the same error.

[I] 尝试强制,但是当返回主服务器以保存更改时,我的暂存已过时。因此,当我提交时,存储库是不一样的。当我再次尝试使用 git push 时,我得到了同样的错误。

How can I fix this issue?

我该如何解决这个问题?

回答by Katie

Just do:

做就是了:

git push origin <your_branch_name> --force

or if you have a specific repo:

或者如果你有一个特定的回购:

git push https://git.... --force

This will delete your previous commit(s) and push your current one.

这将删除您之前的提交并推送您当前的提交。

It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution...

这可能不合适,但如果有人偶然发现此页面,认为他们可能想要一个简单的解决方案......

Short flag

短旗

Also note that -fis short for --force, so

另请注意,它-f是 的缩写--force,所以

git push origin <your_branch_name> -f

will also work.

也会起作用。

回答by cregox

And if push --forcedoesn't work you can do push --delete. Look at 2ndline on this instance:

如果push --force不起作用,你可以做push --delete。看2在此实例行:

git reset --hard HEAD~3  # reset current branch to 3 commits ago
git push origin master --delete  # do a very very bad bad thing
git push origin master  # regular push

But beware...

但要小心...

Never ever go back on a public git history!

永远不要回到公共 git 历史!

In other words:

换句话说:

  • Don't ever forcepush on a public repository.
  • Don't do this or anything that can break someone's pull.
  • Don't ever resetor rewritehistory in a reposomeone might have already pulled.
  • 永远不要force推送公共存储库。
  • 不要这样做或任何可能破坏某人的pull.
  • 不碍着resetrewrite历史的回购可能有人已经退出。

Of course there are exceptionally rare exceptions even to this rule, but in most cases it's not needed to do it and it will generate problems to everyone else.

当然,即使是这条规则也有非常罕见的例外,但在大多数情况下,不需要这样做,它会给其他人带来问题。

Do a revert instead.

改为进行还原。

And always be careful with what you push to a public repo. Reverting:

并且始终小心您推送到公共回购的内容。还原:

git revert -n HEAD~3..HEAD  # prepare a new commit reverting last 3 commits
git commit -m "sorry - revert last 3 commits because I was not careful"
git push origin master  # regular push

In effect, bothorigin HEADs (from the revertand from the evil reset) will contain the same files.

实际上,两个原始 HEAD(来自revert和来自evil reset)将包含相同的文件。



edit to add updated info and more arguments around push --force

编辑以添加更新的信息和更多的参数 push --force

Consider pushing force with lease instead of push, but still prefer revert

考虑用租约而不是推推力,但仍然更喜欢恢复

Another problem push --forcemay bring is when someone push anything before you do, but after you've already fetched. If you push force your rebasedversion now you will replace work from others.

另一个push --force可能带来的问题是当有人在你做之前推送任何东西,但在你已经获取之后。如果您现在强制重新调整版本,您将替换其他人的工作

git push --force-with-leaseintroduced in the git 1.8.5(thanks to @VonCcomment on the question) tries to address this specific issue. Basically, it will bring an error and not push if the remote was modified since your latest fetch.

git push --force-with-leasegit 1.8.5 中引入(感谢 @VonC对该问题的评论)试图解决这个特定问题。基本上,如果自最近一次获取以来修改了遥控器,它将带来错误而不是推送。

This is good if you're really sure a push --forceis needed, but still want to prevent more problems. I'd go as far to say it should be the default push --forcebehaviour. But it's still far from being an excuse to force a push. People who fetchedbefore your rebasewill still have lots of troubles, which could be easily avoided if you had revertedinstead.

如果您确实确定push --force需要 a,但仍想防止更多问题,这很好。我会说它应该是默认push --force行为。但这还远不能成为强制使用push. 谁的人获取你的前底垫中仍然有很多的烦恼,可能你已经很容易地避免的恢复来代替。

And since we're talking about git --pushinstances...

既然我们在谈论git --push实例......

Why would anyone want to force push?

为什么会有人想要强制推送?

@linquizebrought a good push force example on the comments: sensitive data. You've wrongly leaked data that shouldn't be pushed. If you're fast enough, you can "fix"*it by forcing a push on top.

@linquize在评论中带来了一个很好的推动力示例:敏感数据。您错误地泄露了不应推送的数据。如果你足够快,你可以通过强制推动来“修复”*它。

*The data will still be on the remoteunless you also do a garbage collect, or clean it somehow. There is also the obvious potential for it to be spread by others who'd fetchedit already, but you get the idea.

*数据仍然会在遥控器上,除非你同时做垃圾回收,或以某种方式进行清洁。它也有可能被已经获取它的其他人传播,但你明白了。

回答by ubik

First of all, I would not make any changes directly in the "main" repo. If you really want to have a "main" repo, then you should only push to it, never change it directly.

首先,我不会直接在“主”存储库中进行任何更改。如果你真的想要一个“主”存储库,那么你应该只推送它,不要直接改变它。

Regarding the error you are getting, have you tried git pullfrom your local repo, and then git pushto the main repo? What you are currently doing (if I understood it well) is forcing the push and then losing your changes in the "main" repo. You should merge the changes locally first.

关于您遇到的错误,您是否尝试git pull过从本地仓库git push到主仓库?您目前正在做的(如果我理解得很好)是强制推送,然后在“主”存储库中丢失您的更改。您应该首先在本地合并更改。

回答by IcedDante

If I'm on my local branch A, and I want to force push local branch B to the origin branch C I can use the following syntax:

如果我在本地分支 A 上,并且想强制将本地分支 B 推送到源分支 CI 可以使用以下语法:

git push --force origin B:C

回答by mustafa Elsayed

use this following command:

使用以下命令:

git push -f origin master

回答by VonC

I would really recommend to:

我真的建议:

  • push only to the main repo

  • make sure that main repo is a bare repo, in order to never have any problem with the main repo working tree being not in sync with its .gitbase. See "How to push a local git repository to another computer?"

  • If you do have to make modification in the main (bare) repo, clone it (on the main server), do your modification and push back to it

In other words, keep a bare repo accessible both from the main server and the local computer, in order to have a single upstream repo from/to which to pull/pull.

换句话说,保持一个可以从主服务器和本地计算机访问的裸仓库,以便有一个单独的上游仓库来拉/拉。

回答by mihai

This was our solution for replacing master on a corporate gitHub repository while maintaining history.

这是我们在维护历史记录的同时替换企业 gitHub 存储库上的 master 的解决方案。

push -fto master on corporate repositories is often disabled to maintain branch history. This solution worked for us.

push -f掌握企业存储库通常被禁用以维护分支历史记录。这个解决方案对我们有用。

git fetch desiredOrigin
git checkout -b master desiredOrigin/master // get origin master


git checkout currentBranch  // move to target branch
git merge -s ours master  // merge using ours over master
// vim will open for the commit message
git checkout master  // move to master
git merge currentBranch  // merge resolved changes into master


push your branch to desiredOriginand create a PR

将您的分支推送到desiredOrigin并创建 PR

回答by Brian M.

I had the same question but figured it out finally. What you most likely need to do is run the following two git commands (replacing hash with the git commit revision number):

我有同样的问题,但终于想通了。您最有可能需要做的是运行以下两个 git 命令(用 git commit 修订号替换 hash):

git checkout <hash>
git push -f HEAD:master