【问题标题】:Push commits affecting a given path to a new origin?将影响给定路径的提交推送到新源​​?
【发布时间】:2020-12-12 09:58:21
【问题描述】:

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

# C:\Repositories\VisualStudio\MyProject
.git # repository is one level above clientapp and includes other folders/paths than clientapp
afolder
clientapp
file1
file2

现在我想将clientapp 文件夹移动到新位置并将其重命名为frontend。然后我只想为frontend 创建一个新的存储库。到这里没问题:

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

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

【问题讨论】:

标签: git


【解决方案1】:

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


  1. 下载并使用git-filter-repo其中git filter-branchofficially recommends

    *Windows 用户 - 请参阅 Install git-filter-repo on Windows

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

    git filter-repo --path clientapp
    
  3. 把clientapp里面的文件移到根目录

    git filter-repo --subdirectory-filter clientapp/
    
  4. 转到您的新存储库,将远程添加到原始存储库

  5. 在您的新存储库中添加一个远程到原始存储库:

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

    git pull repo-A master
    
  7. 删除与存储库 A 的远程连接。

    git remote rm repo-A
    
  8. 从您的新存储库推送

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

进一步阅读:关于如何move files from one repository to another, preserving git history 的中篇文章,尤其是关于将文件合并到新存储库 B 的部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2013-12-06
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多