强制“ git push”覆盖远程文件

本文翻译自:Force “git push” to overwrite remote files

I want to push my local files, and have them on a remote repo, without having to deal with merge conflicts. 我想推送我的本地文件,并将它们放在远程仓库中,而不必处理合并冲突。 I just want my local version to have priority over the remote one. 我只希望我的本地版本优先于远程版本。

How can I do this with Git? 如何使用Git做到这一点?


#1楼

参考:https://stackoom.com/question/i6Fa/强制-git-push-覆盖远程文件


#2楼

You should be able to force your local revision to the remote repo by using 您应该可以使用以下命令将本地修订版本强制为远程仓库

git push -f <remote> <branch>

(eg git push -f origin master ). (例如git push -f origin master )。 Leaving off <remote> and <branch> will force push all local branches that have set --set-upstream . 离开<remote><branch>将强制推送所有设置了--set-upstream本地分支。

Just be warned, if other people are sharing this repository their revision history will conflict with the new one. 请注意,如果其他人共享此存储库,他们的修订历史将与新的存储库发生冲突。 And if they have any local commits after the point of change they will become invalid. 并且如果它们在更改点之后有任何本地提交,则它们将变为无效。

Update : Thought I would add a side-note. 更新 :我想加个旁注。 If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. 如果您要创建其他人可以查看的更改,那么创建具有这些更改的分支并定期重新设置基础以使它们与主要开发分支保持最新状态并不少见。 Just let other developers know this will happen periodically so they'll know what to expect. 只是让其他开发人员知道这会定期发生,以便他们知道会发生什么。

Update 2 : Because of the increasing number of viewers I'd like to add some additional information on what to do when your upstream does experience a force push. 更新2 :由于观众数量的增加,我想添加一些有关upstream遇到强制推动时该怎么做的附加信息。

Say I've cloned your repo and have added a few commits like so: 假设我已经克隆了您的仓库,并添加了一些提交,如下所示:

D----E  topic
           /
A----B----C         development

But later the development branch is hit with a rebase , which will cause me to receive an error like so when I run git pull : 但是后来development分支受到了rebase攻击,这将导致我在运行git pull时收到类似的错误:

扫描二维码关注公众号,回复: 10784830 查看本文章
Unpacking objects: 100% (3/3), done.
From <repo-location>
 * branch            development     -> FETCH_HEAD
Auto-merging <files>
CONFLICT (content): Merge conflict in <locations>
Automatic merge failed; fix conflicts and then commit the result.

Here I could fix the conflicts and commit , but that would leave me with a really ugly commit history: 在这里,我可以解决冲突并commit ,但这会使我留下非常丑陋的提交历史:

C----D----E----F    topic
      /              /
A----B--------------C'  development

It might look enticing to use git pull --force but be careful because that'll leave you with stranded commits: 使用git pull --force可能看起来很诱人,但要小心,因为这会使您陷入搁浅的提交状态:

D----E   topic

A----B----C'         development

So probably the best option is to do a git pull --rebase . 因此,最好的选择可能是执行git pull --rebase This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue . 这将需要我像以前一样解决任何冲突,但是对于每一步,而不是提交,我将使用git rebase --continue In the end the commit history will look much better: 最后,提交历史看起来会更好:

D'---E'  topic
           /
A----B----C'         development

Update 3: You can also use the --force-with-lease option as a "safer" force push, as mentioned by Cupcake in his answer : 更新3:您也可以使用--force-with-lease选项作为“更安全”的强制推送, 如Cupcake在他的回答中提到的

Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own: 如果使用“租用”强制推送,则如果遥控器上有您不期望的新提交(从技术上讲,如果尚未将其提取到远程跟踪分支中),则强制推送失败。您不想意外覆盖您甚至不知道的其他人的提交,而只想覆盖自己的提交:

 git push <remote> <branch> --force-with-lease 

You can learn more details about how to use --force-with-lease by reading any of the following: 您可以通过阅读以下任何内容来了解​​有关如何使用--force-with-lease的更多详细信息:


#3楼

Another option (to avoid any forced push which can be problematic for other contributors) is to: 另一个选择(避免任何可能对其他贡献者造成问题的强行推送)是:

  • put your new commits in a dedicated branch 将您的新提交放入专门的分支
  • reset your master on origin/master 重置masterorigin/master
  • merge your dedicated branch to master , always keeping commits from the dedicated branch (meaning creating new revisions on top of master which will mirror your dedicated branch). 将您的专用分支合并到master ,始终保留专用分支的提交(这意味着在master上创建新的修订版本,以反映您的专用分支)。
    See " git command for making one branch like another " for strategies to simulate a git merge --strategy=theirs . 有关模拟git merge --strategy=theirs策略,请参见“ 使一个分支像另一个分支的git命令 ”。

That way, you can push master to remote without having to force anything. 这样,您可以将master推送到远程,而无需强制执行任何操作。


#4楼

You want to force push 你想强行推

What you basically want to do is to force push your local branch, in order to overwrite the remote one. 您基本上想要做的就是强制推送本地分支,以覆盖远程分支。

If you want a more detailed explanation of each of the following commands, then see my details section below. 如果要对以下每个命令进行更详细的说明,请参阅下面的“我的详细信息”部分。 You basically have 4 different options for force pushing with Git: 基本上,您可以使用4种不同的方式来强制使用Git进行推送:

