How To Remove A Submodule In Git?
In Git, a submodule is a repository embedded in your main repository. A submodule is useful when you want to use one project within another one but want to treat them as different projects with separate commits history. Sometimes, however, a developer might want to remove a git submodule.
To remove a submodule use the git rm command:
bashgit rm <name-of-submodule>
In this article, I will explain, step-by-step, how to remove a submodule, as well as answer some of the most common questions.
Let’s get to it 😎.
To remove a submodule first, we need to add one to our repository.
To add a submodule to a repository use:
bashgit submodule add <remote_url>
This will create a .gitmodules file with the submodule entry and the submodule folder.
To verify that the command worked use the git status command.
Then you will need to commit and push the submodule.
Note: If you want to RESET a submodule, read this article.
How to remove a git submodule?
To remove a submodule use:
bashgit rm <name-of-submodule>
This command will remove the .gitmodules file's submodule entry. Also, it will remove the submodule folder.
However, the submodule in the .git folder will be kept around to make checkouts of past commits possible.
If you want to remove those references as well you need to follow those steps:
1. Remove the submodule folder with:
bashrm -rf .git/modules/<path-to-submodule>
2. Remove the config with:
bashgit config --remove-section <submodule>
How to remove a git submodule but keep the files?
To remove a submodule but keep the files you need to follow those steps:
1. Rename your submodule's folder:
bashmv submodulefolder submodulefolder_tmp
2. De-initialize the submodule:
bashgit submodule deinit submodulefolder
3. Remove the folder using the git rm cached command:
bashgit rm --cached submodulefolder
4. Rename your submodule's folder again:
bashmv submodulefolder_tmp submodulefolder
Final thoughts
Although submodules aren't the most well-known feature of Git, developers need to know when and how to use them. As you can see, Git makes it easy for us to manage submodules by providing simple-to-use commands.
Here is a quick recap for you:
- To add a submodule use: git submodule add <remote_url>.
- To remove a submodule use: git rm <name-of-submodule>.
I have written more tutorials on git if you are interested in learning more about it and its commands.
If you have any questions please ask them in the comments section.
I always check for new comments and respond/answer to them.
Thank you for reading this article.
Please share it with your fellow developers and colleagues.