r/git•
pzone

Return name of the current upstream branch

I am trying to define a command git commit-to to automatically commit unstaged changes to a new branch pointing to the same upstream branch as the current one.

Update: thanks to u/felipec for the solution:

"!f() { git stash; 
        git checkout -b $1 origin/`git rev-parse --abbrev-ref @{upstream}`; 
        git stash pop; 
        git add -u; 
        git commit; }; f"

Posted Aug 18, 2015, 2:19 AM UTC · edited

28
jakkarth•

git branch -vv will include the upstream tracked branch in the output if there is one. I'm not sure of an easier way to get it.

3
jakkarth•

git for-each-ref --format='%(upstream:short)' may also be useful, now that I think about it.

2
pzoneOP•

git for-each-ref --format='%(upstream:short)' $(git symbolic-ref HEAD)

does exactly what I'm looking for. =)

1
pzoneOP•

Thanks a lot: the manual for git branch was especially helpful.

git config branch.<refname>.merge will return the name of the upstream ref.

It's not the prettiest command ever since it still needs git symbolic-ref to be substituted in and a post-processing step. Nobody ever said git was pretty though.

1
felipec•
git rev-parse --abbrev-ref @{upstream}
3
pzoneOP•

Damn, now that's some Appendix A kung fu shit.

1
felipec•

Actually you don't need --symbolic. Fixed. (I was a Git developer)

1
pzoneOP•

I had a feeling you were after a glance at git help rev-parse (=

1