131

Is there a way to browse and display files in a git repo without cloning it first? I can do those in svn using the commands:

svn ls /path/to/repo 
svn cat /path/to/repo/file-in-repo

I can supposedly use git show but doing:

git show /path/to/repo
git show HEAD:/path/to/repo

result to

fatal: Not a git repository
Christian Alis
  • 6,556
  • 5
  • 31
  • 29

7 Answers7

88

The command you want is git ls-remote which allows you to get some information about remote repositories, but you cant show history or list directories or anything of that level: essentially it only lets you see the remote objects at a very high-level (you can see the current HEADs and tags for example).

The only real way to do what you want (if I understand correctly) would be to use ssh to run a remote command and return the results, for example:

ssh me@otherhost "cd repo && git log -n 10"

What you want would be lovely functionality if they could add it, but from what I read it's not very easy since getting history etc needs a lot of information to be local to git, and at that point you may as well have done a git fetch.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 17
    One thing you can do with `git clone`, though, is to fetch only a single revision by passing `--depth 1`. This avoids fetching potentially large amounts of history, and would be sufficient to answer questions like "which files are present in revision `abcdef1234567890`?" – ctrueden Aug 06 '15 at 22:06
24

Git is distributed version control system, while Subversion is centralized (client-server) version control system. They work differently; get used to that. Please read my answer explaining the consequences of that difference to git equivalent of svn status -u question at StackOverflow.

Repeating myself a bit: in centralized version control system (like CVS or Subversion) almost all commands are processed on server, and involve network. Very few commands are performed locally. Note that to have good performance of "svn status" and "svn diff" Subversion stores 'pristine copy' of checked-out version on client, to not have to involve network transfer for those common operations (this means that Subversion checkout = 2 x size of working directory at least).

In distributed version control system (like Git, Mercurial or Bazaar), where you have local copy (clone) of a whole repository, almost all commands are performed on client. Very few commands require network connection to other repository (to server).

The number of command you can perform on server is limited.

  • You can list all references on remote with "git ls-remote <URL>".
  • You can get snapshot of (part) of repository (if remote server enabled it) with
    "git archive --remote=<URL> HEAD".
  • You can clone only a few last commits (so called "shallow clone") with
    "git clone --depth=1 <URL>".
  • If server provides git web interface to repository, you can use it to browse.
Community
  • 1
  • 1
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 53
    As correct as you are, if you are browsing a remote repo without first cloning it, then obviously you have decided that it is ok to forego the offline capabilities of git. Given that, I see no reason to pretend that this wouldn't be a useful feature for some things, for instance, a local client that allows you to browse a the file contents of a remote repo locally. – LadyCailin Nov 01 '12 at 15:44
  • 16
    Agreed, the position that Jakub takes is highly restrictive. It is worth losing reputation more than once to point this out. – ctpenrose Jul 19 '13 at 00:09
  • 13
    I dislike the "get used to that" tone., but when readin until the end I found a solution to my current problem - wating to see what's in 110 repository I have one git, but no ssh or shell access to, and which are all probably quite big, some 12GB or so. So the clone with a minmized depth helps at least to only see recent interesting history. and make the git repo as small as possible. – Henning Mar 05 '14 at 10:55
  • 3
    Such functionality would be nice to have in case of code review tools where you don't need the whole repo just log with changes is enough. – Lukasz Lenart Mar 21 '14 at 09:09
  • 2
    @Henning, Right you are, I guess you could call it the "git used to that" tone LOL – Motorhead Sep 20 '17 at 02:57
  • 2
    Imagine creating a Release Notes / Changelog document for a Yocto-based project. What you see in your `git log` is a lot of "bump version" commits and if you want to know what has actually changed, with this "get used to this" approach, you have to go and clone every repository that normally only gets cloned on the build server. Not very convenient, you know. So it would really be helpful if git server could do `log` or fetch individual files. Especially taking in account that nothing actually prevents it from doing so except for the "get used to it" attitude. – Alexander Amelkin Mar 12 '18 at 11:57
  • @AlexanderAmelkin: if there is a web interface for those projects repositories, you won't have to clone each, you could just use that interface (be it gitweb, cgit, GitLab CE, etc.). – Jakub Narębski Mar 12 '18 at 14:04
  • 1
    Th web is a not a catch-all for every solution. Some of us use the CLI to automate certain tasks. A GUI solution is not necessarily a solution if one thinks beyond narrow solution domains. – Ken Ingram Aug 11 '20 at 22:09
  • If I understand correctly, the network access between a local and remote looks more or less like copying down the files stored in `.git`. Assuming that's the case, then it should be possible to build a client that provides 100% of the local functionality of git at less cost than a clone using an in-process-memory "file system" that downloads on demand. This would likely be a net loss for a CLI (it would re-download stuff) but it would be handy as a library for tooling like CI/CD. – BCS Jun 09 '21 at 21:17
