以编程方式获取用于git push的远程

时间:2018-10-21 09:03:01

标签: git push git-push git-remote

发布git push时如何获得使用的遥控器?

我希望能够在脚本或git alis中使用它。

相关:Default remote for git fetch

3 个答案:

答案 0 :(得分:3)

答案并不像获取那样简单,因为需要考虑一系列故障预置:

这些别名考虑了以上所有情况:

branch-name = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
branch-remote-push  = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"

另外,URL:

branch-url-push = !"remote=$(git branch-remote-push  \"$1\") && git remote get-url --push \"$remote\" #"  # cognizant of pushInsteadOf

答案 1 :(得分:2)

还取决于您是否要使用遥控器的名称或URL

git push --dry-run --porcelain --verbose

,然后在第一行中的“ To”之后查看URL即可。

答案 2 :(得分:0)

这对我有用:

$ git remote get-url --push origin

来自文档 here

**get-url**

Retrieves the URLs for a remote. Configurations for insteadOf and pushInsteadOf are expanded here. By default, only the first URL is listed.

With --push, push URLs are queried rather than fetch URLs.

With --all, all URLs for the remote will be listed.