Git Force推送语法,“-f”对比“+分支” [英] Git Force push syntax, "-f" versus "+branch"

查看:119
本文介绍了Git Force推送语法,“-f”对比“+分支”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



git push -f原始分支这两个git命令在强制推送更改时有什么区别? >和 git push origin + branch

解决方案

推送一个分支,没有任何区别。

  git push -f原始分支

  git push origin + branch 

完全等同。当您使用单个 git push 推送多个分支时,会出现差异。仔细阅读,或直接跳到结论获取答案。 $ b

git-push man page告诉你



最基本的 git push 形式涉及一个或多个refspec s)是

  git push< repository> < refspec> ... 

< refspec> ... 参数必须满足在 git-push 手册页中描述:

< blockquote>

< refspec> ...

 用什么源对象指定要更新的目标引用。 $< refspec>`参数的
格式是一个可选的加号+,后跟
源对象< src>,后跟一个冒号`:`,后面跟着
destination ref`< dst>`。


所以,要清楚的是,

  [+]< src> [:< dest>] 

如果:< dst> 被忽略,与< src> 相同的引用将被更新...除非 remote。< repository> .push 条目(如果有的话)在你的Git配置中另有说明。



进一步在 git-push 手册页,您可以找到


通过使用可选的前导 + ,您可以告诉Git更新< dst>
ref,即使它默认不允许(例如,它不是
快进。 )这不会尝试将< src> 合并到< dst> 中。


更进一步:


请注意 - force 适用于所有被推入的ref,因此
u用 push.default 设置为匹配或者用多个推送
目标配置 remote。*。push 可能会覆盖其他
比当前分支(包括严格落后于
远程对象的本地ref)的其他
。要强制推送到一个分支,在refspec前面使用 +
来推送(例如 git push origin + master 强制
a推送到 master 分支)。



总结



简而言之,可选的 + code> -f 不会:如果您一次推送多个参考, + 可以让您以指定哪些推送的引用被强制推送,而 --force (或 -f )适用于 all 被推入的ref。



示例



考虑使用本地回购两个分支, master develop ,您要推送它(使用一个 git push 命令)转换为原始远程。




  • git push origin master develop 会推送两个分支,但都不会强制推送。


  • git push origin + master develop 会推送两个分支,但只有 master 会得到力推动。

  • git push origin master + develop ,反过来,会推送两个分支,但只有 develop 会被强制推送。

  • code>将强制推送两个分支。它与 git push -f origin master develop 完全等价。



What is the difference between these two git commands when force pushing changes?

git push -f origin branch and git push origin +branch

解决方案

If you're only pushing one branch, there is no difference.

git push -f origin branch

and

git push origin +branch

are exact equivalents. The difference arises when you're pushing more than one branch with a single git push. Read on for details, or skip directly to Conclusion for the answer.

What the git-push man page tells you

The most basic form of git push that involve one or more refspec(s) is

git push <repository> <refspec>...

The form that the <refspec>... argument must satisfy is described in the git-push man page thus:

<refspec>...

Specify what destination ref to update with what source object. The
format of a `<refspec>` parameter is an optional plus `+`, followed by
the source object `<src>`, followed by a colon `:`, followed by the
destination ref `<dst>`.

So, just to be clear, the syntax for <refspec> is

[+]<src>[:<dest>]

If :<dst> is omitted, the same ref as <src> will be updated... unless the remote.<repository>.push entry (if any) in your Git config says otherwise.

Further down in the git-push man page, you find

By having the optional leading +, you can tell Git to update the <dst> ref even if it is not allowed by default (e.g., it is not a fast-forward.) This does not attempt to merge <src> into <dst>.

And still further down:

Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).

Conclusion

In a nutshell, the optional + gives you finer control than -f does: if you're pushing more than one ref at a time, the + allows you to specify which of the pushed refs get force-pushed, whereas --force (or -f) applies to all the refs that are getting pushed.

Example

Consider a local repo with two branches, master and develop, which you want to push (with a single git push command) to an origin remote.

  • git push origin master develop will push both branches, but neither will get force-pushed.

  • git push origin +master develop will push both branches, but only master will get force-pushed.

  • git push origin master +develop , conversely, will push both branches, but only develop will get force-pushed.

  • git push origin +master +develop will force-push both branches. It's an exact equivalent to git push -f origin master develop.

这篇关于Git Force推送语法,“-f”对比“+分支”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