Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

git - 强制“ 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做到这一点?)

  ask by opensas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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时收到类似的错误:)

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的更多详细信息:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...