简体   繁体   English

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

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.如果您正在创建其他人将查看的更改,那么创建一个包含这些更改的分支并定期 rebase 以使它们与主开发分支保持同步并不少见。 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:假设我已经克隆了您的 repo 并添加了一些提交,如下所示:

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

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:使用 Git 强制推送基本上有 4 种不同的选项:

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.当推送分支的分支被省略时,Git 将根据您的配置设置来计算它。 In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch:在 2.0 之后的 Git 版本中,新的 repo 将具有默认设置来推送当前签出的分支:

git push <remote> --force

while prior to 2.0, new repos will have default settings to push multiple local branches.而在 2.0 之前,新的 repos 将有默认设置来推送多个本地分支。 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:当远程和分支都被省略时, 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设置确定,否则默认为原始存储库。

  • 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基本上只是将所有本地分支推送到远程上具有相同名称的分支(默认为 origin)。

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

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/masterorigin/master 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 推送到远程,而无需强制执行任何操作。

为我工作:

git push --set-upstream origin master -f

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

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 将根据新远程检查本地 ref 并拒绝强制推送。 --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

Simple steps by using tortoisegit使用 tortoisegit 的简单步骤

GIT giving local files commit and pushing into git repository. GIT 提供本地文件提交并推送到 git 存储库。

Steps :脚步 :

1) stash changes stash name 1) stash 更改stash 名称

2) pull 2)

3) stash pop 3)藏匿流行

4) commit 1 or more files and give commit changes description set author and Date 4)提交1 个或多个文件并提供提交更改描述集作者和日期

5) push 5)

// make sure your local branch is up to date
git checkout fix-build
git pull origin fix-build

// push your local branch to overwrite remote branch 'main'
git push -f origin fix-build:main

ps.附言。 The accepted answer did not work for me.接受的答案对我不起作用。 I am not sure if it is due to Git version.我不确定是不是 Git 版本的问题。

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

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