Git: 1。列出一个分支中的所有文件,2。比较来自不同分支的文件

  1. 寻找像 ls -Rdir/s这样的命令,它可以列出提交/分支中的所有文件。
  2. 有没有什么命令可以比较来自不同分支的两个文件?
123182 次浏览
  1. git ls-tree -r --name-only <commit> (where instead of <commit> there can be <branch>).
    You might want to use also -t option which lists subdirectories before descending into them
  2. git diff <branchA>:<fileA> <branchB>:<fileB>,
    or if you want to compare the same file git diff <branchA> <branchB> -- <file>

To compare the same file from different branches:

git diff branch_1..branch_2 file.txt

To list all files in a tree object:

git ls-tree -r branch

To list all files added in the new branch

git diff --name-only branch1 master

As of Git v2.1.0 [08/15/14]

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.

It has nice options to show files in different ways like cached, staged, deleted, modified, ignored or others for the untracked. It also supports matching patterns. Having also a --debug arg, you can easily list creation time, modification time, inode id, staged0, staged1 and staged2 for files.


For the diff of two branch, simply use git diff <branch> <other branch> as stated in other answers.

List files in branch with git ls-files

  1. Try git ls-files described in the git-scm docu:
# Switch to <branch> of interest
$ git checkout <branch>
# List all files in <branch>
$ git ls-files

For further options check the documentation.