Skip to content

Instantly share code, notes, and snippets.

@andmax
Last active March 30, 2021 14:42
Show Gist options
  • Save andmax/402a9a989552f31e14fa98bce1b938a2 to your computer and use it in GitHub Desktop.
Save andmax/402a9a989552f31e14fa98bce1b938a2 to your computer and use it in GitHub Desktop.
Teaches migration of git repo from one url to another + git submodule
### To simply clone as git submodule do:
git submodule add <clone_repo_url>
git commit -am "new submodule added"
### To migrate git repo into submodule
git clone <old_repo_url>
git remote add <new_repo_name> <new_repo_url>
git push <new_repo_name> master
git remote rm origin
<edit .git/config to replace "new_repo_name" to "origin">
git push new_repo_name master
<edit .git/config to add branch "master" three lines>
[branch "master"]
remote = origin
merge = refs/heads/master
git clone <top_repo_url>
git submodule add <new_repo_url>
git commit -am "adding new_repo as submodule"
git push origin master
git clone <top_repo_url>
git submodule init
git submodule update
git pull --recurse-submodules
# The submodule init + update can be done with:
git submodule update --init
# The recurse submodules above makes all submodules HEAD detached, fix it by:
git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
# Display status of submodules when doing git status:
git config --global status.submoduleSummary true
# Scripting for pulling the main repo and updating submodules automatically:
#!/usr/bin/env bash
git pull "$@" &&
git submodule sync --recursive &&
git submodule update --init --recursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment