-
Create the submodule:
git submodule add -b master <git@github.com:MYSUBMODULE.git> <path/to/MYSUBMODULE>
This creates the submodule and makes it track submodule's
masterbranch. You can change the branch name if you want to track a different branch. -
Change some settings. In the parent repo:
# make it so that git status will include changes to submodules. git config status.submodulesummary 1 # make it so that git diff will show a list of submodule commit messages # instead of just the previous and current commit hashes. git config diff.submodule log
Either one of these can take the
--globalflag. -
Make sure the submodule is on the correct branch and not in a detached head.
cd <MYSUBMODULE> git checkout master
-
Pulling submodule remote changes:
# to merge remote changes on top of your local ones git submodule update --remote --merge # Or to rewind your changes, apply remote changes, then replay your changes afterward git submodule update --remote --rebase
If you don’t specify either
--mergeor--rebaseoptions, your changes will live on in whatever branch of the submodule you made them in, but the submodule will revert to detached head state before applying the remote changes. -
Pushing submodule local changes
cd <MYSUBMODULE> git add <stuff> git commit -m "comment" git push
And then to update the parent repo to track the updated verison of your submodule:
cd .. git add <MYSUBMODULE> git commit -m "updated my submodule" git push
In the parent repo, you can also use
git push --recurse-submodules=checkwhich prevents pushing the parent repo if the submodule(s) are not pushed first. Another option isgit push --recurse-submodules=on-demandwhich will try to push the submodules automatically (if necessary) before pushing the parent repo.
-
If ended up with a detached HEAD, it can be pushed to a remote repository (without the need to creating a new local branch) by the following command:
git push <remote name> HEAD:<remote branch name>
E.g.
git push origin HEAD:master
if got this error:
Server does not allow request for unadvertised object 1234...Do this:
git submodule sync-
NOTE: You will lose your submodule’s git history if you follow this solution! If you value the submodule history and won’t accept this compromise, do not follow these instructions!
- Move the files and deinit the submodule
mv yoursubmodule yoursubmodule_tmp git submodule deinit yourSubmodule git rm --cached yourSubmodule mv yoursubmodule_tmp yoursubmodule git add yoursubmodule
- Git submodules metadata is stored in the .gitmodules file as shown below. Remove the submodule from that file:
[submodule "yoursubmodule"] path = path/to/yoursubmodule url = git@github.com/exampleUser/models-
.git/config has a similar entry, remove the submodule from that as well.
-
Cleanup the .git/modules directory
rm -rf .git/modules/yoursubmodule
- Switching to the local branch you want to rename and rename it:
git checkout <old_name>
git branch -m <new_name>- If the <old_name> branch has been pushed to the remote repository, delete it from remote:
git push origin --delete <old_name>- Push the <new_name> branch and reset the upstream branch:
git push origin -u <new_name>
This was really helpful thank you