Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: List all files in a directory across all branches?

Tags:

git

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?

like image 756
hawkeye Avatar asked Dec 10 '14 10:12

hawkeye


People also ask

How do I list all files in a branch?

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.

Which command is used to get the list of all the available branches?

$ 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.


2 Answers

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
like image 184
pratZ Avatar answered Oct 13 '22 09:10

pratZ


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.

like image 23
Magnus Bäck Avatar answered Oct 13 '22 10:10

Magnus Bäck