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
/tmp
.proj_master
and proj_mod
.porj_master
as proj_ALL
then clone prom_mod
as a submodule in it.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
Initialize proj_master
.
$ cd /tmp
$ mkdir proj_master; cd proj_master
$ git init .
$ touch README
$ git add .; git commit -m "hello proj_master"
Initialize proj_mod
.
$ cd /tmp
$ mkdir proj_mod; cd proj_mod
$ git init .
$ touch README
$ git add .; git commit -m "hello proj_mod"
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.
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.
Simply click on the submodule > Edit and you can rename the submodule in the new window that opens up.
Git Submodules Moving a submodule If needed, create the parent directory of the new location of the submodule ( mkdir -p new/path/to ). Move all content from the old to the new directory ( mv -vi old/path/to/module new/path/to/submodule ). Make sure Git tracks this directory ( git add new/path/to ).
Select the repo you want to rename under Git repositories on the left and select .... Select Rename repository... from the menu. If the Repositories pane is not expanded, select > to expand it and display the list of repositories. Enter a new repo name in the Repository name field in the dialog, then select Rename.
To rename any file or folder, use git mv command which takes two arguments. The first argument is the source and the second is the destination. We can easily rename any file using the git command and the new name will be assigned to that file. We can rename the file using GitHub or the command line.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With