1

I want to do some things for each branch which fits to a given criteria.

Example:

Track all branches from a specific remote:

For each branch I simply have to: git checkout --track remotes/origin/br1

But if I now want to do this as a bulk change, I have to write something like:

git branch --all| grep 'remotes/origin' | xargs -n1 git checkout --track

But this will fail for some of the found "branches" because they are not branches but symbolic-refs like remotes/origin/HEAD which points to origin/master.

As I read from other answers like https://stackoverflow.com/a/3099621/878532 I see that the people are manually filtering out wrong refs with "grep -v HEAD" here. But this is not what I expect, because here I have to know that HEAD is a symbolic-ref and not a branch.

So I need something to find only real branches. I already take a look on git for-each-ref --format <...> and was in hope that objtype give me some hint but I did not get anything which helps.

Q: How I can get a list of real branches to use them for a bulk change.

Klaus
  • 24,205
  • 7
  • 58
  • 113

1 Answers1

0

HEAD symbolic ref is very annoying in this case, but you can use rev-parse with --symbolic-full-name to get rid of it and obtain the actual branch it references. In the end, I would do:

git branch -rl 'origin/*' | awk '{ print $1; }' | xargs git rev-parse --symbolic-full-name | sort -u

git branch options:

  • -r returns only remote branches
  • -l lets you specify a pattern that selected branch names will match.

I used awk to keep only the first token, discarding -> origin_branch_name. sort -u is necessary here because the branch pointed by HEAD is clearly duplicated now.

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42