hs14428
hs14428

Reputation: 129

Get list of git repo branch names

How do I get a list of branch names from a git repository in a shell script?

e.g. If my repo has the following branches:

master, dev, test, feature/new-feature

I want to store these in a list that can be accessed to check if a a certain branch name exists already. I then can use this command(s) in a script for use in a Jenkinsfile

Thanks

Upvotes: 0

Views: 1157

Answers (1)

phd
phd

Reputation: 95028

You can use git branch — without options it lists all local branches, with option -r remote branches, with -a all (local + remote combined).

But the command is not intended to be used in shell scripts. The most correct way is to use git for-each-ref:

git for-each-ref --format="%(refname:short)" refs/heads/ 

to list local branches;

git for-each-ref --format="%(refname:short)" refs/remotes/ 

to list remote branches;

git for-each-ref --format="%(refname:short)" refs/tags/

to list tags;

git for-each-ref --format="%(refname:short)" refs/

to list all references (local branches + remote branches + tags).

Upvotes: 3

Related Questions