Will Taylor
Will Taylor

Reputation: 2304

Git branch file location

I have almost a hundred branches on my local system, most of them out of date. I want to open the file that contains all the branches so I can easily delete most of them, rather than laboriously removing the branches one by one. Is there a way to do that? A file that we can open to see all the branches?

Upvotes: 4

Views: 3678

Answers (2)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59905

Each branch in Git is represented by a single text file containing the SHA-1 of the latest commit in that branch. Those files are called "references".

You can see the paths of the branch refs and the respective commit SHA-1 they point to by saying:

git show-ref --heads

which will return something like:

923eccb90415758c74bb3418007bf5691a0d4a1c refs/heads/some-branch
60e2e7f120f5410ec0ba97fa5093bae4dba4ee57 refs/heads/master

Now, if you wanted to, you could just delete a branch reference by saying:

rm .git/refs/heads/some-branch

but I wouldn't recommend messing with the repository's internal structure directly; instead, you should use Git's own commands to do that.

For example, say you wanted to delete all the branches that start with the word feature-. You could say:

git branch --list 'feature-*'

to get the list of matching branch names. From there, you could delete those branches by saying:

git branch --list 'feature-*' | xargs git branch -D

where the -D option tells Git to delete the branch regardless of whether is has been merged into the current branch or not.

Upvotes: 3

Romain Valeri
Romain Valeri

Reputation: 21908

In git you don't have a file containing a list of branches, but a directory containing one file per branch, named exactly after it.

It's .git/refs/heads, and for example your master branch consists of the file .git/refs/heads/master (no extension) and its content is the commit hash master currently points to.

(note about branches with slashes in the name : they'll be split into the specific directories, i.e. the file for branch feature/new-homepage will be in a subdirectory feature, and named new-homepage)


Also, maybe check packed refs. Either to use it on your repo, or to check if anyone has already packed some refs.

Upvotes: 2

Related Questions