How to rename a git repo (project) which contains submodules in its subdirectories

前端 未结 4 1761
礼貌的吻别
礼貌的吻别 2021-02-12 14:34

I never expect renaming a git repo, which, more specifically, is the top-level folder holds the project, would be so hard. Yes, the project containing some submodules, but it is

4条回答
  •  死守一世寂寞
    2021-02-12 15:37

    You have a couple of options, they end up being the same thing:

    clone again

    Instead of renaming the folder - just clone again

    $ cd /project/original
    $ cd ..
    $ mkdir moved
    $ git init
    $ git pull ../original master
    $ git submodule init
    $ git submodule update
    

    Compare original/.git/config to moved/.git/config and address any significant differences (missing branches need creating - missing remotes just need adding to the config file).

    fix paths

    You can rename your project folder, it just needs a little tweaking.

    • Fix your submodule .git file references.

    I.e. these files:

    $ cd /project/moved
    $ find -name .git -type f
    

    All you need to do is edit them to point at the right directory

    • Fix your submodule .git config files

    I.e. these files:

    $ cd /project/moved
    $ find .git/modules/ -name config
    

    Here, update the worktree setting:

    [core]
        ...
        worktree = /original/path/submodule
    

    To

    [core]
        ...
        worktree = /moved/path/submodule
    

    And that's it.

    A note about versions

    1.7.8 introduced the use of a .git file for submodules and used absolute paths, this was fixed in 1.7.10 - therefore the problem only applies to git repos created with git version 1.7.8, and 1.7.9.

提交回复
热议问题