6

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?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • Do you know the file names? – hjpotter92 Dec 10 '14 at 10:48
  • Judging from `git-ls-files` doc, one still has to check out the folder's contents from all the branches. So the direct approach is 1) get the branches list; 2) for each branch, checkout the folder and do git-ls-files on it. – raina77ow Dec 10 '14 at 10:54
  • we know the file pattern but not the exact file names – hawkeye Dec 10 '14 at 11:16
  • Did you try `git log --pretty=format: --name-only --diff-filter=A | sort -u`? These might interest you: http://stackoverflow.com/a/543426/1076075 and http://superuser.com/a/429694 – Bharadwaj Srigiriraju Dec 10 '14 at 11:36

3 Answers3

4

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
pratZ
  • 3,078
  • 2
  • 20
  • 29
1

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.

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
  • 1
    To focus on a particular directory use `$i:path/to/it`, and I'm not sure but I think OP just wants the one dir, i.e. not `-r`. – jthill Dec 10 '14 at 12:03
  • Thanks @jthill. How would I do this? (Just one dir) – hawkeye Dec 10 '14 at 20:57
  • exactly as I put it there: replace your `$1` with `$1:path/to/it`, and if you don't want the recursive take out the `ls-tree`'s `-r` – jthill Dec 10 '14 at 21:05
0

thanks to https://stackoverflow.com/users/1587370/pratz

occasionally i found it useful to have also the branch name in the report

for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do 
     git ls-tree -r --name-only $branch . |xargs -L 1 echo \#$branch: 
done
grenix
  • 623
  • 7
  • 15