How can it possible to get all names of some remote origin branches?
I started from --remote --list
options, but got redundant origin/HEAD -> origin/master
message and branches from the another origin.
$> git branch --remote --list
origin/HEAD -> origin/master
origin1/develop
origin1/feature/1
origin1/feature/2
origin1/feature/3
origin1/master
origin2/develop
origin2/feature/1
origin2/feature/2
origin2/master
Branches of specific origin could be matched with <pattern>
option, but redundant message is still there. Actually that pattern is not really correct, because some origin's name could be a substring of another origin name, or even some branch.
$> git branch --remote --list origin1*
origin1/HEAD -> origin/master
origin1/develop
origin1/feature/1
origin1/feature/2
origin1/feature/3
origin1/master
What am I looking for is a list of branch names of origin1
, any of them I could use for git checkout
command. Something like that:
develop
feature/1
feature/2
feature/3
master
It's important that it should be done without grep
, sed
, tail
or even ghc -e
wrappers, only with true git
power, because of their unsafeness and variation.
It's important that it should be done without
grep
,sed
,tail
or evenghc -e
wrappers, only with true git power, because of their unsafeness and variation.
That is only true for git porcelain commands (see "What does the term porcelain mean in Git?")
Use the plumbing command ls-remote, and then you will be able to filter its output.
ls-remote without parameter would still list the remote HEAD:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote origin
8598d26b4a4bbe416f46087815734d49ba428523 HEAD
8598d26b4a4bbe416f46087815734d49ba428523 refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012 refs/heads/test_trap
But if you ask only for the heads of said remote:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin
8598d26b4a4bbe416f46087815734d49ba428523 refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012 refs/heads/test_trap
Final answer:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin | sed 's?.*refs/heads/??'
master
test_trap
(Yes, it uses sed
, but the output of a plumbing command is supposed to be stable enough to be parsed)
See also Git 2.23 (Q3 2019) which documents an example:
git branch -r -l '<remote>/<pattern>'
git for-each-ref 'refs/remotes/<remote>/<pattern>'
An alternate method, after some research into the same problem, is:
git for-each-ref --format='%(refname:strip=2)' refs/remotes/<remote_name>
This will give a sorted list of your local refs for the named remote at the point you last fetched.
You can adjust this for their tags etc.
The existing answer both uses something explicitly not wanted in the question (sed) and is a remote command.
I found this that avoids both those issues, uses only local git commands and a pipe:
git rev-parse --remotes=origin | git name-rev --name-only --stdin
Update: Not really optimal either, but keeping it if someone knows how to improve it. It lists the full remote including the /remotes/origin prefix if you have no local branch, but only the local name if you have. In addition it seem to skip some refs if there are several pointing to the same SHA1.
来源:https://stackoverflow.com/questions/10075184/git-list-of-branch-names-of-specific-remote