简体   繁体   English

如何将一个 repo 移动到另一个 repo 文件夹并保留 git 历史记录?

[英]How to move one repo to another repo folder and keep git history?

I want to move my project(egproject-A) to monorepo and keep whole git history.我想将我的项目(例如项目-A)移动到 monorepo 并保留整个 git 历史记录。 now when adding remote repository to new one with现在将远程存储库添加到新存储库时

git remote add A <remote-repo-project-A>

and pull project with并拉项目

git pull project-A master --allow-unrelated-histories

source files are in folder monorepo\src instead of monorepo\apps\project-A\src源文件位于文件夹 monorepo\src 而不是 monorepo\apps\project-A\src

Basically I want to move repo-A to new repo with following structure:基本上我想将 repo-A 移动到具有以下结构的新 repo:

repo-A
---- src

new repo
---- projects
---- ---repo-A(rename to project-A)
---- ------src

History, in Git, is simply the commits.历史,在 Git 中,只是提交。 The commits are the history.提交历史。 So the literal answer to the question that you didn't quite ask—"can I change the contents of the commits without changing the commits' contents"—is of course no .因此,对于您没有完全提出的问题的字面答案——“我可以在不更改提交内容的情况下更改提交的内容吗”——当然是no More importantly, no commit can ever be changed, by anyone or anything, because the commits are numbered , and the numbers are cryptographic checksums of their contents.更重要的是,任何人或任何事情都不能更改任何提交,因为提交是编号的,而数字是其内容的加密校验和。 So if you change anything about a commit, that changes the number .因此,如果您更改有关提交的任何内容,则会更改number

All is not, however, lost.然而,一切都没有丢失。 You can copy the commits to new-and-improved commits, whose only difference from the original commits is... well, let's take one commit as an example.您可以将提交复制到新的和改进的提交,其与原始提交的唯一区别是......好吧,让我们以一个提交为例。 The original commit says the files are named src/file1 , src/file2 , etc, and here are their contents: ... The commit was made by ____ on ____ and its parents are... The new-and-improved commit says the files are named projects/A/src/file1 , projects/A/src/file2 , etc., and here are their contents... The commit was made by (same person) on (same date) and its parents are (the new-and-improved copies of the original commit's parents).原始提交说文件被命名为src/file1src/file2等,这里是它们的内容:......提交是由____在____上进行的,它的父母是......新的和改进的提交说这些文件被命名为projects/A/src/file1projects/A/src/file2等,这里是它们的内容......提交是由(同一个人)在(同一日期)作出的,其父母是(原始提交的父母的新的和改进的副本)。

This copying process is tedious but straightforward, and it can be done by git filter-branch , git filter-repo , and various other tools as shown in git move directory to another repository while keeping the history (among many other very similar questions).这个复制过程很繁琐但很简单,可以通过git filter-branchgit filter-repo以及git 中所示的各种其他工具来完成,同时将许多其他类似的问题移至另一个存储库 Note that if you have other additional repositories whose commits—or rather, new-and-improved copies of commits—you'd like to have in this repository, you must decide whether to convert each set of commits one at a time, then try to make combining (merge) commits to combine them, or to write a much fancier filter that attempts to combine all the other repositories' commits at once.请注意,如果您有其他额外的存储库,其提交——或者更确切地说,提交的新的和改进的副本——你想在这个存储库中拥有,你必须决定是否一次转换每组提交,然后尝试进行组合(合并)提交以组合它们,或者编写一个更漂亮的过滤器,尝试一次组合所有其他存储库的提交。 That is, one new-and-improved commit might represent two or more "original" commits from other repositories.也就是说,一个新的和改进的提交可能代表来自其他存储库的两个或多个“原始”提交。 This is a much more difficult proposition and the existing filter programs that do the simple straightforward thing do not do this for you.这是一个更困难的命题,现有的过滤程序做简单直接的事情并没有为你做这件事。

I tried the following steps.我尝试了以下步骤。

[1] cd /path/to/work_folder [1] cd /path/to/work_folder

[2] git clone https://github.com/git_repo_1/git_repo_1.git [2] git 克隆https://github.com/git_repo_1/git_repo_1.git

[3] cd git_repo_1/ [3] cd git_repo_1/

[4] git remote rm origin [4] git 远程rm原点

[5] git filter-branch --subdirectory-filter folder_to_move/ -- -- all [5] git filter-branch --subdirectory-filter folder_to_move/ -- -- 所有

[6] mkdir folder_to_move [6] mkdir folder_to_move

[7] git mv file(s) folder_to_move/ [8] git mv dir(s) folder_to_move/ [7] git mv 文件 folder_to_move/ [8] git mv dir(s) folder_to_move/

[9] git add. [9] git 添加。

[10] git commit (add some comment) [10] git 提交(添加一些评论)

[11] cd /path/to/work_folder [11] cd /path/to/work_folder

[12] git clone https://github.com/git_repo_2/git_repo_2.git [12] git 克隆https://github.com/git_repo_2/git_repo_2.git

[13] cd git_repo_2/ [13] cd git_repo_2/

[14] git remote add repo-A-branch /path/to/work_folder/git_repo_1/ [14] git 远程添加repo-A-branch /path/to/work_folder/git_repo_1/

[15] git pull repo-A-branch master --allow-unrelated-histories [15] git pull repo-A-branch master --allow-unrelated-histories

[16] git remote rm repo-A-branch [16] git 远程rm repo-A-branch

[17] git push [17] git推

All the above steps are successful.以上步骤均成功。 However when I go to github web interface and want to see the history of the modifications on that file within the folder I moved I do not see any history.但是,当我从 go 到 github web 界面并想查看我移动的文件夹中该文件的修改历史时,我没有看到任何历史。

However on my terminal if I do the following但是在我的终端上,如果我执行以下操作

cd git_repo_2/ git log cd git_repo_2/ git 日志

I can see all the history for that folder I moved from git_repo_1 to git_repo_2我可以看到我从 git_repo_1 移动到 git_repo_2 的那个文件夹的所有历史记录

My question is --> Is there a way where I can see the history of the changes for each file after moving the directory from git_repo_1 to git_repo_2我的问题是 --> 将目录从git_repo_1移动到git_repo_2后,有没有办法可以查看每个文件的更改历史记录

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

相关问题 将文件夹从一个存储库复制/移动到另一个存储库,同时保留 Git 中的历史记录 - Copy/Move a folder from one repo to another repo while preserving history in Git 将一个 git 存储库复制到另一个具有历史记录的存储库 - Copying one git repo to another repo with history 如何将一些文件从一个 git 存储库移动到另一个(不是克隆),保留历史记录 - How to move some files from one git repo to another (not a clone), preserving history Git:将具有历史记录的一部分从一个存储库移动到另一个存储库 - Git: move part from one repo to other repo with history GIT 将一个仓库与另一个仓库中的文件夹合并,而不会丢失历史记录 - GIT MERGE a repo with a folder in another repo without loosing history 将一个本地存储库文件/历史记录移到另一本地存储库中 - Move one local repo files/history in to another local repo 如何使用`git format-patch`和`git am`将文件从一个git repo移动到另一个保存历史记录 - How to move files from one git repo to another preserving history using `git format-patch`and `git am` 将Git Repo作为普通文件夹移至另一个报告 - Move Git Repo To Another Report as A Normal Folder 如何在 git 中将一个仓库转移到另一个仓库 - How to transfer one repo to another repo in git 在另一个git repo中将一个文件夹的git repo分开 - Separate git repo for one folder in another git repo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM