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: <a href="https://stackoverflow.com/q/16838158/5353461" rel="nofollow">Default remote for git fetch</a>


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

<ul><li><a href="https://git-scm.com/docs/git-config#git-config-branchltnamegtpushRemote" rel="nofollow">branch.<name>.pushRemote</a></li> <li><a href="https://git-scm.com/docs/git-config#git-config-remotepushDefault" rel="nofollow">remote.pushDefault</a></li> <li><a href="https://git-scm.com/docs/git-config#git-config-branchltnamegtremote" rel="nofollow">branch.<name>.remote</a></li> <li>origin</li> </ul>

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.


  • HTML5 video fallback when all types unsupported
  • Is Socket.IO a must in developing client apps for NodeJS servers?
  • Unable to add the file iOS application bundle
  • Run cakephp app in apache alias
  • Conditional JOINs based on column value
  • What to do if “git push heroku master” failed?
  • Is there a way to use Firebase Legacy console from new account
  • Syncronizing database from Active Directory
  • Finding the number of elements less than or equal to k in a multiset
  • EIPHTTPProtocolExceptionPeer exception using PutBlock with array of bytes all set to zero
  • How read between delimiters in php DOM of a XML file?
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this
  • SQL append distinct values from two columns and two tables
  • phpmailer - How to verify a sent email arrived at its destination
  • How to extract a number from a string [duplicate]
  • Connect Node.js with Oracle on Windows platform
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Change navbar in bootstrap if user login
  • Best win32 compiled scripting language? [closed]
  • Displaying a list of videos from a channel - Vimeo Advanced API
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • Meteor.. accounts- password— Create account on client without login
  • Generating anchors with PyYAML.dump()?
  • can variables be set randomly when declaring them again?
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Access user's phone number on iOS 7
  • How to autopopulate a field in SugarCRM form
  • Who propagate bugfixes across branches (corporate development)?
  • How to rebase a series of branches?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I rollback to a specific git commit
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?