Git how to update submodule
Git how to update submodule
How to only update specific git submodules?
So, updating all my submodules is done by running
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I end up there by searching how to update specific submodule only, which means for me, updating a submodule to the ref pointed by its super-repo. Which is not the question nor the answer but only the title.
So in a hope of helping some others like me the answer to the question title is :
which will put this submodule in the state of the ref commited in the super-repo.
—remote This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch. The remote used is branch’s remote (branch..remote), defaulting to origin.
In order to update a specific submodule you can use :
In your case, it should be :
Actually the proper syntax is:
If you’ve just cloned a repo with submodules, you can clone a specific submodule with:
This will clone the master of that submodule, from their you can cd into the submodule and pull whatever branches you need.
As part of the Git 2.13 (Q2 2017) «active submodule» feature (see «Ignore new commits for git submodule «), you have this commit bb62e0a from Brandon Williams ( bmwill ):
This also configures the ‘ submodule.active ‘ configuration option to be the given pathspec, such that any future invocation of git submodule update will keep up with the pathspec.
After the clone is created, initialize and clone submodules within based on the provided pathspec.
If no pathspec is provided, all submodules are initialized and cloned.
parse-options : don’t emit «ambiguous option» for aliases
But due to the way the options parsing machinery works this resulted in the rather absurd situation of:
Add OPT_ALIAS() to express this link between two or more options and use it in git-clone.
Git submodule update
I’m not clear on what the following means (from the Git submodule update documentation):
My main use case is to have a bunch of central repositories, which I will embed via submodules into other repositories. I would like to be able to improve on these central repositories, either directly in their original location, or from within their embedding repositories (the ones that use them via submodule).
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
This GitPro page does summarize the consequence of a git submodule update nicely
Note March 2013:
As mentioned in «git submodule tracking latest», a submodule now (git1.8.2) can track a branch.
MindTooth’s answer illustrate a manual update (without local configuration):
In both cases, that will change the submodules references (the gitlink, a special entry in the parent repo index), and you will need to add, commit and push said references from the main repo.
Next time you will clone that parent repo, it will populate the submodules to reflect those new SHA1 references.
The rest of this answer details the classic submodule feature (reference to a fixed commit, which is the all point behind the notion of a submodule).
Switching branches with submodules in them can also be tricky. If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory:
So, to answer your questions:
can I create branches/modifications and use push/pull just like I would in regular repos, or are there things to be cautious about?
You can create a branch and push modifications.
WARNING (from Git Submodule Tutorial): Always publish (push) the submodule change before publishing (push) the change to the superproject that references it. If you forget to publish the submodule change, others won’t be able to clone the repository.
how would I advance the submodule referenced commit from say (tagged) 1.0 to 1.1 (even though the head of the original repo is already at 2.0)
Git submodules are implemented using two moving parts:
These together triangulate a specific revision of a specific repository which is checked out into a specific location in your project.
you cannot modify the contents of the submodule from within the main project
100% correct: you cannot modify a submodule, only refer to one of its commits.
This is why, when you do modify a submodule from within the main project, you:
A submodule enables you to have a component-based approach development, where the main project only refers to specific commits of other components (here «other Git repositories declared as sub-modules»).
A submodule is a marker (commit) to another Git repository which is not bound by the main project development cycle: it (the «other» Git repo) can evolves independently.
It is up to the main project to pick from that other repo whatever commit it needs.
However, should you want to, out of convenience, modify one of those submodules directly from your main project, Git allows you to do that, provided you first publish those submodule modifications to its original Git repo, and then commit your main project refering to a new version of said submodule.
But the main idea remains: referencing specific components which:
The list of specific commits you are refering to in your main project defines your configuration (this is what Configuration Management is all about, englobing mere Version Control System)
If a component could really be developed at the same time as your main project (because any modification on the main project would involve modifying the sub-directory, and vice-versa), then it would be a «submodule» no more, but a subtree merge (also presented in the question Transferring legacy code base from cvs to distributed repository), linking the history of the two Git repo together.
Does that help understanding the true nature of Git Submodules?
Update a submodule to the latest commit
I have a project A which is a library and it is used in a project B.
Both projects A and B have a separate repository on github BUT inside B we have a submodule of A.
I edited some classes on the library, which is in the repo A, I pushed on the remote repo, so the library (repo A) is updated.
These updates do not reflect on the «reference» (the submodule) the submodule refers to a previous commit. what should I do in order to update the submodule on git?
9 Answers 9
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Enter the submodule directory:
Pull the repo from you project A (will not update the git status of your parent, project B):
Go back to the root directory & check update:
If the submodule updated before, it will show something like below:
Then, commit the update:
UPDATE
As @paul pointed out, since git 1.8, we can use
to update the submodule to the latest remote commit. It’ll be convenient in most cases.
Since git 1.8 you can do
This will update the submodule to the latest remote commit. You will then need to add and commit the change so the gitlink in the parent repository is updated:
First, git add it
then git commit it
the git push it
And then push the changes as without this, the SHA-1 identity the pointing to the submodule won’t be updated and so the change won’t be visible to anyone else.
If you update a submodule and commit to it, you need to go to the containing, or higher level repo and add the change there.
will show something like:
The fact that the submodule is out of sync can also be seen with
the output will show:
The plus indicates that the your submodule is pointing ahead of where the top repo expects it to point to.
simply add this change:
When you push up your changes, make sure you push up the change in the submodule first and then push the reference change in the outer repo. This way people that update will always be able to successfully run
How to Add and Update Git Submodules
A Submodule is a Git Repository that is a subdirectory of another Git Repository. This Git Repository is called the parent of the Submodule. A lot of times the main project will need code from some other external repository or library. We can simply copy and paste the code needed from this external repository into our main project. But this approach is not the most efficient one. Let’s learn more about Submodules and how to use them in Git.
Submodule
A Submodule is a Git Repository inside another Git Repository. This embedded Git Repository can be managed independently and will have its own Git workflow. We can also use this embedded repository as a Submodule for various other repositories without creating new files from scratch for each repository.
Why are Submodules Used?
Git Submodule Commands
Now that we know what Submodules are and what they are used for, let’s look at how to create and use submodules in Git.
Adding a Submodule
To add a submodule we use the Git Submodule Add command. We need to pass the remote repository URL where the project that we want to embed is hosted. It is a good idea to first create a separate subdirectory in your repository and then add all the submodules to that subdirectory.
Alternatively, we can first navigate to the destination folder and then run the above command without adding the subdirectory name.
After running the above command Git will first start cloning the remote repository.
A hidden file is created with the name of .gitmodules in the parent repository. This is the file where Git will keep track of all our submodules.
Also, our Git configurations are altered to add the new submodule.
Cloning a Repository with Submodules
When we clone a repository Git will not download any of the submodules of that repository. There are two ways to tell Git that we also want it to clone the submodules.
We can use —recurse-submodules option with the Git Clone command to also clone the submodules.
Or we can use the Git Submodule Update command with the —init and the —recursive command after cloning the repository using the Git Clone command.
Updating a Submodule
A repository that we are using as a submodule may be maintained by some other team of developers and they may make changes to this remote repository. To update our version of the submodule we use the Git Submodule Update command with the —remote and —merge options. We need to run this option
The above command fetches and merges the new commits into the submodule. To just fetch the new commits we can simply navigate to the submodule and run the Git Fetch command. If after examining the commits we want to merge them then we can use the Git Merge command.
Removing a Submodule
We may need to remove an unwanted submodule. This can be done by using the Git Submodule Deinit command. This will safely remove the submodule from all the configuration files. Then we can simply delete the submodule by using the Git Rm command.
Summary
Submodules are very useful when our project increases in size and we need separate repositories for different components of our project. It is also very helpful when we have to use some external libraries for our project. Instead of copying files and pasting them all into a single repository we can better maintain and manage our repository by using submodules. The Git Submodule command can be used to add and update submodules.
Update Git submodule to latest commit on origin
I have a project with a Git submodule. It is from an ssh://. URL, and is on commit A. Commit B has been pushed to that URL, and I want the submodule to retrieve the commit, and change to it.
Now, my understanding is that git submodule update should do this, but it doesn’t. It doesn’t do anything (no output, success exit code). Here’s an example:
16 Answers 16
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The git submodule update command actually tells Git that you want your submodules to each check out the commit already specified in the index of the superproject. If you want to update your submodules to the latest commit available from their remote, you will need to do this directly in the submodules.
Or, if you’re a busy person:
will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule. As the documentation puts it:
—remote
This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.
This is equivalent to running git pull in each submodule, which is generally exactly what you want.
In your project parent directory, run:
Or if you have recursive submodules run:
Sometimes this still doesn’t work, because somehow you have local changes in the local submodule directory while the submodule is being updated.
Most of the time the local change might not be the one you want to commit. It can happen due to a file deletion in your submodule, etc. If so, do a reset in your local submodule directory and in your project parent directory, run again:
Now go back to the main project, stage the submodule and commit that:
Now push your new version of the main project:
From this point on, if anyone else updates their main project, then git submodule update for them will update the submodule, assuming it’s been initialized.
It seems like two different scenarios are being mixed together in this discussion:
Scenario 1
Using my parent repository’s pointers to submodules, I want to check out the commit in each submodule that the parent repository is pointing to, possibly after first iterating through all submodules and updating/pulling these from remote.
This is, as pointed out, done with
Scenario 2, which I think is what OP is aiming at
New stuff has happened in one or more submodules, and I want to 1) pull these changes and 2) update the parent repository to point to the HEAD (latest) commit of this/these submodules.
This would be done by
Not very practical, since you would have to hardcode n paths to all n submodules in e.g. a script to update the parent repository’s commit pointers.
It would be cool to have an automated iteration through each submodule, updating the parent repository pointer (using git add ) to point to the head of the submodule(s).
For this, I made this small Bash script:
git-update-submodules.sh
To run it, execute
Elaboration
Then some submodule initializing, might be necessary, if new submodules have been added or are not initialized yet:
Then I update/pull all submodules:
After a possible successful pull (if new stuff was found on the remote), I do a push to ensure that a possible merge-commit is not left behind on the client. Again, it only happens if a pull actually brought in new stuff.
Finally, the final || true is ensuring that script continues on errors. To make this work, everything in the iteration must be wrapped in the double-quotes and the Git commands are wrapped in parentheses (operator precedence).
My favourite part:
Finally, a commit with some message explaining that the parent repository was updated. This commit will be ignored by default, if nothing was done. Push this to origin, and you’re done.
I have a script running this in a Jenkins job that chains to a scheduled automated deployment afterwards, and it works like a charm.
Источники информации:
- http://stackoverflow.com/questions/1979167/git-submodule-update
- http://stackoverflow.com/questions/8191299/update-a-submodule-to-the-latest-commit
- http://www.studytonight.com/git-guide/how-to-add-and-update-git-submodules
- http://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin