首页 > 解决方案 > 以编程方式获取远程用于 git push

问题描述

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

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

相关:git fetch 的默认远程

标签: gitpushgit-pushgit-remote

解决方案


答案不像获取那么简单,因为有一个需要考虑的后备列表:

这些别名考虑了以上所有内容:

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

推荐阅读