我一直在想,是否有一个好的“gitexport”解决方案可以创建一个没有.git存储库目录的树的副本。我知道至少有三种方法:

git克隆,然后删除.git存储库目录。git签出索引暗示了这一功能,但以“只需将所需的树读入索引……”开头,我不完全确定该怎么做。gitexport是一个第三方脚本,本质上是将git克隆到临时位置,然后再将rsync--exclude='.git'复制到最终目标。

这些解决方案中没有一个让我感到满意。最接近svn导出的可能是选项1,因为两者都要求目标目录首先为空。但选项2似乎更好,假设我能够理解将树读入索引意味着什么。


当前回答

从Git手册:

使用git签出索引“导出整个树”

前缀功能基本上使使用git签出索引作为“导出为树”函数变得简单。只需将所需的树读入索引,然后执行以下操作:

$git签出索引--prefix=git导出目录/-a

其他回答

这将复制除.dot文件之外的所有内容。我使用这个将git克隆项目导出到我的web应用程序的git repo中,而不使用.git文件。

cp-R/git repo/path/to/destination的路径/

简单的旧bash非常有用:)

Bash实现git导出。

我已经将.empty文件的创建和删除过程划分为各自的功能,目的是在“gitarchive”实现中重新使用它们(稍后将发布)。

我还将“.gitattributes”文件添加到进程中,以便从目标导出文件夹中删除不需要的文件。在使“gitexport”功能更高效的同时,还包含了对流程的详细描述。

EMPTY_FILE=“.EMPTY”;

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;
  }

输出:$git导出/tmp/rel-1.0正在将“.empty”文件添加到空文件夹:。。。完成。正在签出索引组件:。。。完成。正在重置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/{*~}”文件。。。完成。”“/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管理轮25445901 3 11月12:57/tmp/rel-1.0.tgz我现在已经将“gitarchive”功能合并到一个使用“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;
  }

在添加前缀(例如目录名)时将git导出到zip存档:

git archive master --prefix=directoryWithinZip/  --format=zip -o out.zip

正如我所理解的那样,它更多的是从服务器上下载特定的状态,没有历史记录,也没有其他分支的数据,而不是从本地存储库中提取状态(就像这里的许多用户一样)。

可以这样做:

git clone -b someBranch --depth 1 --single-branch git://somewhere.com/repo.git \
&& rm -rf repo/.git/

--自Git 1.7.10(2012年4月)以来,单一分支可用。--据报道,深度有问题,但就出口而言,上述问题应该无关紧要。

我只是想指出,如果你是

导出存储库的子文件夹(这就是我使用SVN导出功能的方式)可以将该文件夹中的所有内容复制到部署目标因为您已经有了整个存储库的副本。

然后,您可以只使用cpfoo[destination]而不是前面提到的gitarchive-master foo|-x-C[destination。