Git: 1.List all files in a branch, 2.compare files from different branch

Git

Git Problem Overview


  1. Looking for a command like ls -R or dir/s that can list all files in a commit/branch.
  2. Is there any command that can compare two files from different branches?

Git Solutions


Solution 1 - Git

  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>

Solution 2 - Git

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

Solution 3 - Git

To list all files added in the new branch

git diff --name-only branch1 master

Solution 4 - Git

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.

Solution 5 - Git

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, owner & group id, size and flags for files.


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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScudView Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GitdwlzView Answer on Stackoverflow
Solution 3 - GitPeter MansellView Answer on Stackoverflow
Solution 4 - GitdrstoopView Answer on Stackoverflow
Solution 5 - GitBerkant IpekView Answer on Stackoverflow