强制“git push”覆盖远程文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10510462/
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
Force "git push" to overwrite remote files
提问by opensas
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 做到这一点?
回答by Trevor Norris
You should be able to force your local revision to the remote repo by using
您应该能够使用
git push -f <remote> <branch>
(e.g. git push -f origin master
). Leaving off <remote>
and <branch>
will force push all local branches that have set --set-upstream
.
(例如git push -f origin master
)。娅<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:
假设我已经克隆了你的 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
分支被 a 击中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
. This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue
. In the end the commit history will look much better:
所以可能最好的选择是做一个git pull --rebase
. 这将要求我像以前一样解决任何冲突,但对于每一步,我将使用git rebase --continue
. 最后,提交历史看起来会好很多:
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:
如果远程上有您意想不到的新提交(从技术上讲,如果您还没有将它们提取到您的远程跟踪分支中),则使用“租约”强制推送允许强制推送失败,这在以下情况下很有用你不想意外地覆盖你甚至不知道的其他人的提交,你只想覆盖你自己的:
git push <remote> <branch> --force-with-lease
您可以
--force-with-lease
通过阅读以下任何内容来了解有关如何使用的更多详细信息:
回答by opensas
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. In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch:
当省略推送分支的分支时,Git 将根据您的配置设置计算出来。在 2.0 之后的 Git 版本中,新的 repo 将具有默认设置来推送当前检出的分支:
git push <remote> --force
while prior to 2.0, new repos will have default settings to push multiple local branches. The settings in question are the remote.<remote>.push
and push.default
settings (see below).
而在 2.0 之前,新的 repos 将具有推送多个本地分支的默认设置。有问题的设置是remote.<remote>.push
和push.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:
当远程和分支都被省略时, just 的行为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. The remote is determined by the branch'sbranch.<remote>.remote
setting, and defaults to the origin repo otherwise.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 开始,默认设置 ,
simple
基本上只会将您当前的分支推送到其上游远程对应部分。远程由分支的branch.<remote>.remote
设置确定,否则默认为原始存储库。在 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.
您可以push.default
通过阅读git-config(1) 手册页git help config
或在线版本来阅读更多设置。
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
通过阅读以下任何内容来了解有关如何使用的更多详细信息:
回答by VonC
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
onorigin/master
- merge your dedicated branch to
master
, always keeping commits from the dedicated branch (meaning creating new revisions on top ofmaster
which will mirror your dedicated branch).
See "git command for making one branch like another" for strategies to simulate agit merge --strategy=theirs
.
- 将您的新提交放在专用分支中
- 重置你
master
的origin/master
- 将您的专用分支合并到
master
,始终保持来自专用分支的提交(意味着在master
其上创建新修订版将反映您的专用分支)。
“见制作一个分支像另一个混帐命令”的策略来模拟git merge --strategy=theirs
。
That way, you can push master to remote without having to force anything.
这样,您可以将 master 推送到远程,而无需强制执行任何操作。
回答by Lando Ke
git push -f is a bit destructive because it resets any remote changes that had been made by anyone else on the team. A safer option is {git push --force-with-lease}.
git push -f 有点破坏性,因为它会重置团队中任何其他人所做的任何远程更改。一个更安全的选择是 {git push --force-with-lease}。
What {--force-with-lease} does is refuse to update a branch unless it is the state that we expect; i.e. 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. You can tell {--force-with-lease} exactly what to check for, but by default will check the current remote ref. 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. Now, unless Bob does a pull from the remote, his local reference to the remote will be out of date. 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} effectively only allows you to force-push if no-one else has pushed changes up to the remote in the interim. It's {--force} with the seatbelt on.
{--force-with-lease} 所做的是拒绝更新分支,除非它是我们期望的状态;即没有人更新上游分支。在实践中,这是通过检查上游 ref 是否符合我们的预期来实现的,因为 ref 是散列,并将父链隐式编码为它们的值。你可以告诉 {--force-with-lease} 确切地检查什么,但默认情况下会检查当前的远程引用。这在实践中意味着当 Alice 更新她的分支并将其推送到远程存储库时,分支的 ref 指向头将被更新。现在,除非 Bob 从远程拉取数据,否则他对远程的本地引用将过时。当他使用 {--force-with-lease} 进行推送时,git 将根据新的远程检查本地引用并拒绝强制推送。{--force-with-lease} 有效地只允许您在没有其他人在此期间将更改推送到远程时强制推送。{--force} 系好安全带。
回答by Jithish P N
Works for me:
对我有用:
git push --set-upstream origin master -f
回答by Nagnath Mungade
Simple steps by using tortoisegit
使用 tortoisegit 的简单步骤
GIT giving local files commit and pushing into git repository.
GIT 提供本地文件提交并推送到 git 存储库。
Steps :
脚步 :
1) stash changesstash name
1) stash 更改stash 名称
2) pull
2)拉
3) stash pop
3)藏匿流行
4) commit1 or more files and give commit changes description set author and Date
4)提交1 个或多个文件并提供提交更改描述集作者和日期
5) push
5)推