【问题标题】:Programmatically get remote used for git push以编程方式获取远程用于 git push
【发布时间】:2019-03-25 14:27:49
【问题描述】:

如何获取发出git push时使用的遥控器?

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

相关:Default remote for git fetch

【问题讨论】:

    标签: git push git-push git-remote


    【解决方案1】:

    这对我有用:

    $ 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.
    

    【讨论】:

      【解决方案2】:

      取决于你想要远程的名称还是它的 URL,也可以

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

      在第一行中查看“To”之后的 URL 可能会。

      【讨论】:

      • Enter passphrase for /home/ravi/.ssh/XXXX: git-receive-pack: permission denied: XXXX fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
      • 或:fatal: No configured push destination.Either specify the URL from the command-line or configure a remote repository using...
      • 好吧,当然--dry-run 仅在真正的推送有效时才有效。如果您希望能够获得远程推送,即使真正的推送失败,您也应该在问题中更清楚地说明这一点。
      • 无论如何,如果你添加--verbose,仍然会打印URL。
      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-02
        • 2021-08-20
        • 1970-01-01
        • 1970-01-01
        • 2019-05-18
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        相关资源
        最近更新 更多