Gre*_*ill 2312 git export svn-export git-archive
我一直在想是否有一个很好的"git export"解决方案,它创建了一个没有.git存储库目录的树的副本.我知道至少有三种方法:
git clone然后删除.git存储库目录.git checkout-index 提到这个功能,但开头是"只需将所需的树读入索引...",我不完全确定如何做.git-export是第三方脚本,基本上git clone进入临时位置,然后rsync --exclude='.git'进入最终目的地.这些解决方案都没有让我感到满意.最接近的一个svn export选项可能是选项1,因为这两个都要求目标目录首先为空.但是假设我可以弄清楚将树读入索引意味着什么,选项2似乎更好.
CB *_*ley 2354
实现这一目标的最简单方法可能就是git archive.如果你真的需要扩展树,你可以做这样的事情.
git archive master | tar -x -C /somewhere/else
Run Code Online (Sandbox Code Playgroud)
大多数时候我需要从git"导出"某些东西,我想要一个压缩存档,所以我做这样的事情.
git archive master | bzip2 >source-tree.tar.bz2
Run Code Online (Sandbox Code Playgroud)
ZIP存档:
git archive --format zip --output /full/path/to/zipfile.zip master
Run Code Online (Sandbox Code Playgroud)
git help archive 有关详细信息,它非常灵活.
请注意,即使存档不包含.git目录,它也会包含其他隐藏的git特定文件,如.gitignore,.gitattributes等.如果您不想在存档中使用它们,请确保使用.gitattributes文件中的export-ignore属性并在进行归档之前提交此属性.阅读更多...
注意:如果您对导出索引感兴趣,则命令为
git checkout-index -a -f --prefix=/destination/path/
Run Code Online (Sandbox Code Playgroud)
(有关详细信息,请参阅格雷格的答案)
Gre*_*ill 316
我发现了2选项意味着什么.从存储库中,您可以执行以下操作:
git checkout-index -a -f --prefix=/destination/path/
Run Code Online (Sandbox Code Playgroud)
路径末尾的斜杠很重要,否则会导致文件位于/ destination中,前缀为"path".
由于在正常情况下索引包含存储库的内容,因此"将所需的树读入索引"没有什么特别之处.它已经存在了.
该-a标志需要检出索引中的所有文件(我不确定在这种情况下省略此标志意味着什么,因为它不能做我想要的).该-f标志强制覆盖输出中的任何现有文件,此命令通常不会执行此操作.
这似乎是我正在寻找的那种"git export".
小智 253
git archive 也适用于远程存储库.
git archive --format=tar \
--remote=ssh://remote_server/remote_repository master | tar -xf -
Run Code Online (Sandbox Code Playgroud)
要导出repo中的特定路径,请添加尽可能多的路径作为git的最后一个参数,例如:
git archive --format=tar \
--remote=ssh://remote_server/remote_repository master path1/ path2/ | tar -xv
Run Code Online (Sandbox Code Playgroud)
Ant*_*los 57

