Skip to content

Instantly share code, notes, and snippets.

@pamartineza
Last active June 28, 2023 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pamartineza/3ff2568584eec5bdb7ad55e39b507d17 to your computer and use it in GitHub Desktop.
Save pamartineza/3ff2568584eec5bdb7ad55e39b507d17 to your computer and use it in GitHub Desktop.
Git Submodules Cheatsheet
//Adding a submodule:
git submodule add git@github.com:GreenLionSoft/xxxx.git folderName
git submodule init
git submodule update
cd folderName
git checkout xxxx
//Config for checking submodules before pushing
git config push.recurseSubmodules check
http://blog.davidecoppola.com/2015/02/how-to-create-git-submodule-from-repository-subdirectory/
//Creating submodule from existing folder e.g. src/libs/Library1
2- CLONE Project REPOSITORY
The second step is cloning the Project repository to work on a new copy.
git clone git@REMOTE_URL:/path/to/git/repos/Project.git
3- FILTER SUBDIRECTORY
Now it’s time to rewrite the repository to look as if Library1/ had been its project root, and discard all other history.
git filter-branch --subdirectory-filter src/libs/Library1 -- --all
4- REPLACE ORIGIN
As we are working on a clone of Project, origin is still pointing to the remote repository of Project. We want to remove that and make origin point to the new remote repository of Library1.
git remote rm origin
git remote add origin git@REMOTE_URL:/path/to/git/repos/Library1.git
5- PUSH FILES TO REMOTE REPOSITORY
//Then we can push all the Library1 files and populate its remote repository.
git push origin master
//At this point Library1 is an independent git repository.
//DELETE Library1 FROM Project
//It’s time to remove the Library1/ directory from Project as we’re going to replace it with a submodule.
//Go into the root directory of Project and type the following commands:
git rm -r src/libs/Library1/
git commit -m "Removed Library1 directory to replace it with submodule."
//Now we can add Library1 as a submodule from the root directory of Project:
git submodule add git@REMOTE_URL:/path/to/git/repos/Library1.git src/libs/Library1
//Finally we can commit and push our changes
git commit -m "Replaced Library1 directory with submodule."
git push origin master
//Convenient git configuration
git config push.recurseSubmodules check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment