开发者

Using a project in a Git repo in another project

开发者 https://www.devze.com 2023-02-12 10:19 出处:网络
I have a framework I am building to make some common functions (database connection) easier and would like to use it in another project. I could always just copy and paste it, but I am working on it a

I have a framework I am building to make some common functions (database connection) easier and would like to use it in another project. I could always just copy and paste it, but I am working on it and expanding it constantly. It would be much easier to just h开发者_如何学JAVAave it copy its updated self over whenever I do a commit in my Git repo.

Could I use Git to automatically update the framework in my other project or will I have to find another solution?


Use either git subtrees or git submodules.

Last time I've checked, submodules had some major issues (but I've heard that they were improved a bit in recent versions). See here for details: Git submodules workflow

In short:

Subtrees are regular Git branches, merged with special merge strategy, which puts all their files into a subdirectory of the host repository working copy. Aside of the merge strategy (which matters only when merge commit is created), Git "subtree" is a perfectly normal Git branch.

Pros:

  • No extra tool support needed, except when you need to pull in new changes from the subtree, and even then all support is the -s subtree command line option (see here for the full workflow). You need to remember about subtree only when you do the subtree pull.

Cons:

  • All subtree commits are visible in the history of the host project.
  • Don't commit changes to the host repository's directory where subtree lives in or face conflicts on next subtree pull.

Submodules are Git repositories inside the host repository.

Pros:

  • Cleaner history, better separation from the host repository.
  • One may commit directly to the submodule upstream from the submodule repository directly (or so I think).

Cons:

  • For each (or many) Git command you have remember that you're dealing with repository with submodules, lots of nasty corner cases (or so it was when I looked). Special tool support required to work with each clone of the repository.

Disclaimer: I'm biased against submodules. Try and see for yourself, what approach is better for your workflow.


git-submodules.

You will have a few issues with updating in different places, but it should work out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号