Git:从存储库获取一个子文件夹到一个新文件夹而不会丢失历史记录 [英] Git: Get a subfolder from a repository to a new one without losing history

查看:162
本文介绍了Git:从存储库获取一个子文件夹到一个新文件夹而不会丢失历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个分支的应用程序的 git 存储库。源代码树由几个目录组成。例如:

  main_folder 
| - > .git
| - > dir0
| - > dir1
| - > dir2

不幸的是,从开发开始我就没有使用 git -submodules 也不是 git-subtree 。现在我想将其中一个目录,例如 dir0 移动到一个新的存储库中作为一个新的独立应用程序。当然,如果可能的话,我想保留历史记录和分支



  • 所以我有两个问题:


    1. 第一个显而易见的是我如何做到这一点?

    2. 对于旧版本库,最佳做法是什么?只需删除文件夹并提交更改或其他内容即可?
    3. 使用 使用 git splits



      基本上使用相同的 git filter-branch 的方法,但有一个更简单的界面。


      1. 安装 git splits 。我根据 jkeating的解决方案将其创建为git扩展。

      2. 将目录拆分为本地分支

        #切换到您的repo目录
        cd / path / to / repo
        #checkout分支
        git checkout MyOldBranch
        #将一个或多个目录分割成新分支
        git分割-b MyNewBranch dir1 dir2 dir2 ...
        MyNewBranch分支。






        如果您希望将新分支推送到独立回购


        1. 在某处创建一个空的回购站。我们假设我们在GitHub上创建了一个名为 new_repo 的空回购,其路径为: git@github.com:simpliwp / new_repo.git


        2. 推入新的回购。

          #为空回购添加一个新的远程原点,这样我们可以推到GitHub上的空回购
          git remote add origin_new_repo git@github.com:simpliwp / new_repo.git
          #将分支推送到新的repo的主分支
          git push origin_new_repo new_repo:master


        3. 将新创建的远程仓库克隆到新的本地目录中

          #将旧仓库中的当前目录改为
          cd / path / to / where / you / want / the / new / local / repo
          #clone您刚刚推送至
          的远程回购git clone git@github.com:simpliwp / new_repo.git



        I have a git repository of an application with multiple branches. The source tree consists of several directories. E.g:

        main_folder
        |--> .git
        |--> dir0
        |--> dir1
        |--> dir2
        

        Unfortunately, from the beginning of the development I didn't use git-submodules nor git-subtree. Now I want to move one of the directories, e.g dir0 to a new repository as a new stand-alone application, Of course if possible, I want to keep both history and branches of the corresponding directory.

        So I have two questions:

        1. The first and obvious one is how I can do this?
        2. Also for the old repository, what would be the best practice? Just delete the folder and commit the changes, or something else?

        解决方案

        use git splits

        it basically use your same git filter-branch approach but has an easier interface.

        1. install git splits. I created it as a git extension, based on jkeating's solution.
        2. Split the directories into a local branch #change into your repo's directory cd /path/to/repo #checkout the branch git checkout MyOldBranch #split directory or directories into new branch git splits -b MyNewBranch dir1 dir2 dir2...

        Stop here (you're done) if all you want is the directories copied with their history to the new MyNewBranch branch.


        If you want the new branch pushed to a standalone repo

        1. Create an empty repo somewhere. We'll assume we've created an empty repo called new_repo on GitHub that has path : git@github.com:simpliwp/new_repo.git

        2. Push to the new repo. #add a new remote origin for the empty repo so we can push to the empty repo on GitHub git remote add origin_new_repo git@github.com:simpliwp/new_repo.git #push the branch to the new repo's master branch git push origin_new_repo new_repo:master

        3. Clone the newly created remote repo into a new local directory
          #change current directory out of the old repo cd /path/to/where/you/want/the/new/local/repo #clone the remote repo you just pushed to git clone git@github.com:simpliwp/new_repo.git

        这篇关于Git:从存储库获取一个子文件夹到一个新文件夹而不会丢失历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