How to reference remote repositories in a git alia

2019-08-09 15:12发布

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.

1条回答
forever°为你锁心
2楼-- · 2019-08-09 15:32

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.

查看更多
登录 后发表回答