I've got some files I'm looking for but I'm not sure which branch they got put into. I'd like to list all the files for a given directory across all branches.
My question is, in git, is there a way to list all files in a directory across all branches?
For listing, you may use git ls-files to list all files recursively in the current index/working directory. You may refer to Git-SCM Docs / git-ls-files or type man git-ls-files if you have installed Git and have man pages available.
$ git show-branch As working with Git version control system, we have to work with branches. Our repositories may contain a number of branches and sometimes it is hard to remember all – whether on the local or remote repo.
You can get the files in a directory using git ls-tree.
Here I'm writing the output of git ls-tree
to a file.
$ for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
git ls-tree -r --name-only $branch <directory> >> file-list ;
done
You can get the unique files by:
sort -u file-list
Use git ls-tree -r
to recursively list all files for a particular treeish. Combine with git for-each-ref
to enumerate branches and some shell filtering.
for i in $(git for-each-ref '--format=%(refname)' 'refs/heads/*') ; do
echo $i
git ls-tree -r $i | awk '$4 ~ /filename-pattern/ {print $4}'
echo
done
Replace filename-pattern
with, well, a regular expression that matches the files that you're interested in. Remember to escape any slashes in the path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With