Can a git submodule be created retroactively?

試著忘記壹切 提交于 2021-02-10 19:44:11

问题


I've seen a lot of tutorials on using submodules, but they both seem to have one of two common use cases.

A) I've got some code in a subdirectory, I want to begin to track it separately, so I turn it into a submodule using filter-branch

B) I want to pull a git repo into a subdirectory of my project and have it tracked separately

But lets say you didn't know about git submodules and had a copy of a repo dropped into a subdirectory of your own repo, and had to make some modifications, but eventually realized it should have been a submodule. Is there a way to retroactively take that subdirectory and turn it into a submodule that is reintegrated with the repo it was copied from?

It might work better with an example. Lets say your local project is Xyz. You pull down the (imaginary) project https://github.com/abc/def and copied it into a subdirectory of Xyz. So you had something like:

Xyz/
  .git/
  def/somefile.js
  def/anotherfile.csv
  def/... more stuff
  something.dat
  whatever.txt
  ... even more stuff

So then you made some small modifications to stuff in def/ and stuff outside of def/ and committed. And then again and again dozens of times.

Now you realize you need to update against the latest version of https://github.com/abc/def -- how can it be done? Is there a way to turn the directory into a submodule, and then somehow reintegrate the history of https://github.com/abc/def and make that it's remote?


回答1:


You have a few options here.

If you want to have the project be a submodule and have it always have been a submodule (rewriting history), then it is tricky. You have a nasty filter-branch where you also have to replace the directory with a proper reference to submodule. You also have to go back and replay all your work into the submodule to create all those references.

Easier would be to just make it a submodule going forward. Branch/apply/merge in the submodule to get up to date, nuke the directory from your original project, add the submodule, then maintain things going forward.

Easier still might be to simple branch off the original import SHA, apply the newest version from github to that branch, and then merge that branch into your mainline. No submodules needed in this situation.



来源:https://stackoverflow.com/questions/26363894/can-a-git-submodule-be-created-retroactively

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!