1

让我们调用my-dirty-repository一个现有的 Git 存储库,其中包含许多不相关的脚本。这是一个需要适当清理的包罗万象的存储库。

作为一个最小、完整和可验证的示例,假设此存储库仅包含:

script1.sh
script2.sh

在多个分支之间进行各种独立更新的提交。

目的是创建 2 100% 独立的 Git 存储库,只有保留文件的历史记录(参考)。

我们称它们为my-clean-repository1and my-clean-repository2,第一个只有关于 script1 的历史,第二个只有关于 script2 的历史。

我尝试了 3 种方法来满足我的需求,但没有成功:

我很确定有一种方法可以正确执行它。

4

1 回答 1

0

编辑:我创建了专用工具cloneToCleanGitRepositories来满足这一需求。

它是以下旧版本的完整版本。


@mkasberg 感谢您对交互式 rebase 的建议,这在一些简单的历史情况下非常有趣。

我试过了,它解决了我想要一个干净的专用、独立的 git 存储库的一些脚本的问题。

最终,这对他们中的大多数人来说还不够,我再次尝试了另一个使用Git 过滤系统的解决方案。

最后,我写了这个小脚本:

#!/bin/bash
##
## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
## Description: Create clean git repositories for each file in root of specified source Git repository, updating history consequently. 
## Version: 1.0

[ $# -lt 2 ] && echo -e "Usage: $0 <source repository> <dest root directory>" >&2 && exit 1

SOURCE_REPO="$1"
[ ! -d "$SOURCE_REPO" ] && echo -e "Specified source Git repository '$SOURCE_REPO' does not exist." >&2 && exit 1
DEST_ROOT_DIR="$2"
[ ! -d "$DEST_ROOT_DIR" ] && echo -e "Specified destination root directory '$DEST_ROOT_DIR' does not exist." >&2 && exit 1

sourceRepoName=$( basename "$SOURCE_REPO" )

# For each file in root of the source git repository.
for refToManage in $( find "$SOURCE_REPO" -maxdepth 1 -type f ); do
  echo -ne "Managing $refToManage ... "

  refFileName=$( basename "$refToManage" )
  newDestRepo="$DEST_ROOT_DIR/$refFileName"

  # Creates the repository if not existing.
  logFile="$newDestRepo/logFile.txt"
  echo -ne "creating new repository: $newDestRepo, Log file: $logFile ... "
  if [ ! -d "$newDestRepo" ]; then
    mkdir -p "$newDestRepo"
    cd "$newDestRepo"
    ! git clone -q "$SOURCE_REPO" && echo -e "Error while cloning source repository to $newDestRepo." >&2 && exit 2
  fi
  cd "$newDestRepo/$sourceRepoName"

  # Removes all other resources.
  FILTER='git ls-tree -r --name-only --full-tree "$GIT_COMMIT" | grep -v "'$refFileName'" | tr "\n" "\0" | xargs -0 git rm -f --cached -r --ignore-unmatch'
  ! git filter-branch -f --prune-empty --index-filter "$FILTER" -- --all >"$logFile" 2>&1 && echo -e "Error while cleaning new git repository." >&2 && exit 3

  # Cleans remote information to ensure there is no push to the source repository.
  ! git remote remove origin >>"$logFile" 2>&1 && echo -e "Error while removing remote." >&2 && exit 2

  echo "done"
done

用法 :

mkdir /tmp/cleanRepoDest
createCleanGitRepo.sh ~/_gitRepo/Scripts /tmp/cleanRepoDest

在目标目录中,它将在指定源 Git 存储库的根目录中为每个文件创建一个新的干净 git 存储库。在每一个中,历史都是干净的,并且只与保留的脚本有关。

此外,它断开/删除远程以确保避免将更改推回源存储库的问题。

这样,很容易从一个大而肮脏的包罗万象的 Git 存储库“迁移”到各种干净的 :-)

于 2018-11-22T14:36:47.253 回答