简体   繁体   中英

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM