Programmatically get remote used for git push

时光毁灭记忆、已成空白 提交于 2019-12-06 11:49:05

The answer is not as simple as for fetching, because there is a list of fallbacks which need to be considered:

These aliases take into account all of the above:

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 #"

And, as a bonus, for the URL:

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

Depending on whether you want the remote's name or its URL, also

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

and looking at the URL after "To " in the first line might do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!