git push <remote> <branch> -f
git push origin master -f # Example

git push <remote> -f
git push origin -f # Example

git push -f

git push <remote> <branch> --force-with-lease

If you want a more detailed explanation of each command, then see my long answers section below. 如果您想对每个命令进行更详细的说明,请参阅下面的“我的长答案”部分。

Warning: force pushing will overwrite the remote branch with the state of the branch that you're pushing. 警告:强行推送将用您正在推送的分支的状态覆盖远程分支。 Make sure that this is what you really want to do before you use it, otherwise you may overwrite commits that you actually want to keep. 在使用它之前,请确保这是您真正想要做的,否则您可能会覆盖您实际想要保留的提交。

Force pushing details 强制推送细节

Specifying the remote and branch 指定遥控器和分支

You can completely specify specific branches and a remote. 您可以完全指定特定的分支和远程。 The -f flag is the short version of --force -f标志是--force的简短版本

git push <remote> <branch> --force
git push <remote> <branch> -f

Omitting the branch 省略分支

When the branch to push branch is omitted, Git will figure it out based on your config settings. 如果省略了分支到push分支,Git将根据您的配置设置将其解决。 In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch: 在2.0之后的Git版本中,新的仓库将具有默认设置以推送当前已签出的分支:

git push <remote> --force

while prior to 2.0, new repos will have default settings to push multiple local branches. 在2.0之前的版本中,新存储库将具有默认设置以推送多个本地分支。 The settings in question are the remote.<remote>.push and push.default settings (see below). 有问题的设置是remote.<remote>.pushpush.default设置(请参见下文)。

Omitting the remote and the branch 省略遥控器和分支

When both the remote and the branch are omitted, the behavior of just git push --force is determined by your push.default Git config settings: 当省略remote和branch时,仅git push --force的行为由push.default Git配置设置确定:

git push --force
  • As of Git 2.0, the default setting, simple , will basically just push your current branch to its upstream remote counter-part. 从Git 2.0开始,默认设置simple基本上只会将当前分支推送到其上游远程对等部分。 The remote is determined by the branch's branch.<remote>.remote setting, and defaults to the origin repo otherwise. 远程由分支的branch.<remote>.remote设置确定,否则默认为源branch.<remote>.remote

  • Before Git version 2.0, the default setting, matching , basically just pushes all of your local branches to branches with the same name on the remote (which defaults to origin). 在Git 2.0版之前,默认设置matching基本上只是将您的所有本地分支推送到远程上具有相同名称的分支(默认为起源)。

You can read more push.default settings by reading git help config or an online version of the git-config(1) Manual Page . 您可以通过阅读git help configgit-config(1)手册页的在线版本来阅读更多push.default设置。

Force pushing more safely with --force-with-lease 使用--force-with-lease强制更安全地推动

Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own: 如果使用“租用”强制推送,则如果遥控器上有您不期望的新提交(从技术上讲,如果尚未将其提取到远程跟踪分支中),则强制推送失败。您不想意外覆盖您甚至不知道的其他人的提交,而只想覆盖自己的提交:

git push <remote> <branch> --force-with-lease

You can learn more details about how to use --force-with-lease by reading any of the following: 您可以通过阅读以下任何内容来了解​​有关如何使用--force-with-lease的更多详细信息:


#5楼

git push -f is a bit destructive because it resets any remote changes that had been made by anyone else on the team. git push -f有点破坏性,因为它会重置团队中其他任何人所做的任何远程更改。 A safer option is {git push --force-with-lease}. 一个更安全的选择是{git push --force-with-lease}。

What {--force-with-lease} does is refuse to update a branch unless it is the state that we expect; {--force-with-lease}所做的是拒绝更新分支,除非它是我们期望的状态。 ie nobody has updated the branch upstream. 即没有人更新上游分支。 In practice this works by checking that the upstream ref is what we expect, because refs are hashes, and implicitly encode the chain of parents into their value. 在实践中,这是通过检查上游ref是否符合我们的期望来实现的,因为ref是散列,并将父链隐式地编码为其值。 You can tell {--force-with-lease} exactly what to check for, but by default will check the current remote ref. 您可以确切告诉{--force-with-lease}要检查的内容,但是默认情况下会检查当前的远程引用。 What this means in practice is that when Alice updates her branch and pushes it up to the remote repository, the ref pointing head of the branch will be updated. 实际上,这意味着当Alice更新其分支并将其推送到远程存储库时,分支的ref指向头将被更新。 Now, unless Bob does a pull from the remote, his local reference to the remote will be out of date. 现在,除非Bob从遥控器上拉出,否则他对遥控器的本地引用将是过时的。 When he goes to push using {--force-with-lease}, git will check the local ref against the new remote and refuse to force the push. 当他使用{--force-with-lease}进行推送时,git将对照新的远程服务器检查本地引用,并拒绝强制进行推送。 {--force-with-lease} effectively only allows you to force-push if no-one else has pushed changes up to the remote in the interim. {--force-with-lease}仅在没有其他人在过渡期间将更改推到远程的情况下才有效地强制推入。 It's {--force} with the seatbelt on. 系上安全带可以{--force}。


#6楼

对我有用git push --set-upstream origin master -f

发布了0 篇原创文章 · 获赞 75 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105484808