You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将old_repo迁移至new_repo的sub_dir_4_old_repo并保留Git提交历史

迁移Git仓库至目标仓库子目录并保留提交历史
  • 先把旧仓库克隆到本地临时目录(如果本地没有的话):
    git clone <old_repo的远程地址> temp_old_repo
    进入这个临时仓库:
    cd temp_old_repo

  • 重写所有提交历史,把仓库里的所有文件都移动到目标子目录sub_dir_4_old_repo下:
    用git filter-repo工具执行命令:
    git filter-repo --to-subdirectory-filter sub_dir_4_old_repo

    要是没装这个工具,先通过包管理器安装,比如Debian/Ubuntu用sudo apt install git-filter-repo,Mac用brew install git-filter-repo

  • 把新仓库设为远程仓库地址:
    git remote add new_repo <new_repo的远程地址>

  • 把重写后的历史推送到新仓库的对应分支(比如main分支):
    git push new_repo main
    (如果你的旧仓库默认分支是master,就把命令里的main换成master)

  • 最后可以删掉本地的临时仓库(可选):
    cd ..
    rm -rf temp_old_repo

内容的提问来源于stack exchange,提问作者feiyuerenhai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 00:20:00