Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get remote used for git push

How can I obtain the remote used when issuing git push?

I want to be able to use this in a script, or in git alises.

Related: Default remote for git fetch

like image 391
Tom Hale Avatar asked Sep 09 '26 14:09

Tom Hale


2 Answers

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

  • branch.<name>.pushRemote
  • remote.pushDefault
  • branch.<name>.remote
  • origin

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
like image 114
Tom Hale Avatar answered Sep 12 '26 04:09

Tom Hale


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.

like image 37
sschuberth Avatar answered Sep 12 '26 04:09

sschuberth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!