如何在git别名中引用远程存储库? [英] How to reference remote repositories in a git alias?

查看:143
本文介绍了如何在git别名中引用远程存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用git别名在远程存储库上的等效跟踪分支上执行一个名为origin的操作?例如,如果我在本地仓库的分支foo上,并且想运行一个通常默认为origin / foo分支的命令,我该怎么做?



下面是一个具体的例子:

我想看看来自origin / foo分支的传入提交,并且我处于本地富分支。为此,我会运行 git fetch&& git log --pretty = oneline --abbrev-commit ..origin / foo 。因此,我设置了一个 git传入别名,如下所示:

  [别名] 
incoming =!git fetch&& git log --pretty = oneline --abbrev-commit .. @ {u}

然而,这别名解释为 git fetch&& git log --pretty = oneline --abbrev-commit ..upstream / foo ,其中 @ {u} 替代等价物分支在上游远程。我想要执行相同的命令,但要在 origin 远程的等效分支上执行。我怎样才能做到这一点?我试过 @ {o} ,但那不起作用,我在git别名文档的任何地方都找不到这个语法。我从这个答案中的@ sschuberth的评论得出我的答案,但我还没有找到其他文件。



更新
我找到了一个紧密的解决方案,但它使用bash参数,而不是一个git别名:


  incoming =!git fetch&& git log --pretty = oneline --abbrev-commit .. $ 1 

这里的缺点是我无法利用git提供的自动完成功能,它不会自动比较我的本地分支和我的远程 origin 存储库。

解决方案

请注意,据我所知, Git仍然不能正确支持三角工作流程,您需要从上游远程服务器进行操作,应用所做的更改并将其推送至远程远程服务器(这是您的上游分支项目)。 Git的维护人员Junio表示,即使在三角形工作流程中,< at> b< b> {u}仍然应该参考
与您整合的地方,即您的上游,而不是您推动发布工作结果的地方

每个本地分支只能有一个远程追踪分支,并且要么是 upstream ,或者 origin 中的对应分支。 @ {u} 指向配置为远程跟踪分支的任何内容,而不管语义上是否为上游项目或分支项目。令人混淆的是,Git文档将 @ {u} 引用为上游的缩写,但实际上大部分时间点都指向通常是原点的东西。 为您的本地分行 foo upstream / foo origin / foo 使用 git branch --set-upstream-to origin / foo

编辑:如果您喜欢冒险,可以尝试使用 Felipe Contreras的Git分支,该分支添加发布跟踪分支的概念。 p>

How can one reference a git alias to perform an operation on the equivalent tracking branch on a remote repository called "origin"? For example, if I am on branch "foo" of my local repo, and I want to run a command that normally defaults to my "origin/foo" branch, how can I do so?

Here is a concrete example:

I want to see the incoming commits from my "origin/foo" branch, and I am on my local "foo" branch. To do so, I would run git fetch && git log --pretty=oneline --abbrev-commit ..origin/foo. Thus, I set up a git incoming alias as follows:

[alias]
    incoming = !git fetch && git log --pretty=oneline --abbrev-commit ..@{u}

However, this alias is interpreted as git fetch && git log --pretty=oneline --abbrev-commit ..upstream/foo, where the @{u} is a substitute for the equivalent branch on the upstream remote. I want to do the same command, but on the equivalent branch on the origin remote. How can I do this? I tried @{o}, but that doesn't work, and I cannot find this syntax anywhere in the git alias docs. I derived my answer from @sschuberth's comment in this answer, but I have not found much documentation otherwise.

Update I found a close solution, but it uses a bash argument instead of a git alias:

incoming = !git fetch && git log --pretty=oneline --abbrev-commit ..$1

The disadvantage here is that I cannot leverage the auto-completion provided by git and it does not automatically compare my local branch against that of my remote origin repository.

解决方案

Note that to the best of my knowledge Git still does not properly support a "triangular" workflow where you pull from an "upstream" remote, apply your changes, and push to an "origin" remote (which is your fork of the upstream project). Junio, Git's maintainer, says

Note that even in a triangular workflow,  <at> {u} should still refer to
the place you integrate with, i.e. your "upstream", not to the place
you push to publish the result of your work.

You can only have one remote tracking branch per local branch, and that either is the corresponding branch in upstream, or the corresponding branch in origin. @{u} points to whatever is configured as the remote tracking branch, independent of whether semantically this is your upstream project or forked project. Confusingly, the Git docs refer to @{u} as being a shorthand for "upstream", but in fact it most of the time points to what usually is "origin".

Long story short, for @{u} to work like you want your need to change the remote tracking branch for your local branch foo from upstream/foo to origin/foo using git branch --set-upstream-to origin/foo.

Edit: If you're adventurous you could probably also try using Felipe Contreras' fork of Git which adds the concept of a "publish" tracking branch.

这篇关于如何在git别名中引用远程存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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