Git Clone:只是文件,好吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3946538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:17:28  来源:igfitidea点击:

Git Clone: Just the files, please?

git

提问by Dan Rosenstark

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

我想克隆一个 GIT 仓库而不是一个.git目录。换句话说,我只想要文件。有没有办法做到这一点?

git clone --no-checkoutdid the exact opposite of what I want (gave me just the .gitdirectory).

git clone --no-checkout做了与我想要的完全相反的事情(只给了我.git目录)。

I am trying to do that for a remoterepo, not a local one, meaning this is not a duplicateof "How to do a “git export” (like “svn export”)" (even though the solution might end up being the same).

我正在尝试为远程存储库而不是本地存储库执行此操作,这意味着这不是如何执行“git 导出”(如“svn 导出”)“(即使解决方案可能最终成为相同的)。

采纳答案by VonC

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-allscript)

与您要查找的内容最接近的 git 命令将通过git archive.
请参阅备份使用 git 的项目:它将在存档中包含所有文件(如果您正在使用git-archive-all脚本,则包括子模块)

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

然后您可以在任何地方使用该存档,只返回文件,没有.git目录。

git archive --remote=<repository URL> | tar -t

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

如果您只需要第一级的文件夹和文件:

git archive --remote=<repository URL> | tar -t --exclude="*/*"

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

仅列出远程仓库的一级文件夹:

git archive --remote=<repository URL> | tar -t --exclude="*/*" | grep "/"

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

注意:这不适用于 GitHub(不支持)

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

因此,您需要克隆(浅层以加快克隆步骤),然后在本地存档

git clone --depth=1 [email protected]: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 --git-dir=/path/to/another/folder.git clone --depth=1 /url/to/repo

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

repo 文件夹将只包含文件,不包含.git.

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

注意:git --git-dir命令的git一个选项,而不是git clone



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

使用 Git 2.14.X/2.15(2017 年第四季度)更新:它将确保避免添加空文件夹

"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.

" git archive",尤其是与 pathspec 一起使用时,在其输出中存储了一个空目录,即使 Git 本身从不这样做。
这已被修复。

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)

请参阅René Scharfe (``) 的提交 4318094(2017 年 9 月 12 日
推荐人:Jeff King ( peff)
(由Junio C gitsterHamano合并-- --提交 62b1cb7 中,2017 年 9 月 25 日)

archive: don't add empty directories to archives

While git doesn't track empty directories, git archivecan 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 .gitignorefile in it.

archive: 不要向档案添加空目录

虽然 git 不跟踪空目录,但git archive可以被欺骗将一些目录放入档案中。
虽然对象数据库支持它,但它不能在索引中表示,因此它不太可能在野外发生。

由于 git 不支持空目录,因此也不应将它们写入档案。
如果确实需要一个空目录,则可以通过在其中放置一个空.gitignore文件来对其进行跟踪和存档。

回答by Jon Penn

git archive --format=tar --remote=<repository URL> HEAD | tar xf -

taken from here

取自这里

回答by knittl

you can create a shallow clone to only get the last few revisions:

您可以创建一个浅克隆以仅获取最后几个修订版:

 git clone --depth 1 git://url

then either simply delete the .git directory or use git archiveto export your tree.

然后简单地删除 .git 目录或用于git archive导出您的树。

回答by Andrew

Why not perform a clone and then delete the .gitdirectory so that you just have a bare working copy?

为什么不执行克隆然后删除.git目录,以便您只有一个裸露的工作副本?

Edit: Or in fact why use clone at all? It's a bit confusing when you say that you want a git repo but without a .gitdirectory. If you mean that you just want a copy of some state of the tree then why not do cp -Rin the shell instead of the git clone and then delete the .gitafterwards.

编辑:或者实际上为什么要使用克隆?当你说你想要一个 git repo 但没有.git目录时,这有点令人困惑。如果您的意思是您只想要树的某个状态的副本,那么为什么不在cp -Rshell 中而不是在 git clone 中进行操作,然后再删除它.git

回答by JaredPar

It sounds like you just want a copy of the source code. If so why not just copy the directory and exclude the .git directory from the copy?

听起来您只想要一份源代码的副本。如果是这样,为什么不直接复制目录并从副本中排除 .git 目录?

回答by Slam

git checkout -f

git 结帐 -f

There's another way to do this by splitting the repo from the working tree.

还有另一种方法可以通过从工作树中拆分 repo 来做到这一点。

This method is useful if you need to update these git-less git files on a regular basis. For instance, I use it when I need to check out source files and build an artifact, then copy the artifact into a different repo just for deployment to a server, and I also use it when pushing source code to a server when I want the source code to checkout and build into the www directory.

如果您需要定期更新这些 git-less git 文件,则此方法很有用。例如,当我需要检出源文件并构建工件时,我会使用它,然后将工件复制到不同的存储库中仅用于部署到服务器,并且在将源代码推送到服务器时,我也会使用它。源代码签出并构建到 www 目录中。

We'll make two folders, one for the git one for the working files:

我们将创建两个文件夹,一个用于 git 一个用于工作文件:

mkdir workingfiles
mkdir barerepo.git

initialize a bare git repo:

初始化一个裸 git 仓库:

cd barerepo.git
git --bare init 

Then create a post-receive hook:

然后创建一个 post-receive 钩子:

touch hooks/post-receive
chmod ug+x hooks/post-receive

Edit post-receive in your favorite editor:

在您最喜欢的编辑器中编辑接收后:

GIT_WORK_TREE=/path/to/workingfiles git checkout -f
# optional stuff:
cd down/to/some/directory
[do some stuff]

Add this as a remote:

将此添加为远程:

git remote add myserver ssh://user@host:/path/to/barerepo.git

Now every time you push to this bare repo it will checkout the working tree to /workingfiles/. But /workingfiles/itself is not under version control; running git statusin /workingfiles/will give the error fatal: Not a git repository (or any parent up to mount point /data). It's just plain files.

现在,每次推送到这个裸仓库时,它都会将工作树检出到/workingfiles/. 但/workingfiles/它本身不受版本控制;跑git status进去/workingfiles/会报错fatal: Not a git repository (or any parent up to mount point /data)。这只是普通文件。

Unlike other solutions rm -r .gitcommand is not needed, so if /workingfiles/is some other git repo you don't have to worry about the command used removing the other repo's git files.

与其他解决方案不同,rm -r .git不需要命令,所以如果/workingfiles/是其他一些 git repo,您​​不必担心用于删除其他 repo 的 git 文件的命令。

回答by edam

Very simple IMHO:

非常简单的恕我直言:

  1. Just git clone REPO_URLit then
  2. Just remove the .gitdir using rm -rf ./REPO_NAME/.gitfrom the repo.
  1. git clone REPO_URL这样
  2. 只需从回购中删除.git目录 using rm -rf ./REPO_NAME/.git

For example, if my repository's name is MyRepothen to get this repo without metadata I could run either:

例如,如果我的存储库的名称是MyRepo在没有元数据的情况下获取此存储库,我可以运行:

git clone https://github.com/MyUser/MyRepo.git && rm -rf ./MyRepo/.git

Or I could get it into a specified directory ( let's say in a dir called MyDir):

或者我可以把它放到一个指定的目录中(假设在一个名为 的目录中MyDir):

git clone https://github.com/MyUser/MyRepo.git MyDir && rm -rf ./MyDir/.git

Hopefully this helps.

希望这会有所帮助。