只是用svn export.
As far as I know Github does not allow archive --remote. Although GitHub is svn compatible and 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.
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):
svn export https://github.com/username/repo-name/trunk/
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
The HEAD branch or master branch will be available using trunk:
svn ls https://github.com/jquery/jquery/trunk
Run Code Online (Sandbox Code Playgroud)
The non-HEAD branches will be accessible under /branches/:
svn ls https://github.com/jquery/jquery/branches/2.1-stable
Run Code Online (Sandbox Code Playgroud)
All tags under /tags/ in the same fashion:
svn ls https://github.com/jquery/jquery/tags/2.1.3
Run Code Online (Sandbox Code Playgroud)
jpe*_*ras 41
从Git手册:
使用git-checkout-index"导出整个树"
前缀能力基本上使得git-checkout-index用作"export as tree"函数变得微不足道.只需将所需的树读入索引,然后执行:
$ git checkout-index --prefix=git-export-dir/ -a
Dan*_*eck 38
我写了一个简单的包装器git-checkout-index,你可以像这样使用:
git export ~/the/destination/dir
Run Code Online (Sandbox Code Playgroud)
如果目标目录已存在,则需要添加-f或--force.
安装简单; 只需将脚本放在您的某个位置PATH,并确保它是可执行的.
kos*_*tmo 36
对于Git而言,这似乎不是SVN的问题.Git只在存储库根目录中放置一个.git文件夹,而SVN在每个子目录中放置一个.svn文件夹.所以"svn export"避免了递归命令行魔术,而Git递归则没有必要.
are*_*del 27
相当于
svn export . otherpath
Run Code Online (Sandbox Code Playgroud)
在现有的回购中是
git archive branchname | (cd otherpath; tar x)
Run Code Online (Sandbox Code Playgroud)
相当于
svn export url otherpath
Run Code Online (Sandbox Code Playgroud)
是
git archive --remote=url branchname | (cd otherpath; tar x)
Run Code Online (Sandbox Code Playgroud)
小智 22
如果您没有排除文件,请.gitattributes export-ignore尝试git checkout
mkdir /path/to/checkout/
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout -f -q
Run Code Online (Sandbox Code Playgroud)
-f
从索引中检出路径时,在未合并的条目时不要失败; 相反,未合并的条目将被忽略.
和
-q
避免冗长
此外,您可以获取任何分支或标记,或者从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 -- ./
Run Code Online (Sandbox Code Playgroud)
的/path/to/checkout/必须是空的,Git会不会删除任何文件,但将覆盖具有相同名称的文件没有任何警告
更新:为了避免斩首问题或在使用checkout导出标签,分支或SHA1时保持工作存储库完好无损,您需要-- ./在最后添加
双短划线--告诉git,破折号后的所有内容都是路径或文件,并且在这种情况下告诉git checkout不要更改HEAD
例子:
此命令将只获取libs目录以及readme.txt完全提交的文件
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout fef2e1f2de5f3d4f5e87df7d8 -f -q -- ./libs ./docs/readme.txt
Run Code Online (Sandbox Code Playgroud)
这将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
Run Code Online (Sandbox Code Playgroud)
获得另一个分支的导出
git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout myotherbranch -f -q -- ./
Run Code Online (Sandbox Code Playgroud)
请注意,它./是相对于存储库的根目录的
sla*_*ick 21
我广泛使用git-submodules.这个对我有用:
rsync -a ./FROM/ ./TO --exclude='.*'
Run Code Online (Sandbox Code Playgroud)
Lar*_*ann 19
在寻找导出git存储库的方法时,我经常点击此页面.我对这个问题的回答考虑了svn export与git相比设计的三个属性,因为svn遵循集中式存储库方法:
使用svn导出某个分支是通过指定适当的路径来完成的
git clone --depth 1 --branch master git://git.somewhere destination_path
rm -rf destination_path/.git
Run Code Online (Sandbox Code Playgroud)在构建某个版本时,克隆一个稳定的分支是很有用的,例如--branch stable或--branch release/0.9.
Har*_*mon 16
这将复制所有内容,减去.dot文件.我使用它来将git克隆的项目导出到我的web应用程序的git repo而没有.git的东西.
cp -R ./path-to-git-repo/path/to/destination /
普通的老bash工作得很好:)
tel*_*.io 13
就像克隆一样简单,然后删除.git文件夹:
git clone url_of_your_repo path_to_export && rm -rf path_to_export/.git
zee*_*wan 11
是的,这是一个干净利落的命令,用于存档您的代码而不会在存档中包含任何git,并且可以传递而不必担心任何git提交历史记录.
git archive --format zip --output /full/path/to/zipfile.zip master
Run Code Online (Sandbox Code Playgroud)
dki*_*zer 10
我只想指出你的情况
然后你可以使用cp foo [destination]而不是提到的git-archive master foo | -x -C [destination].
bis*_*hop 10
对于GitHub用户,该git archive --remote方法不能直接使用,因为导出URL是短暂的.您必须向GitHub询问URL,然后下载该URL. curl这么简单:
curl -L https://api.github.com/repos/VENDOR/PROJECT/tarball | tar xzf -
Run Code Online (Sandbox Code Playgroud)
这将为您提供本地目录中的导出代码.例:
$ curl -L https://api.github.com/repos/jpic/bashworks/tarball | tar xzf -
$ ls jpic-bashworks-34f4441/
break conf docs hack LICENSE mlog module mpd mtests os README.rst remote todo vcs vps wepcrack
Run Code Online (Sandbox Code Playgroud)
编辑
如果您希望将代码放入特定的现有目录(而不是github中的随机目录):
curl -L https://api.github.com/repos/VENDOR/PROJECT/tarball | \
tar xzC /path/you/want --strip 1
Run Code Online (Sandbox Code Playgroud)
您可以将任何提交的远程仓库归档为zip文件.
git archive --format=zip --output=archive.zip --remote=USERNAME@HOSTNAME:PROJECTNAME.git HASHOFGITCOMMIT
Run Code Online (Sandbox Code Playgroud)
Bash实现git-export.
我已经在自己的函数上对.empty文件创建和删除过程进行了细分,目的是在'git-archive'实现中重用它们(稍后将发布).
我还将'.gitattributes'文件添加到进程中,以便从目标导出文件夹中删除不需要的文件.在使'git-export'功能更有效的同时包含了流程的冗长.
"空" EMPTY_FILE =;
function create_empty () {
## Processing path (target-dir):
TRG_PATH="${1}";
## Component(s):
EXCLUDE_DIR=".git";
echo -en "\nAdding '${EMPTY_FILE}' files to empty folder(s): ...";
find ${TRG_PATH} -not -path "*/${EXCLUDE_DIR}/*" -type d -empty -exec touch {}/${EMPTY_FILE} \;
#echo "done.";
## Purging SRC/TRG_DIRs variable(s):
unset TRG_PATH EMPTY_FILE EXCLUDE_DIR;
return 0;
}
declare -a GIT_EXCLUDE;
function load_exclude () {
SRC_PATH="${1}";
ITEMS=0; while read LINE; do
# echo -e "Line [${ITEMS}]: '${LINE%%\ *}'";
GIT_EXCLUDE[((ITEMS++))]=${LINE%%\ *};
done < ${SRC_PATH}/.gitattributes;
GIT_EXCLUDE[${ITEMS}]="${EMPTY_FILE}";
## Purging variable(s):
unset SRC_PATH ITEMS;
return 0;
}
function purge_empty () {
## Processing path (Source/Target-dir):
SRC_PATH="${1}";
TRG_PATH="${2}";
echo -e "\nPurging Git-Specific component(s): ... ";
find ${SRC_PATH} -type f -name ${EMPTY_FILE} -exec /bin/rm '{}' \;
for xRULE in ${GIT_EXCLUDE[@]}; do
echo -en " '${TRG_PATH}/{${xRULE}}' files ... ";
find ${TRG_PATH} -type f -name "${xRULE}" -exec /bin/rm -rf '{}' \;
echo "done.'";
done;
echo -e "done.\n"
## Purging SRC/TRG_PATHs variable(s):
unset SRC_PATH; unset TRG_PATH;
return 0;
}
function git-export () {
TRG_DIR="${1}"; SRC_DIR="${2}";
if [ -z "${SRC_DIR}" ]; then SRC_DIR="${PWD}"; fi
load_exclude "${SRC_DIR}";
## Dynamically added '.empty' files to the Git-Structure:
create_empty "${SRC_DIR}";
GIT_COMMIT="Including '${EMPTY_FILE}' files into Git-Index container."; #echo -e "\n${GIT_COMMIT}";
git add .; git commit --quiet --all --verbose --message "${GIT_COMMIT}";
if [ "${?}" -eq 0 ]; then echo " done."; fi
/bin/rm -rf ${TRG_DIR} && mkdir -p "${TRG_DIR}";
echo -en "\nChecking-Out Index component(s): ... ";
git checkout-index --prefix=${TRG_DIR}/ -q -f -a
## Reset: --mixed = reset HEAD and index:
if [ "${?}" -eq 0 ]; then
echo "done."; echo -en "Resetting HEAD and Index: ... ";
git reset --soft HEAD^;
if [ "${?}" -eq 0 ]; then
echo "done.";
## Purging Git-specific components and '.empty' files from Target-Dir:
purge_empty "${SRC_DIR}" "${TRG_DIR}"
else echo "failed.";
fi
## Archiving exported-content:
echo -en "Archiving Checked-Out component(s): ... ";
if [ -f "${TRG_DIR}.tgz" ]; then /bin/rm ${TRG_DIR}.tgz; fi
cd ${TRG_DIR} && tar -czf ${TRG_DIR}.tgz ./; cd ${SRC_DIR}
echo "done.";
## Listing *.tgz file attributes:
## Warning: Un-TAR this file to a specific directory:
ls -al ${TRG_DIR}.tgz
else echo "failed.";
fi
## Purgin all references to Un-Staged File(s):
git reset HEAD;
## Purging SRC/TRG_DIRs variable(s):
unset SRC_DIR; unset TRG_DIR;
echo "";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ git-export /tmp/rel-1.0.0
将'.empty'文件添加到空文件夹:...完成.
Check-Out Index组件:...已完成.
重置HEAD和索引:......完成.
清除Git特定组件:...
'/tmp/rel-1.0.0/{.buildpath}'文件......完成了.'
'/tmp/rel-1.0.0/{.project}'文件......完成了.'
'/tmp/rel-1.0.0/{.gitignore}'文件......完成了.'
'/tmp/rel-1.0.0/{.git}'文件......完成了.'
'/tmp/rel-1.0.0/{.gitattributes}'文件......完成了.'
'/tmp/rel-1.0.0/{*.mno}'文件......完成了.'
'/tmp/rel-1.0.0/ {*〜}'文件......完成了.'
'/tmp/rel-1.0.0/{.*〜}'文件......完成了.'
'/tmp/rel-1.0.0/{*.swp}'文件......完成了.'
'/tmp/rel-1.0.0/{*.swo}'文件......完成了.'
'/tmp/rel-1.0.0/{.DS_Store}'文件......完成了.'
'/tmp/rel-1.0.0/{.settings}'文件......完成了.'
'/tmp/rel-1.0.0/{.empty}'文件......完成了.'
完成.
归档已检出组件:......已完成.
-rw-r - r-- 1 admin wheel 25445901 3月12日12:57 /tmp/rel-1.0.0.tgz
我现在已将'git archive'功能合并到一个使用'create_empty'功能和其他功能的进程中.
function git-archive () {
PREFIX="${1}"; ## sudo mkdir -p ${PREFIX}
REPO_PATH="`echo "${2}"|awk -F: '{print $1}'`";
RELEASE="`echo "${2}"|awk -F: '{print $2}'`";
USER_PATH="${PWD}";
echo "$PREFIX $REPO_PATH $RELEASE $USER_PATH";
## Dynamically added '.empty' files to the Git-Structure:
cd "${REPO_PATH}"; populate_empty .; echo -en "\n";
# git archive --prefix=git-1.4.0/ -o git-1.4.0.tar.gz v1.4.0
# e.g.: git-archive /var/www/htdocs /repos/domain.name/website:rel-1.0.0 --explode
OUTPUT_FILE="${USER_PATH}/${RELEASE}.tar.gz";
git archive --verbose --prefix=${PREFIX}/ -o ${OUTPUT_FILE} ${RELEASE}
cd "${USER_PATH}";
if [[ "${3}" =~ [--explode] ]]; then
if [ -d "./${RELEASE}" ]; then /bin/rm -rf "./${RELEASE}"; fi
mkdir -p ./${RELEASE}; tar -xzf "${OUTPUT_FILE}" -C ./${RELEASE}
fi
## Purging SRC/TRG_DIRs variable(s):
unset PREFIX REPO_PATH RELEASE USER_PATH OUTPUT_FILE;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 8
如果你想要一些适用于子模块的东西,这可能是值得的.
注意:
假设:
cd MASTER_DIR && tar -zcvf ../DEST_DIR/export.tar.gz --exclude ='.git*'.&& cd ../DEST_DIR/&& tar xvfz export.tar.gz && rm export.tar.gz
git 导出到 zip 存档,同时添加前缀(例如目录名称):
git archive master --prefix=directoryWithinZip/ --format=zip -o out.zip
Run Code Online (Sandbox Code Playgroud)
我的偏好实际上是在Makefile(或其他构建系统)中有一个dist目标,它导出代码的可分发存档(.tar.bz2,.zip,.jar或任何合适的东西).如果您碰巧使用GNU autotools或Perl的MakeMaker系统,我认为这是自动存在的.如果没有,我强烈建议添加它.
ETA(2012-09-06):哇,苛刻的downvotes.我仍然认为使用构建工具而不是源代码控制工具构建发行版会更好.我相信使用构建工具构建工件.在我目前的工作中,我们的主要产品是用蚂蚁目标建造的.我们正在切换源代码控制系统,并且这个蚂蚁目标的存在意味着迁移中的麻烦减少了.
小智 6
这会将提交范围(C到G)中的文件复制到tar文件中.注意:这只会提交文件.不是整个存储库.从这里略微修改
提交历史示例
A - > B - > C - > D - > E - > F - > G - > H - > I
git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT C~..G | xargs tar -rf myTarFile.tar
Run Code Online (Sandbox Code Playgroud)
-r - >递归到子树
--no-commit-id - > git diff-tree在适用时输出带有提交ID的行.此标志禁止提交ID输出.
--name-only - >仅显示已更改文件的名称.
--diff-filter = ACMRT - >仅选择这些文件.请参阅此处获取完整的文件列表
C..G - >此提交范围内的文件
C~ - >包含来自Commit C的文件.不仅仅是自Commit C以来的文件.
| xargs tar -rf myTarFile - >输出到tar
到目前为止,我见过的最简单的方法(也适用于 Windows)是git bundle:
git bundle create /some/bundle/path.bundle --all
有关更多详细信息,请参阅此答案:如何通过 USB 驱动器将我的 git 存储库从 Windows 机器复制到 linux 机器?
就我所知,这个问题更多的是关于从服务器上仅下载某些状态,没有历史记录,也没有其他分支的数据,而不是从本地存储库中提取状态(就像这里的许多辅助工具一样)。
可以这样完成:
git clone -b someBranch --depth 1 --single-branch git://somewhere.com/repo.git \
&& rm -rf repo/.git/
Run Code Online (Sandbox Code Playgroud)
--single-branch 自Git 1.7.10(2012年4月)起可用。--depth(是?)有问题,但对于出口而言,上述问题无关紧要。| 归档时间: |
|
| 查看次数: |
615824 次 |
| 最近记录: |