我需要创建 Git 项目的存档,忽略并省略
.gitignore
指定的文件,但包括实际的 .git
存储库文件夹。只需运行 git archive master
即可忽略 .git
文件夹。
有没有办法让
git archive
包含.git
文件夹但仍然忽略.gitignore
指定的文件?
由于
git archive
仅生成 tar 存档,因此您可以直接使用 tar
处理该文件。
$ git archive HEAD > tar.tar
$ tar -rf tar.tar .git
tar 的
-r
选项将给定的文件附加到正在处理的存档(在 -f
之后指定)。检查 tar 的手册页,了解 tar 具有的令人生畏的功能列表。
看起来像做类似的事情
# copy over all project files except for those ignored
git clone /path/to/repository /path/to/output
# create a tar.gz archive of the project location
tar czvf projectExport.tar.gz /path/to/output
# remove the cloned directory
rm -fr /path/to/output
完成工作。这不是世界上最漂亮的解决方案,但看起来很有效。
git bundle
是一个较新的命令(自 git 1.5 起,于 2007 年发布),它允许创建与远程计算机共享的存档。 git bundle
更灵活,因为它允许增量更新和包含不同的分支。 但是,您可以创建使用单个完整分支的捆绑包,例如 master
。
基本用法如下:
$ git bundle create mybundle master
现在您可以将捆绑包移动到您想要解压的位置:
$ scp mybundle user@host:~/mybundle
并在该主机上使用
git clone
将捆绑包转回存储库:
$ ssh user@host
user@host $ git clone mybundle myrepo
一个缺点是远程机器也需要 git。 该捆绑包是一个“打包”文件,因此需要从捆绑包中“检出”文件以再次创建工作目录(而不是 tar)。
tar && git
和!tar && !git
的机器组比tar && !git
和!tar && git
大得多。 也就是说,机器可能两者都有,也可能两者都没有。 特别是当您打算构建源代码时。
对于 Windows 机器,可能两者都没有。 您可以使用“zip”形式。
git archive -o ../archive.zip --format=zip HEAD
zip -ur ../archive.zip .git/
..我向你表示哀悼。