简体   繁体   English

将git远程名称重命名为“上游”

[英]Rename git remote name to “upstream”

I have a tiny problem with my git setup. 我的git设置有一个小问题。 I am working with forked repositories quite often. 我经常使用分叉的存储库。 To fork them, I use the button on GitHub. 为了分叉它们,我使用了GitHub上的按钮。 After that, the command git remote -v shows me "origin" and "original-username". 之后,命令git remote -v向我显示“原始”和“原始用户名”。

"original-username" is now the upstream branch where I can pull changes from and create pull requests too. 现在,“原始用户名”是上游分支,我可以在其中提取更改并创建提取请求。 I would like to have this named "upstream" though. 我想将其命名为“上游”。 That would make it easier to use the same commands on different forks, and I can even create aliases with that. 这将使在不同的fork上使用相同的命令变得更加容易,我什至可以使用它创建别名。

So I know I can change the remote name with git remote rename original-username upstream , but that would mean I have to do it for every fork, every time I get that fork on a machine, and I have to make git remote -v first etc, and I am lazy :P 所以我知道我可以使用git remote rename original-username upstream更改远程名称,但是这意味着每次我在机器上安装该fork时,我都必须对每个fork进行更改,并且必须使git remote -v首先等,我很懒:P
Also original-username is different on every fork. 每个叉上的原始用户名也不同。

So my question is: 所以我的问题是:
Is there a possibility to rename the remote branch (from different names) to upstream, maybe with a cool alias function or a small script? 是否可以将远程分支(从不同名称)重命名为上游,可能是使用很酷的别名功能或小的脚本?

My dream woudl something like this: 我的梦想是这样的:

[alias]
    setupstream = "!f(){ git remote rename $oldupstream upstream; };f"

$oldupstream should be gotten automatically, in some way. $oldupstream应该以某种方式自动获取。

Making the assumption that a local repository will only have two remote's configured where one is origin and the other is the one you want to rename something like this should work. 假设本地存储库仅配置了两个远程存储库,其中一个是origin ,另一个是您想重命名类似名称的库。

setupstream = "!f() { \
    local rem=$(git remote | grep -v '^origin$'); \
    if [ \"$(wc -l <<<\"$rem\")\" -gt 1 ]; then \
        echo 'Too many remotes.' >&2; \
        return; \
    fi; \
    if [ -z \"$rem\" ]; then \
        echo 'No non-origin remote found.' >&2; \
        return; \
    fi; \
    if [ \"$rem\" = upstream ]; then \
        echo '\"upstream\" remote already exists.'; \
        return; \
    fi; \
    git remote rename \"$rem\" upstream; \
    echo \"Renamed $rem to upstream.\"; \
}; f"

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

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