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
git branch -vvwill include the upstream tracked branch in the output if there is one. I'm not sure of an easier way to get it.git for-each-ref --format='%(upstream:short)'may also be useful, now that I think about it.git for-each-ref --format='%(upstream:short)' $(git symbolic-ref HEAD)does exactly what I'm looking for. =)
Thanks a lot: the manual for git branch was especially helpful.
git config branch.<refname>.mergewill return the name of the upstream ref.It's not the prettiest command ever since it still needs
git symbolic-refto be substituted in and a post-processing step. Nobody ever said git was pretty though.