See also:
How can I see which Git branches are tracking which remote / upstream branch?
How can I find out which remote branch a local branch is tracking?
Do I need to parse git config output, or is there a command that would do this for me?
See also:
How can I see which Git branches are tracking which remote / upstream branch?
How can I find out which remote branch a local branch is tracking?
Do I need to parse git config output, or is there a command that would do this for me?
Two choices:
% git rev-parse --abbrev-ref --symbolic-full-name @{u}
origin/mainline
or
% git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)"
origin/mainline
git branch -vv | grep 'BRANCH_NAME'
git branch -vv : This part will show all local branches along with their upstream branch .
grep 'BRANCH_NAME' : It will filter the current branch from the branch list.
git-status porcelain (machine-readable) v2 output looks like this:
$ git status -b --porcelain=v2
# branch.oid d0de00da833720abb1cefe7356493d773140b460
# branch.head the-branch-name
# branch.upstream gitlab/the-branch-name
# branch.ab +2 -2
And to get the branch upstream only:
$ git status -b --porcelain=v2 | grep -m 1 "^# branch.upstream " | cut -d " " -f 3-
gitlab/the-branch-name
If the branch has no upstream, the above command will produce an empty output (or fail with set -o pipefail).
You can try this :
git remote show origin | grep "branch_name"
branch_name needs to be replaced with your branch
Lists both local and remote branches:
$ git branch -ra
Output:
feature/feature1
feature/feature2
hotfix/hotfix1
* master
remotes/origin/HEAD -> origin/master
remotes/origin/develop
remotes/origin/master
If you want to find the upstream for any branch (as opposed to just the one you are on), here is a slight modification to @cdunn2001's answer:
git rev-parse --abbrev-ref --symbolic-full-name YOUR_LOCAL_BRANCH_NAME@{upstream}
That will give you the remote branch name for the local branch named YOUR_LOCAL_BRANCH_NAME.
Improving on this answer, I came up with these .gitconfig aliases:
branch-name = "symbolic-ref --short HEAD"
branch-remote-fetch = !"branch=$(git branch-name) && git config branch.\"$branch\".remote || echo origin #"
branch-remote-push = !"branch=$(git branch-name) && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"
branch-url-fetch = !"remote=$(git branch-remote-fetch) && git remote get-url \"$remote\" #" # cognizant of insteadOf
branch-url-push = !"remote=$(git branch-remote-push ) && git remote get-url --push \"$remote\" #" # cognizant of pushInsteadOf
Having tried all of the solutions here, I realized none of them were good in all situations:
This command gets all names:
git branch -a --contains HEAD --list --format='%(refname:short)'
For my application, I had to filter out the HEAD & master refs, prefer remote refs, and strip off the word 'origin/'. and then if that wasn't found, use the first non HEAD ref that didn't have a / or a ( in it.
I use this alias
git config --global alias.track '!sh -c "
if [ \$# -eq 2 ]
then
echo \"Setting tracking for branch \" \$1 \" -> \" \$2;
git branch --set-upstream \$1 \$2;
else
git for-each-ref --format=\"local: %(refname:short) <--sync--> remote: %(upstream:short)\" refs/heads && echo --URLs && git remote -v;
fi
" -'
then
git track
note that the script can also be used to setup tracking.
More great aliases at https://github.com/orefalo/bash-profiles
git branch -vv | grep 'hardcode-branch-name'
# "git rev-parse --abbrev-ref head" will get your current branch name
# $(git rev-parse --abbrev-ref head) save it as string
# find the tracking branch by grep filtering the current branch
git branch -vv | grep $(git rev-parse --abbrev-ref head)
Display only current branch info without using grep:
git branch -vv --contains
This is short for:
git branch -vv --contains HEAD
and if your current HEAD's commit id is in other branches, those branches will display also.
I use EasyGit (a.k.a. "eg") as a super lightweight wrapper on top of (or along side of) Git. EasyGit has an "info" subcommand that gives you all kinds of super useful information, including the current branches remote tracking branch. Here's an example (where the current branch name is "foo"):
pknotz@s883422: (foo) ~/workspace/bd
$ eg info
Total commits: 175
Local repository: .git
Named remote repositories: (name -> location)
origin -> git://sahp7577/home/pknotz/bd.git
Current branch: foo
Cryptographic checksum (sha1sum): bd248d1de7d759eb48e8b5ff3bfb3bb0eca4c5bf
Default pull/push repository: origin
Default pull/push options:
branch.foo.remote = origin
branch.foo.merge = refs/heads/aal_devel_1
Number of contributors: 3
Number of files: 28
Number of directories: 20
Biggest file size, in bytes: 32473 (pygooglechart-0.2.0/COPYING)
Commits: 62