做一个“git export”(比如“svn export”)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160608/
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
Do a "git export" (like "svn export")?
提问by Greg Hewgill
I've been wondering whether there is a good "git export" solution that creates a copy of a tree without the .git
repository directory. There are at least three methods I know of:
我一直想知道是否有一个好的“git export”解决方案可以创建一个没有.git
存储库目录的树的副本。我知道的方法至少有以下三种:
git clone
followed by removing the.git
repository directory.git checkout-index
alludes to this functionality but starts with "Just read the desired tree into the index..." which I'm not entirely sure how to do.git-export
is a third party script that essentially does agit clone
into a temporary location followed byrsync --exclude='.git'
into the final destination.
git clone
然后删除.git
存储库目录。git checkout-index
暗指此功能,但以“只需将所需的树读入索引...”开头,我不完全确定该怎么做。git-export
是一个第三方脚本,它本质上是先git clone
进入一个临时位置,然后rsync --exclude='.git'
进入最终目的地。
None of these solutions really strike me as being satisfactory. The closest one to svn export
might be option 1, because both those require the target directory to be empty first. But option 2 seems even better, assuming I can figure out what it means to read a tree into the index.
这些解决方案都没有让我感到满意。最接近的svn export
可能是选项 1,因为两者都要求目标目录首先为空。但是选项 2 似乎更好,假设我可以弄清楚将树读入索引意味着什么。
回答by CB Bailey
Probably the simplest way to achieve this is with git archive
. If you really need just the expanded tree you can do something like this.
实现这一目标的最简单方法可能是使用git archive
. 如果你真的只需要扩展的树,你可以做这样的事情。
git archive master | tar -x -C /somewhere/else
Most of the time that I need to 'export' something from git, I want a compressed archive in any case so I do something like this.
大多数时候我需要从 git 中“导出”一些东西,无论如何我都想要一个压缩的存档,所以我做这样的事情。
git archive master | bzip2 >source-tree.tar.bz2
ZIP archive:
ZIP 存档:
git archive --format zip --output /full/path/to/zipfile.zip master
git help archive
for more details, it's quite flexible.
git help archive
有关更多详细信息,它非常灵活。
Be aware that even though the archive will not contain the .git directory, it will, however, contain other hidden git-specific files like .gitignore, .gitattributes, etc. If you don't want them in the archive, make sure you use the export-ignore attribute in a .gitattributes file and commit this before doing your archive. Read more...
请注意,即使存档不包含 .git 目录,它也会包含其他隐藏的 git 特定文件,如 .gitignore、.gitattributes 等。如果您不希望它们在存档中,请确保您在 .gitattributes 文件中使用 export-ignore 属性并在进行存档之前提交。阅读更多...
Note: If you are interested in exporting the index, the command is
注意:如果你有兴趣导出索引,命令是
git checkout-index -a -f --prefix=/destination/path/
(See Greg's answerfor more details)
(有关详细信息,请参阅Greg 的回答)
回答by Greg Hewgill
I found out what option 2 means. From a repository, you can do:
我发现了选项 2 的含义。从存储库,您可以执行以下操作:
git checkout-index -a -f --prefix=/destination/path/
The slash at the end of the path is important, otherwise it will result in the files being in /destination with a prefix of 'path'.
路径末尾的斜杠很重要,否则会导致文件位于 /destination 中,前缀为“path”。
Since in a normal situation the index contains the contents of the repository, there is nothing special to do to "read the desired tree into the index". It's already there.
由于在正常情况下索引包含存储库的内容,因此“将所需的树读入索引”没有什么特别的。它已经在那里了。
The -a
flag is required to check out all files in the index (I'm not sure what it means to omit this flag in this situation, since it doesn't do what I want). The -f
flag forces overwriting any existing files in the output, which this command doesn't normally do.
-a
需要该标志来检出索引中的所有文件(我不确定在这种情况下省略这个标志意味着什么,因为它没有做我想要的)。该-f
标志强制覆盖输出中的任何现有文件,该命令通常不会这样做。
This appears to be the sort of "git export" I was looking for.
这似乎是我正在寻找的那种“git export”。
回答by Alexander Somov
git archive
also works with remote repository.
git archive
也适用于远程存储库。
git archive --format=tar \
--remote=ssh://remote_server/remote_repository master | tar -xf -
To export particular path inside the repo add as many paths as you wish as last argument to git, e.g.:
要导出存储库中的特定路径,请添加任意数量的路径作为 git 的最后一个参数,例如:
git archive --format=tar \
--remote=ssh://remote_server/remote_repository master path1/ path2/ | tar -xv
回答by Anthony Hatzopoulos
A special case answer if the repository is hosted on GitHub.
如果存储库托管在 GitHub 上,这是一个特殊情况的答案。
Just use svn export
.
只需使用svn export
.
As far as I know Github does not allow archive --remote
. Although GitHub is svn compatibleand they do have all git repos svn
accessible so you could just use svn export
like you normally would with a few adjustments to your GitHub url.
据我所知 Github 不允许archive --remote
. 尽管 GitHub 与svn 兼容,并且它们确实可以svn
访问所有 git 存储库,因此您可以svn export
像往常一样使用,并对GitHub url 进行一些调整。
For example to export an entire repository, notice how trunk
in the URL replaces master
(or whatever the project's HEAD branch is set to):
例如,要导出整个存储库,请注意trunk
URL 中的替换方式master
(或项目的 HEAD 分支设置为):
svn export https://github.com/username/repo-name/trunk/
And you can export a single file or even a certain path or folder:
您可以导出单个文件甚至某个路径或文件夹:
svn export https://github.com/username/repo-name/trunk/src/lib/folder
Example with jQuery JavaScript Library
jQuery JavaScript 库示例
The HEAD
branch or masterbranch will be available using trunk
:
该HEAD
分支或主分支将可使用trunk
:
svn ls https://github.com/jquery/jquery/trunk
The non-HEAD
brancheswill be accessible under /branches/
:
非HEAD
分支机构将在以下位置访问/branches/
:
svn ls https://github.com/jquery/jquery/branches/2.1-stable
All tagsunder /tags/
in the same fashion:
以相同的方式下的所有标签/tags/
:
svn ls https://github.com/jquery/jquery/tags/2.1.3
回答by jperras
From the Git Manual:
从Git 手册:
Using git-checkout-index to "export an entire tree"
使用 git-checkout-index 来“导出整棵树”
The prefix ability basically makes it trivial to use git-checkout-index as an "export as tree" function. Just read the desired tree into the index, and do:
前缀能力基本上使得使用 git-checkout-index 作为“导出为树”函数变得微不足道。只需将所需的树读入索引,然后执行:
$ git checkout-index --prefix=git-export-dir/ -a
$ git checkout-index --prefix=git-export-dir/ -a
回答by Daniel Schierbeck
I've written a simple wrapper around git-checkout-index
that you can use like this:
我写了一个简单的包装器git-checkout-index
,你可以这样使用:
git export ~/the/destination/dir
If the destination directory already exists, you'll need to add -f
or --force
.
如果目标目录已经存在,则需要添加-f
或--force
。
Installation is simple; just drop the script somewhere in your PATH
, and make sure it's executable.
安装简单;只需将脚本放在 . 中的某个位置PATH
,并确保它是可执行的。
回答by kostmo
It appears that this is less of an issue with Git than SVN. Git only puts a .git folder in the repository root, whereas SVN puts a .svn folder in every subdirectory. So "svn export" avoids recursive command-line magic, whereas with Git recursion is not necessary.
与 SVN 相比,这似乎不是 Git 的问题。Git 只在存储库根目录中放置一个 .git 文件夹,而 SVN 在每个子目录中放置一个 .svn 文件夹。所以“svn export”避免了递归的命令行魔法,而对于 Git 递归则没有必要。
回答by aredridel
The equivalent of
相当于
svn export . otherpath
inside an existing repo is
在现有的回购内是
git archive branchname | (cd otherpath; tar x)
The equivalent of
相当于
svn export url otherpath
is
是
git archive --remote=url branchname | (cd otherpath; tar x)
回答by user5286776117878
If you're not excluding files with .gitattributes
export-ignore
then try git checkout
如果您不排除文件,.gitattributes
export-ignore
然后尝试git checkout
mkdir /path/to/checkout/
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout -f -q
-f
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.
-f
从索引中检出路径时,不要在未合并的条目上失败;相反,未合并的条目将被忽略。
and
和
-q
Avoid verbose
-q
避免冗长
Additionally you can get any Branch or Tag or from a specific Commit Revision like in SVN just adding the SHA1 (SHA1 in Git is the equivalent to the Revision Number in SVN)
此外,您可以获取任何分支或标签或从特定的提交修订版(如 SVN 中的提交修订版),只需添加 SHA1(Git 中的 SHA1 相当于 SVN 中的修订版号)
mkdir /path/to/checkout/
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout 2ef2e1f2de5f3d4f5e87df7d8 -f -q -- ./
The /path/to/checkout/
must be empty, Git will not delete any file, but will overwrite files with the same name without any warning
的/path/to/checkout/
必须是空的,Git会不会删除任何文件,但将覆盖具有相同名称的文件没有任何警告
UPDATE:
To avoid the beheaded problem or to leave intact the working repository when using checkout for export with tags, branches or SHA1, you need to add -- ./
at the end
更新:在使用 checkout 导出标签、分支或 SHA1 时,为了避免斩首问题或保持工作存储库完好无损,您需要-- ./
在最后添加
The double dash --
tells git that everything after the dashes are paths or files, and also in this case tells git checkout
to not change the HEAD
双破折号--
告诉 git 破折号后面的所有内容都是路径或文件,并且在这种情况下也告诉git checkout
不要更改HEAD
Examples:
例子:
This command will get just the libs directory and also the readme.txt
file from that exactly commit
此命令将仅获取 libs 目录以及该readme.txt
提交中的文件
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout fef2e1f2de5f3d4f5e87df7d8 -f -q -- ./libs ./docs/readme.txt
This will create(overwrite) my_file_2_behind_HEAD.txt
two commits behind the head HEAD^2
这将my_file_2_behind_HEAD.txt
在头部后面创建(覆盖)两个提交HEAD^2
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout HEAD^2 -f -q -- ./my_file_2_behind_HEAD.txt
To get the export of another branch
获取另一个分支的导出
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout myotherbranch -f -q -- ./
Notice that ./
is relative to the root of the repository
请注意,./
相对于存储库的根
回答by slatvick
I use git-submodules extensively. This one works for me:
我广泛使用 git-submodules。这个对我有用:
rsync -a ./FROM/ ./TO --exclude='.*'