Git pass common submodule (main branch)

I have two or more projects (let them be called ProjectFoo and ProjectBar) that have some common code that I put in the submodule .

I understand that if I make changes to the submodule from ProjectFoo, it will be in a separate head, which only all ProjectFoo clones can see:

(master) $ cd ProjectFooBarCommoneSubmodule/ (master) $ git commit -am "Common code fix." (56f21fb0...) $ git push Everything up-to-date 

Perhaps because the master branch has not changed. Maybe I could do something like git checkout master && git merge Everything up-to-date , but that seems pretty ugly. Maybe git reset --hard master will do the same, but it seems even more ugly.

How to get common code shared by a project, updated from those projects that use it? In other words, fixing this submodule should update all the different repositories (repositories, not just clones) that use the same submodule.

---- EDIT ----

Apparently my seized repository was corrupted and broken. This should have worked right from the start (in ProjectFoo in this example):

 (master) $ cd ProjectFooBarCommoneSubmodule/ (master) $ git commit -am "Common code fix." (master) $ git push .... fbfdd71..0acce63 master -> master (master) $ cd .. (master) $ git add ProjectFooBarCommoneSubmodule (master) $ git commit -m "Submodule update." 

Then, to get this change in other projects, for example ProjectBar:

 (master) $ cd ProjectFooBarCommoneSubmodule/ (master) $ git pull 

The latest common code will be updated. A git checkout master may be required if it is on a separate head.

+45
git git-submodules commit
Aug 28 '10 at 10:32
source share
5 answers

Short answer:

 cd ProjectFooBarCommoneSubmodule git checkout master <Do your editing> git commit --all -m "Lots of fixes" git push submodule_origin master cd .. git add ProjectFooBarCommoneSubmodule git commit -m "Bumped up the revision of ProjectFooBarCommoneSubmodule" git push origin master 

The longer:

Submodules

Git is a dependency mechanism where the main project (say, A) defines the specified revision in the subproject (say, B) that will be used to build the project A. In order for the tool to be useful, the behavior must be predictable from the point of view of A: s . Dependencies cannot change unless someone decides to include the change in project A. All sorts of unpleasant things can happen: did the changes to the project B: s automatically import, of which the compilation errors are probably the best, since A would immediately notice the failures. This is why the B: s head is off.

State B is stored in (check git submodule status ), and the change change must be made and committed to A in order for it to have any effect. This is what happens in the above example, A changes the revision number stored in the repo and dials the version to the latest. This process will need to be repeated in another main repo, so the automatic "use of the master switch" AFAIK.

BTW. Git is the head of a book on submodules and my github account . Commits do not make sense and contain garbage, but tuning should be fine. Please check this to follow.

Both ProjectFoo and ProjectBar share code in a common submodule.

ProjectFooBarCommoneSubmodule: master 6850e4e4c1fac49de398

In ProjectFoo:

 git submodule status 

-6850e4e4c1fac49de39890703f21486ca04b87a0 general

In ProjectBar:

 git submodule status 

-6850e4e4c1fac49de39890703f21486ca04b87a0 general

So both point to the same revision, right? The trick here is to see that ProjectFoo and ProjectBar point to the version (6850e4e4c1fac49de39890703f21486ca04b87a0) not a branch (master), although they are the same thing. The first is a separate head, and the other is a named branch.

If you want to make some corrections in ProjectFooBarCommoneSubmodule, you can go to a subdirectory, for example. ProjectFoo and select the branch instead of the revision :

 git checkout master <Do your coding and pushing here> 

Then go up one directory and check the status of the git submodule. He must tell you that you are not synchronized right now. for example

 git submodule status 

+ e24bd2bf45d52171a63b67ac05cd4be0ac965f60 general (heads / master-1-ge24bd2b)

Now you can do git add to set the link to this particular commit (ge24bd ...), commit, and after that the link to the submodule points to this revision, which is also the wizard for ProjectFooBarCommoneSubmodule.

Now you need to update the link in ProjectBar. Go to ProjectBar / common and do git fetch origin (this is a quick merge forward), do

 git checkout master cd .. git add common git commit -m "Bumped up the revision" git push origin master # to publish the revision bump to everybody else 

So, as with any git repository, you don't have to work with a separate head. You can either work with the wizard or create a named branch. Either way, make sure that the upstream stream contains changes to ProjectFooBarCommoneSubmodule, or you break ProjectFoo and ProjectBar if they refer to something that doesn't exist. Hope this explained it better.

+59
Aug 28 '10 at 21:27
source share

On submodule : git push origin HEAD:master

+2
Apr 14 '16 at 11:43
source share

If you want to lock and push all submodules at once: git submodule foreach 'git commit -a' ; git submodule foreach 'git push --all' ; git commit -a && \ git push --all --recurse-submodules=on-demand git submodule foreach 'git commit -a' ; git submodule foreach 'git push --all' ; git commit -a && \ git push --all --recurse-submodules=on-demand

0
Nov 03 '16 at 13:09
source share

To merge changes from a separate HEAD to master, run:

 git rebase HEAD master 

then checkout wizard (use -f for strength):

 git checkout master 

If you have several submodules to work with, use: git submodule foreach , for example.

 git submodule foreach git pull origin master -r 
0
Jan 14 '17 at 1:13
source share

I just do:

git submodule foreach git push -u origin master

0
Jul 07 '17 at 0:18
source share



All Articles