Remove Git Submodule Note

Posted on: September 01, 2023

Remove remote origin

After cloning a git repository from someone else, we want to remove its remote origin and add our own remote origin to push to our github:

Bash
# Remove remote origin
git remote rm origin
# Add our own remote origin
git remote add origin git@github.com:<username>/<gitrepo_name>.git

Remove submodule

Sometimes we would like to push a repository that already has another git repository inside, we could add that git repo as a submodule, but sometimes we want to remove that submodule:

Bash
# Navigate inside the outer repository (the one above the submodule)
cd path/to/directory
# Remove the submodule from its tracking
git rm -r --cached <submodule_repo>
# Check to see that the submodule repo is untracked
git status
# Remove the .git folder in submodule
rm -rf <submodule_repo>/.git
# Now you can add and push the repo
git add <submodule_repo>
git commit -m "Fix submodule"
git push