繁体   English   中英

git 将目录移动到另一个存储库,同时保留历史记录

[英]git move directory to another repository while keeping the history

首先 - 很抱歉提出这个问题。 已经有很多关于它的话题。 但我对他们的运气并不好。 我对 git 的不熟悉并没有多大帮助。

我正在将一个文件夹从一个 git 存储库移动到另一个(已经存在)。 例如

repo-1
---- dir1
---- dir2
---- dir3
---- dir-to-move
---- dir5

repo-2
---- dir1
---- dir2
---- dir3

最后我希望回购看起来像

repo-1
---- dir1
---- dir2
---- dir3
---- dir-to-move
---- dir5

repo-2
---- dir1
---- dir2
---- dir3
---- dir-to-move

即,有一段时间dir-to-move将存在于两个 repos 中。 但最终我会将最新的更改迁移到repo-2并从repo-1中删除dir-to-move

我最初的研究让我相信我需要使用filter-branch 例如

如何使用 `git format-patch` 和 `git am` 将文件从一个 git 存储库移动到另一个保留历史的存储库

从那以后,我了解到子树取代了这种方法。 但是它没有按照我的预期进行。 我以为我可以做类似的事情

repo-1工作区

git subtree split -P dir-to-move -b split

拆分分支过滤为仅dir-to-move及其历史记录。 然后在repo-2工作区

git remote add repo-1 repo-1-url.git
git subtree add --prefix dir-to-move split

这确实移动了代码。 它也有点包括历史

例如

cd repo-2
git log

显示来自repo-1 的提交

cd repo-2
git log dir-to-move

仅显示“从提交中添加要移动的目录....”

即包含历史记录,但在检查特定文件/目录时不会显示。

我怎样才能正确地做到这一点?

我无法帮助您使用git subtree ,但是使用filter-branch是可能的。

首先,您需要创建一个包含源分支和目标分支的公共存储库。 这可以通过在“来源”旁边添加一个新的“远程”并从新的远程获取来完成。

在源filter-branch上使用filter-branchrm -rfdir-to-move之外的所有目录。 之后,您将拥有一个可以干净地重新定位或合并到目标分支的提交历史记录。 我认为最简单的方法是从源分支中cherry-pick所有非空提交。 这些提交的列表可以通过运行git rev-list --reverse source-branch -- dir-to-move

当然,如果dir-to-move的历史是非线性的(已经包含了合并提交),那么cherry-pick 不会保留它,所以可以使用git merge代替。

示例创建通用存储库:

cd repo-2
git remote add source ../repo-1
git fetch source

示例过滤器分支

cd repo-2
git checkout -b source-master source/master
CMD="rm -rf dir1 dir2 dir3 dir5"
git filter-branch --tree-filter "$CMD"

示例cherry-pick 到目标主机

cd repo-2
git checkout master
git cherry-pick `git rev-list --reverse source-master -- dir-to-move`

FWIW,经过多次迭代后,以下内容对我有用。

将两个 repos 克隆到临时工作区。

git clone <repourl>/repo-1.git 
git clone <repourl>/repo-2.git
cd repo-1
git remote rm origin # delete link to original repository to avoid any accidental remote changes
git filter-branch --subdirectory-filter dir-to-move -- --all  # dir-to-move is being moved to another repo.  This command goes through history and files, removing anything that is not in the folder.  The content of this folder will be moved to root of the repo as a result. 
# This folder has to be moved to another folder in the target repo.  So, move everything to another folder.
git filter-branch -f --index-filter \
'git ls-files -s | /usr/local/bin/sed -e "s/\t\"*/&dir-to-move\//" |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
        git update-index --index-info &&
 mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD
# Above command will go through history and rewrite the history by adding dir-to-move/ to each files.  As a result, all files will be moved to a subfolder /dir-to-move.  Make sure to use gnu-sed as OSX sed doesn't handle some extensions correctly.  For eg. \t

现在切换到目标 repo 并从源中获取所有内容。

git clone repo-2
git remote add master ../repo-1/
git pull master master --allow-unrelated-histories
git push  # push everything to remote 

以上步骤假设主分支用于源和目标。 然而,标签和分支被忽略了。

