如何重命名其子目录中包含子模块的git repo(project) [英] How to rename a git repo (project) which contains submodules in its subdirectories

查看:310
本文介绍了如何重命名其子目录中包含子模块的git repo(project)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来不希望重命名git repo,更确切地说,它是顶级文件夹来保存项目,会非常困难。是的,该项目包含一些子模块,但它是需要重命名的顶级文件夹,而不是子模块文件夹。看起来,Git在其子模块机制中记录了一些奇怪的绝对路径 让我们假设


  1. 您的所有项目都位于 / tmp 中。

  2. code> proj_master 和 proj_mod

  3. 克隆 porj_master 作为 proj_ALL ,然后克隆 prom_mod 作为其中的子模块。 b
  4. proj_ALL 重命名为 proj_onebillion 。然后发生黑魔法。

以下步骤将重现我提到的问题。我使用的版本是:

  $ git --version 
git版本1.7.9.5







  1. 初始化 proj_master

      $ cd / tmp 
    $ mkdir proj_master; cd proj_master
    $ git init。
    $ touch README
    $ git add。; git commit -mhello proj_master


  2. 初始化 proj_mod

      $ cd / tmp 
    $ mkdir proj_mod; cd proj_mod
    $ git init。
    $ touch README
    $ git add。; git commit -mhello proj_mod


  3. 克隆 proj_master 作为 proj_ALL 和克隆 proj_mod 作为子模块。

      $ cd / tmp 
    $ git clone proj_master proj_ALL
    $ cd proj_ALL
    $ git submodule add / tmp / proj_mod ./mod
    $ git add。; git commit -mhello proj_ALL
    $ git status%一切正常。


  4. proj_ALL 重命名为 proj_onebillion 。遇到致命错误。

      $ cd / tmp 
    $ mv proj_ALL proj_onebillion
    $ cd proj_onebillion
    $ git status
    fatal:不是git存储库:/tmp/proj_ALL/.git/modules/mod







要注意的一件事是 .git 文件在子模块目录中。

  $ cat /tmp/proj_ALL/mod/.git 
gitdir:/ tmp / proj_ALL / .git / modules / mod

是的,绝对路径。第一次,我意识到git知道顶级repo文件夹范围之外的东西。



就是这样。我再重复一次,我重命名顶级项目文件夹,而不是子模块文件夹。我查看 schmuck的问题,它试图重命名子模块文件夹,因此似乎对我的问题没有太大帮助。



如果我错过了以前应该阅读的内容,我很抱歉。对所有人来说,欢迎任何建议。

解决方案

您有几个选项,它们最终都是相同的:
$ b再次克隆

而不是重命名文件夹 - 只需再次克隆

  $ cd / project / original 
$ cd ..
$ mkdir移动
$ git init
$ git pull ../原始主
$ git子模块init
$ git子模块更新

比较 original / .git / config 移动/ .git / config 并且解决任何显着差异(丢弃分支需要创建 - 缺少远程只需要添加到配置文件)。



修复路径



您可以重新命名您的项目文件夹,它只需要稍微调整一下。


  • 修复您的子模块.git文件引用。



Ie这些文件:

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

您只需编辑它们即可指向正确的目录




  • 修复您的子模块.git配置文件


Ie这些文件:

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

在这里,更新 worktree 设置:

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




$ b $ [core] $

b $ b ...
worktree = /移动/路径/子模块

就是这样。

有关版本的说明

1.7.8介绍了使用a子模块的.git文件和使用绝对路径,这是固定在1.7.10 - 因此这个问题只适用于使用git 1.7.8和1.7.9创建的git repos。


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 the top-level folder that needs renaming, not the submodule folder. Git, it seems, records some odd absolute paths in its submodule mechanisms.

Let's assume that

  1. All your projects locate in /tmp.
  2. You've got a proj_master and proj_mod.
  3. You clone porj_master as proj_ALL then clone prom_mod as a submodule in it.
  4. You rename proj_ALL to proj_onebillion. Then black magic happens.

The following steps will reproduce the problem I mentioned. The version of git I use is:

$ git --version
git version 1.7.9.5


  1. Initialize proj_master.

    $ cd /tmp
    $ mkdir proj_master; cd proj_master
    $ git init .
    $ touch README
    $ git add .; git commit -m "hello proj_master"
    

  2. Initialize proj_mod.

    $ cd /tmp
    $ mkdir proj_mod; cd proj_mod
    $ git init .
    $ touch README
    $ git add .; git commit -m "hello proj_mod"
    

  3. Clone proj_master as proj_ALL and clone proj_mod as a submodule.

    $ cd /tmp
    $ git clone proj_master proj_ALL
    $ cd proj_ALL
    $ git submodule add /tmp/proj_mod ./mod
    $ git add .; git commit -m "hello proj_ALL"
    $ git status   % Everything is OK.
    

  4. Rename proj_ALL to proj_onebillion. Encounter a fatal error.

    $ cd /tmp
    $ mv proj_ALL proj_onebillion
    $ cd proj_onebillion
    $ git status
    fatal: Not a git repository: /tmp/proj_ALL/.git/modules/mod
    


One thing to notice is the .git file in the submodule directory.

$ cat /tmp/proj_ALL/mod/.git 
gitdir: /tmp/proj_ALL/.git/modules/mod

Yeah, an absolute path. For the first time, I realize that git is aware of something outside the scope of the top-level repo folder.

That's it. I repeat that one more time that I rename the top-level project folder, not the submodule folder. I check schmuck's question, which tried to rename the submodule folder, therefore seems not so helpful to my problem.

If I miss something that should have been read before, I apologize. To all guys, any advice is welcomed.

解决方案

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.

这篇关于如何重命名其子目录中包含子模块的git repo(project)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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