Git Clone: Just the files, please?

后端 未结 8 656
北海茫月
北海茫月 2021-01-30 00:25

I want to clone a GIT repo and NOT end up with a .git directory. In other words I just want the files. Is there a way to do this?

git clone --no-chec

8条回答
  •  清酒与你
    2021-01-30 00:53

    The git command that would be the closest from what you are looking for would by git archive.
    See backing up project which uses git: it will include in an archive all files (including submodules if you are using the git-archive-all script)

    You can then use that archive anywhere, giving you back only files, no .git directory.

    git archive --remote= | tar -t
    

    If you need folders and files just from the first level:

    git archive --remote= | tar -t --exclude="*/*"
    

    To list only first-level folders of a remote repo:

    git archive --remote= | tar -t --exclude="*/*" | grep "/"
    

    Note: that does not work for GitHub (not supported)

    So you would need to clone (shallow to quicken the clone step), and then archive locally:

    git clone --depth=1 git@github.com:xxx/yyy.git
    cd yyy
    git archive --format=tar aTag -o aTag.tar
    

    Another option would be to do a shallow clone (as mentioned below), but locating the .git folder elsewhere.

    git --git-dir=/path/to/another/folder.git clone --depth=1 /url/to/repo
    

    The repo folder would include only the file, without .git.

    Note: git --git-dir is an option of the command git, not git clone.


    Update with Git 2.14.X/2.15 (Q4 2017): it will make sure to avoid adding empty folders.

    "git archive", especially when used with pathspec, stored an empty directory in its output, even though Git itself never does so.
    This has been fixed.

    See commit 4318094 (12 Sep 2017) by René Scharfe (``).
    Suggested-by: Jeff King (peff).
    (Merged by Junio C Hamano -- gitster -- in commit 62b1cb7, 25 Sep 2017)

    archive: don't add empty directories to archives

    While git doesn't track empty directories, git archive can be tricked into putting some into archives.
    While that is supported by the object database, it can't be represented in the index and thus it's unlikely to occur in the wild.

    As empty directories are not supported by git, they should also not be written into archives.
    If an empty directory is really needed then it can be tracked and archived by placing an empty .gitignore file in it.

提交回复
热议问题