我将dir-to-moverepo 2 首先将repo 1克隆到新位置,然后 cd 到repo 1文件夹。 在我的情况下,我将文件夹从repo 1分支developrepo 2 ,其中develop尚不存在。

git filter-branch --subdirectory-filter dir-to-move -- --all     #line 1
git remote -v                                                    #line 2
git remote set-url origin repo_2_remote_url                      #line 3
git remote -v                                                    #line 4
git push origin develop                                          #line 5

每行说明:

  1. 清除与dir-to-move无关的所有提交,删除该文件夹外的所有文件,移动根文件夹repo 1中的所有内容。 git 文档

只查看触及给定子目录的历史记录。 结果将包含该目录(并且仅包含该目录)作为其项目根目录

  1. 您仍然指向您的 origin repo 1 url
  2. 替换当前文件夹的原始 url 以指向repo 2
  3. 检查 URL 是否已正确更新
  4. 推送到repo 2 (新创建的,在这种情况下) develop分支

您现在可以克隆、拉取或获取您的repo 2存储库。 您将在repo 2文件夹下拥有dir-to-move的内容以及预期的相关历史记录。

另一种解决方案是使用git-filter-repo ,这是git filter-branch 官方推荐的

在我的关于如何将影响给定路径的提交推送到新源​​的回答中找到一个完整的示例,包括有关 Windows 用户安装的提示

git:将文件夹从一个存储库移动到另一个存储库,同时保留历史记录

cd target-repo
git remote add source ../source-repo
git fetch source
git checkout -b source source/master

# filter out history of a single folder
git filter-branch --subdirectory-filter ./dir -- --all

# put the folder history on top of your target repo history
git rebase master

你甚至可以提交关于repo-1 ,删除以外的所有目录dir-to-move本地(通过这种方式无需使用fitler-branch ),然后拉来repo-2

git clone https://path/to/repo-1
cd repo-1
rm dir1 dir2 dir3 dir5 -rf
git commit -m "Removed unwanted directory for repo-2
cd ../
git clone https://path/to/repo-2
cd repo-2
git remote add repo-1-src ../repo-1
git pull repo-1-src master --allow-unrelated-histories

注意:确保git版本为2.9或更高版本以使用--allow-unrelated-histories选项

这确实可以使用git subtree

在 repo-1 中创建一个子树:

git subtree split -P dir-to-move -b <split>

split分支现在将只包含dir-to-move目录。 您现在需要将分支从 repo-1 拉入 repo-2 中的分支。

如果 repo-2 恰好是一个新的存储库(例如,就在git init ),事情就像检查一个没有历史记录的分支并从 repo-1 中拉取一样简单。

cd repo-2
git checkout <branch>
git pull <path-to-repo-1.git> <split>

但是,如果 repo-2 是已经提交的现有存储库(如本问题中的情况),则需要从 repo-2 中的孤立分支合并:

cd repo-2
git checkout --orphan <temp>                    # Create a branch with no history
git pull <path-to-rpeo-1.git> <split>           # Pull the commits from the subtree
git checkout <branch>                           # Go back to the original branch
git merge --allow-unrelated-histories <temp>    # Merge the unrelated commits back
git branch -d <temp>                            # Delete the temporary branch

如果所有其他都失败,则愚蠢的解决方案手动解决方案:

  1. 从第一个 repo 下载所有文件
  2. 创建一个新的仓库
  3. 将第二个 repo 克隆到磁盘。
  4. 解压要保留的第一个 repo 文件(新结构)
  5. 将更改推送到第二个 repo。
  6. 确认你有你想要的
  7. 从您不想要的第一个存储库中删除文件/文件夹。

你当然失去了可追溯性......

我最喜欢的解决方案是http://blog.neutrino.es/2012/git-copy-a-file-or-directory-from-another-repository-preserving-history/ 然而,它似乎正在从谷歌搜索中消失,所以我担心它有一天会消失,所以我在这里复制它:

mkdir /tmp/mergepatchs
cd ~/repo/org
export reposrc=myfile.c #or mydir
git format-patch -o /tmp/mergepatchs $(git log $reposrc|grep ^commit|tail -1|awk '{print $2}')^..HEAD $reposrc
cd ~/repo/dest
git am /tmp/mergepatchs/*.patch

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM