Skip to content

Instantly share code, notes, and snippets.

@pm-hwks
Last active August 6, 2023 15:16
Show Gist options
  • Save pm-hwks/62e5bb9376978a3fbd88adc19fe9ed05 to your computer and use it in GitHub Desktop.
Save pm-hwks/62e5bb9376978a3fbd88adc19fe9ed05 to your computer and use it in GitHub Desktop.
[Git submodule resync] Update Git submodule to latest commit on origin #git #github #git-submodule
## Pull git submodules after cloning project from GitHub
## https://stackoverflow.com/questions/16773642/pull-git-submodules-after-cloning-project-from-github
git submodule update --init --recursive
## Reference : https://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin
# Get the submodule initially
git submodule add ssh://bla submodule_dir
git submodule init
# Time passes, submodule upstream is updated
# and you now want to update
# Change to the submodule directory
cd submodule_dir
# Checkout desired branch
git checkout master
# Update
git pull
# Get back to your project root
cd ..
# Now the submodules are in the state you want, so
git commit -am "Pulled down update to submodule_dir"
## Alternative approach
git submodule foreach git pull origin master
## ( or )
git submodule update --remote --merge
### Reference : https://www.vogella.com/tutorials/GitSubmodules/article.html
### https://www.atlassian.com/git/tutorials/git-submodule
### Cloning a repository that contains submodules
git clone --recursive [URL to Git repo]
### If you already have cloned a repository and now want to load it’s submodules you have to use submodule update.
git submodule update --init
#### if there are nested submodules:
git submodule update --init --recursive
### Pulling with submodules
#### pull all changes in the repo including changes in the submodules
git pull --recurse-submodules
#### pull all changes for the submodules
git submodule update --remote
### Executing a command on every submodule
#### including nested submodules
git submodule foreach --recursive 'git reset --hard'
### Adding a submodule to a Git repository and tracking a branch
#### add submodule and define the master branch as the one you want to track
git submodule add -b master [URL to Git repo]
git submodule init
### update your submodule --remote fetches new commits in the submodules
#### and updates the working tree to the commit described by the branch
git submodule update --remote
### how to update a submodule to its latest commit in its master branch.
#### update submodule in the master branch
#### skip this if you use --recurse-submodules
#### and have the master branch checked out
cd [submodule directory]
git checkout master
git pull
#### commit the change in main repo
#### to use the latest commit in master of the submodule
cd ..
git add [submodule directory]
git commit -m "move submodule to latest commit in master"
#### share your changes
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment