switching a subdirectory managed by git to a submodule

后端 未结 2 560
失恋的感觉
失恋的感觉 2021-02-04 09:24

We used to have a local hack of delayed_job in a Rails app, in vendor/plugins/delayed_job. It was installed as a one-time event and checked into git in the main app repo.

2条回答
  •  孤街浪徒
    2021-02-04 09:55

    Assuming that you do not care about the current contents of vendor/plugins/delayed_job in your working tree (i.e. the content that will be checked out as a submodule is already a suitable replacement for the content in your working tree), the normal procedure for converting a directory into a submodule looks like this:

    git rm -r vendor/plugins/delayed_job
    git submodule add github.com:account/delayed_job.git vendor/plugins/delayed_job
    

    Of course, the GitHub repository URL may vary; for example, you may want to use an HTTP URL instead of the above SSH URL.

    But, it seems like you did something a bit different. As best I can tell, you did something like this:

    rm -rf vendor/plugins/delayed_job
    git clone github.com:account/delayed_job.git vendor/plugins/delayed_job
    

    There are two flaws with this procedure:

    1. The plain rm leaves the old files in your Git index.
    2. Directly cloning gives you a “subrepository”, but not an official submodule.

    Assuming that you do not have any intentionally staged changes in vendor/plugins/delayed_job (you probably do not, since you are replacing it with a submodule), you can clean up the situation with these commands:

    git rm --cached -r vendor/plugins/delayed_job
    git submodule add github.com:account/delayed_job.git vendor/plugins/delayed_job
    

    Cleaning out all the vendor/plugins/delayed_job entries from the index should fix your “still shows new files” problem. Using git submodule add will create the .gitmodules file which turns the “subrepository” into a true submodule.

提交回复
热议问题