17

Take a look at http://git-scm.com/book/en/Git-Internals-Transfer-Protocols for info on how to do this over some transport protocols. Note this won't work for standard git over SSH.

For git over SSH, an up-to-date server-side git should allow you to git-archive directly from the remote, which you could then e.g. pipe to "tar t" to get a list of all files in a given commit.

Crowie
  • 3,220
  • 7
  • 28
  • 48
Jerome Baum
  • 736
  • 6
  • 15
14

GitHub is svn compatible so you can use svn ls

svn ls https://github.com/user/repository.git/branches/master/

BitBucket supports git archive so you can download tar archive and list archived files. It is not very efficient but works:

git archive --remote=git@bitbucket.org:repository HEAD directory | tar -t
fela
  • 679
  • 8
  • 7
9

While you have to checkout a repository, you can skip checking out any files with --no-checkout and --depth 1:

$ time git clone --no-checkout --depth 1 https://github.com/torvalds/linux .
Cloning into '.'...
remote: Enumerating objects: 75646, done.
remote: Counting objects: 100% (75646/75646), done.
remote: Compressing objects: 100% (71197/71197), done.
remote: Total 75646 (delta 6176), reused 22237 (delta 3672), pack-reused 0
Receiving objects: 100% (75646/75646), 201.46 MiB | 7.27 MiB/s, done.
Resolving deltas: 100% (6176/6176), done.

real    0m46.117s
user    0m13.412s
sys     0m19.641s

And while there is only a .git directory:

$ ls -al
total 0
drwxr-xr-x   3 root  staff    96 Dec 26 23:57 .
drwxr-xr-x+ 71 root  staff  2272 Dec 27 00:03 ..
drwxr-xr-x  12 root  staff   384 Dec 26 23:58 .git

you can get a directory listing via:

$ git ls-tree --full-name --name-only -r HEAD | head
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Documentation/.gitignore
Documentation/ABI/README

or get the number of files via:

$ git ls-tree -r HEAD | wc -l
   71259

or get the total file size via:

$ git ls-tree -l -r HEAD | awk '/^[^-]/ {s+=$4} END {print s}'
1006679487
Ross Smith II
  • 11,799
  • 1
  • 38
  • 43
  • 2
    Interesting quirk to this technique: `git status` reports that all files in the repo have been deleted. When using GUI tools, be wary that they can temporarily lock up on large repos! – Billy Jo Feb 24 '21 at 20:36
6

Not the exact, but a way around.

Use GitHub Developer API

  1. Opening this will get you the recent commits.

    https://api.github.com/repos/learningequality/ka-lite/commits

    You can get the specific commit details by attaching the commit hash in the end of above url.

  2. All the files ( You need sha for the main tree)

    https://api.github.com/repos/learningequality/ka-lite/git/trees/7b698a988683b161bdcd48a949b01e2b336b4c01

I hope this may help.

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
Anurag Kanungo
  • 354
  • 3
  • 5
  • 24
    Don't confuse Git with GitHub - I believe original questions was about Git itself. Git installations/hostings (GitHub/BitBucket/Stash) can have various possibilities to browser repositories. – Krzysztof Wolny Feb 25 '15 at 08:04
  • Good idea @Anurag Kanungo Thinking outside the box – Sentry.co Sep 07 '17 at 21:22
2

This is probably considered dirty by some, but a very practical solution in case of github repositories is just to make a script, e.g. "git-ls":

#!/bin/sh
remote_url=${1:? "$0 requires URL as argument"}
curl -s $remote_url | grep js-directory-link | sed "s/.* title=\"\(.*\)\".*/\1/"

Make it executable and reachable of course: chmod a+x git-ls; sudo cp git-ls /usr/local/bin. Now, you just run it as you wish:

git-ls https://github.com/mrquincle/aim-bzr
git-ls https://github.com/mrquincle/aim-bzr/tree/master/aim_modules

Also know that there is a git instaweb utility for your local files. To have the ability to show files and have a server like that does in my opinion not destroy any of the inherent decentralized characteristics of git.

Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39