Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch file location

Tags:

git

branch

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?

like image 755
Will Taylor Avatar asked Oct 15 '25 04:10

Will Taylor


2 Answers

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.

like image 97
Enrico Campidoglio Avatar answered Oct 17 '25 19:10

Enrico Campidoglio


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.

like image 33
Romain Valeri Avatar answered Oct 17 '25 21:10

Romain Valeri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!