git:如何从项目中分离图书馆?过滤分支,子树? [英] git: How to split off library from project? filter-branch, subtree?

查看:118
本文介绍了git:如何从项目中分离图书馆?过滤分支,子树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个更大的(封闭源代码)项目,并且在这个项目的上下文中创建了一个库,在我认为的其他地方也可能是有用的。



我现在想在自己的项目中拆分这个库,它可以作为github或类似的开源代码。当然,图书馆(及其历史)应该不包含我们项目的痕迹。

git-subtree在这里似乎是一个解决方案,但它并不完全适合。



我的目录布局是这样的(因为它是一个Java项目):


  • fencing-game (git workdir)

    • src

      • de

        • fencing_game

          • 运输 (我的图书馆)

            • 协议 (库的一部分)

            • 围栏项目与库的接口)

            • client (与库接口的主项目的一部分)

            • server (与库接口的主项目的一部分)
            • 主项目)
            • server (主项目的一部分)

            • ... (主项目的一部分)



            • em>其他文件和目录(构建系统,网站等) - / b>

              $ b

              分割后,我希望库的目录布局看起来像这样(包括直接在粗体目录中的任何文件):


              • my-library (要确定的名称)

                • src

                  • de

                    • fencing_game

                      • 运输 (我的图书馆)

                        • 协议 (库的一部分)




                  $ b


                历史记录还应包含主要项目历史记录中涉及这部分存储库的部分。



                首先看看 git-subtree split --prefix = src / de / fencing_ame / transport ,但是这个将给我一棵植根于 transport (它不会被编译)的树,以及



                1. 包括 transport / client transport / server 传输/屏蔽目录。

                第一点可以通过使用 git subtree add --prefix = src / de / fencing_ame / transport< commit> 在接收端,但我不认为git-subtree在导出这些子目录时可以做很多事情。 (这个想法的确是在这里共享完整的树)。



                我必须使用 git filter-branch 在这里?



                在拆分之后,我希望能够将我的主项目中的库导入到库中,可以使用git -subtree或git-submodule,在一个单独的子目录中,而不是它现在的位置。我想象这样的布局


                $ b $ p fencing-game ul>
              • src

                • de

                  • fencing_game

                    • transport (空)

                      • fencing (与库连接的主项目的一部分) li>
                      • client (与图书馆接口的主项目的一部分)

                      • server (主要项目接口的一部分) )

                      • server (主项目的一部分)

                      • ... (主项目的一部分)




                  • my-library

                    • src

                      • de

                        • fencing_game

                          • 运输 (我的图书馆)

                            • 协议 (库的一部分)


                          • em>其他文件和目录(构建系统,网站等) - / b>
                            $ b 什么是最没有痛苦的方式来做到这一点?有没有其他的工具,而不是git-subtree和git-filter-branch这个目标?

                            解决方案

                            拆分与父项目混合的子树



                            这似乎是一个常见的请求,但是我不要以为有一个 简单的 答案,就是当文件夹混合在一起时。



                            我建议拆分与其他文件夹混合的库的一般方法是这样的:



                                git subtree split -P src / $ b 
                            1. 使用库的新根分支: de / fencing_game -b temp-br
                              git checkout temp-br

                              # - 或 - ,如果你真的想保留完整路径:

                              git checkout -b temp-br
                              cd src / de / fencing_game

                            2. 然后使用一些东西重新编写历史记录以删除不属于图书馆一部分的部分。我不是这方面的专家,但我能够尝试并找到类似这样的工作:

                                git filter-branch  - -tag-name-filter cat --prune-empty --index-filter'git rm -rf --cached --ignore -unmatch client server otherstuff'HEAD 

                              #也清除sub dir
                              cd transport
                              git filter-branch --tag-name-filter cat --prune-empty --index-filter'git rm -rf --cached --ignore -unmatch fencing client server 'HEAD

                              注意:您可能需要删除连续命令之间由filter-branch所做的备份。

                                git update-ref -d refs / original / refs / heads / temp-br 
                              $ b $ li $最后,只需为该库创建一个新的回购站并吸收剩下的所有内容即可:

                                cd< new-lib-repo> 
                              git init
                              git pull< original-repo> temp-br


                            我建议您最终的图书馆路径更像是 / transport / protocol 而不是完整的父项目路径,因为这似乎与项目有关。


                            So, I've a bigger (closed source) project, and in the context of this project created a library which could also be useful elsewhere, I think.

                            I now want to split off the library in its own project, which could go as open source on github or similar. Of course, the library (and its history there) should contain no traces of our project.

                            git-subtree seems like a solution here, but it does not completely fit.

                            My directory layout is something like this (since it is a Java project):

                            • fencing-game (git workdir)
                              • src
                                • de
                                  • fencing_game
                                    • transport (my library)
                                      • protocol (part of the library)
                                      • fencing (part of the main project interfacing with the library)
                                      • client (part of the main project interfacing with the library)
                                      • server (part of the main project interfacing with the library)
                                    • client (part of the main project)
                                    • server (part of the main project)
                                    • ... (part of the main project)
                              • other files and directories (build system, website and such - part of the main project)

                            After the split, I want the library's directory layout look like this (including any files directly in the bold directories):

                            • my-library (name to be determined)
                              • src
                                • de
                                  • fencing_game
                                    • transport (my library)
                                      • protocol (part of the library)

                            The history should also contain just the part of the main project's history which touches this part of the repository.

                            A first look showed me git-subtree split --prefix=src/de/fencing_ame/transport, but this will

                            1. give me a tree rooted in transport (which will not compile) and
                            2. include the transport/client, transport/server and transport/fencing directories.

                            The first point could be mitigated by using git subtree add --prefix=src/de/fencing_ame/transport <commit> on the receiving side, but I don't think git-subtree can do much against exporting also these subdirectories. (The idea really is to just be able to share the complete tree here).

                            Do I have to use git filter-branch here?

                            After the split, I want to be able to import back the library in my main project, either using git-subtree or git-submodule, in a separate subdirectory rather than where it is now. I imagine the layout this way

                            • fencing-game (git workdir)
                              • src
                                • de
                                  • fencing_game
                                    • transport (empty)
                                      • fencing (part of the main project interfacing with the library)
                                      • client (part of the main project interfacing with the library)
                                      • server (part of the main project interfacing with the library)
                                    • client (part of the main project)
                                    • server (part of the main project)
                                    • ... (part of the main project)
                              • my-library
                                • src
                                  • de
                                    • fencing_game
                                      • transport (my library)
                                        • protocol (part of the library)
                              • other files and directories (build system, website and such - part of the main project)
                            What would be the most pain-free way to do this? Are there other tools than git-subtree and git-filter-branch for this goal?

                            解决方案

                            Splitting a subtree mixed with files from the parent project

                            This seems to be a common request, however I don't think there's a simple answer, when the folders are mixed together like that.

                            The general method I suggest to split out the library mixed in with other folders is this:

                            1. Make a branch with the new root for the library:

                              git subtree split -P src/de/fencing_game -b temp-br
                              git checkout temp-br
                              
                              # -or-, if you really want to keep the full path:
                              
                              git checkout -b temp-br
                              cd src/de/fencing_game
                              

                            2. Then use something to re-write history to remove the parts that aren't part of the library. I'm not expert on this but I was able to experiment and found something like this to work:

                              git filter-branch --tag-name-filter cat --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch client server otherstuff' HEAD
                              
                              # also clear out stuff from the sub dir
                              cd transport 
                              git filter-branch --tag-name-filter cat --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch fencing client server' HEAD
                              

                              Note: You might need to delete the back-up made by filter-branch between successive commands.

                              git update-ref -d refs/original/refs/heads/temp-br
                              

                            3. Lastly, just create a new repo for the library and pull in everything that's left:

                              cd <new-lib-repo>
                              git init
                              git pull <original-repo> temp-br
                              

                            I recommend that your final library path be more like /transport/protocol instead of the full parent project path since that seems kind of tied to the project.

                            这篇关于git:如何从项目中分离图书馆?过滤分支,子树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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