Can I get the upstream remote branch by ruby-git

111 views Asked by At

I'm using ruby-git to operate my Git repo. I can get the local branch that checkout from remote branch, how can I get it upstream remote branch? This's the code:

require 'Git'
repo = Git.open("xxxpath.git")
localbranch = repo.branches["localbranchnamexxx"]
2

There are 2 answers

0
Siim Liiser On

The same way you would do it in normal git

remote_branch = repo.branches["origin/localbranchnamexxx"]
0
mblythe On

Counter-intuitively (to me, at least), the branch tracking information is stored in the git config, not in any branch or ref structures.

require 'git'
repo = Git.open("xxxpath.git")
localbranch = repo.current_branch
upstream_remote = repo.config["branch.#{localbranch}.remote"]
upstream_ref = repo.config["branch.#{localbranch}.merge"]
upstream_branch = upstream_ref.split('/').last
upstream = "#{upstream_remote}/#{upstream_branch}"