Reputation:
I know that I can list the files tracked by a certain branch using git ls-tree -r <branch> --name-only
and this gives me exactly what I was looking for. However when I try to do this with a remote-only branch i get the error: sh: origin/master: No such file or directory
. So is there a way I can list files in a remote branch without pulling that branch?
Upvotes: 0
Views: 216
Reputation: 487883
It's worth mentioning first that this is not the set of files tracked by a branch or tracked by a (Git) repository as these phrases aren't really well-defined. The result of git ls-files -r
on a commit is the set of files in that commit.
The word tracked in Git is rather overloaded, but when we talk about a tracked file vs an untracked file in Git, what we mean is a file that is, or is not, in the index right now.
The set of files that will be in Git's index, if you use git checkout
or git switch
to extract some particular commit, is normally1 the set of files that is in that commit. So in that sense, you can call those "tracked" files, but if you do, you're setting yourself up for confusion. The tracked files are those that are in Git's index right now. You can change that set any time you like:
git add F
will copy the work-tree copy of file F
into Git's index.2 If it was not there before—was untracked—well, now it is, so now it's tracked. Or:
git rm --cached F
will remove the index copy of file F. If it was there before, it's not now; now file F
is untracked. It doesn't matter if file F
is in, or not in, some other commit, unless and until you check out that commit.
Whenever you make a new commit, Git makes that new commit using the files that are in the index at that time. Even the commands git commit -a
, git commit --include files
, and git commit --only files
work with the index, although these make use of extra, temporary index files as well so as to make the updates easy to roll back in case of failures. (This makes describing their exact action, especially in the presence of Git hooks, particularly tricky.)
Meanwhile, you can inspect any commit that you have, using git ls-files -r
on it; you just have to use a name that Git can turn into the correct commit hash ID. As you noted in a comment, you were using two names instead of one name.
1The word normally is here because you can play various tricks with Git's index, or use an alternate index, by setting the environment variable GIT_INDEX_FILE
. Also, Git will sometimes allow you to carry index and/or work-tree changes across commit and/or branch switching operations. For the gory details, see Checkout another branch when there are uncommitted changes on the current branch.
2If you've set the --assume-unchanged
or --skip-worktree
bits on a file that is currently in the index, git add
won't update the index from the work-tree. You can use GIT_INDEX_FILE
as in footnote 1 as well.
Upvotes: 1