推送提交会影响到新源的给定路径吗?

ste*_*wpf 6 git

我目前的文件结构如下所示:

# C:\Repositories\VisualStudio\MyProject
.git # repository is one level above clientapp and includes other folders/paths than clientapp
afolder
clientapp
file1
file2
Run Code Online (Sandbox Code Playgroud)

现在我想将该clientapp文件夹移动到新位置并将其重命名为frontend. 然后我想仅为frontend. 直到这里才出现问题:

# inside C:\Repositories\MyProject\frontend (no more VisualStudio in the path!)
.git
<all the frontend folders and files>
Run Code Online (Sandbox Code Playgroud)

现在问题来了:当然,我现在丢失了原始clientapp(现在frontend:)文件夹的历史记录,因为我创建了一个新的存储库。有什么方法可以将有关原始文件夹的提交复制clientapp到我的新存储库吗frontend

ste*_*wpf 13

TLDR:将原始存储库过滤到想要保留的文件夹,然后从新存储库中将其拉入。


  1. 下载并使用官方推荐的git -filter-repogit filter-branch

    *Windows 用户 - 请参阅在 Windows 上安装 git-filter-repo

  2. 过滤存储库中的相关文件(确保之前有备份!):

    git filter-repo --path clientapp
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将clientapp内的文件移至根目录

    git filter-repo --subdirectory-filter clientapp/
    
    Run Code Online (Sandbox Code Playgroud)
  4. 转到您的新存储库,将远程添加到原始存储库:

    git remote add repo-A <path to original repo, e.g. ../repo-A>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 将此分支中的文件和历史记录拉入存储库 B(仅包含您要移动的目录)。

    git pull repo-A master
    
    Run Code Online (Sandbox Code Playgroud)
  6. 删除与存储库 A 的远程连接。

    git remote rm repo-A
    
    Run Code Online (Sandbox Code Playgroud)
  7. 从新存储库推送

    git push
    
    Run Code Online (Sandbox Code Playgroud)
  8. 删除原始存储库的副本(仅包含过滤后的文件夹)。如果需要,请再次克隆它。愉快地使用具有完整提交历史记录的新存储库。

进一步阅读:关于如何将文件从一个存储库移动到另一个存储库、保留 git 历史记录的中型文章,特别是有关将文件合并到新存储库 B 的部分。