How can I specify a branch/tag when adding a Git submodule?












581














How does git submodule add -b work?



After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").



I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?



Also, is it possible to specify a tag instead of a branch?



I'm using version 1.6.5.2.










share|improve this question




















  • 2




    If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
    – VonC
    Sep 14 '13 at 7:00
















581














How does git submodule add -b work?



After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").



I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?



Also, is it possible to specify a tag instead of a branch?



I'm using version 1.6.5.2.










share|improve this question




















  • 2




    If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
    – VonC
    Sep 14 '13 at 7:00














581












581








581


297





How does git submodule add -b work?



After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").



I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?



Also, is it possible to specify a tag instead of a branch?



I'm using version 1.6.5.2.










share|improve this question















How does git submodule add -b work?



After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").



I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?



Also, is it possible to specify a tag instead of a branch?



I'm using version 1.6.5.2.







git git-submodules






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 6 '18 at 16:20









Fabio Turati

2,55752138




2,55752138










asked Nov 22 '09 at 4:55









Ivan

26.3k144353




26.3k144353








  • 2




    If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
    – VonC
    Sep 14 '13 at 7:00














  • 2




    If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
    – VonC
    Sep 14 '13 at 7:00








2




2




If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
– VonC
Sep 14 '13 at 7:00




If you have an existing submodule which isn't tracking a branch yet, but you wish it now would track a branch... see my answer below
– VonC
Sep 14 '13 at 7:00












11 Answers
11






active

oldest

votes


















614














Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.





It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.



This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.



This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.



If you want to move the submodule to a particular tag:



cd submodule_directory
git checkout v1.0
cd ..
git add submodule_directory
git commit -m "moved submodule to v1.0"
git push


Then, another developer who wants to have submodule_directory changed to that tag, does this



git pull
git submodule update


git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.






share|improve this answer



















  • 5




    That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
    – Ivan
    Nov 22 '09 at 18:19






  • 3




    This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
    – James A. Rosen
    May 4 '12 at 21:12






  • 3




    It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
    – Rudolf Olah
    Apr 30 '13 at 18:15






  • 8




    Is git checkout v1.0 a branch or a tag?
    – Bernhard Döbler
    Oct 1 '15 at 22:30






  • 6




    Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
    – deadbabykitten
    Feb 2 '16 at 0:13



















439














I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.



You know you have a Git submodule when you have these two things.





  1. Your .gitmodules has an entry like so:



    [submodule "SubmoduleTestRepo"]
    path = SubmoduleTestRepo
    url = https://github.com/jzaccone/SubmoduleTestRepo.git



  2. You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.



    Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules.



    Now, all the -b does is add one line in your .gitmodules file. So following the same example it would look like this:



    [submodule "SubmoduleTestRepo"]
    path = SubmoduleTestRepo
    url = https://github.com/jzaccone/SubmoduleTestRepo.git
    branch = master


    EDIT: only branch name is supported above, not SHA or TAG.



    The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer:



    git submodule update --remote


    Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.



    git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.








share|improve this answer



















  • 41




    This answer was the only one that made sense to me. thanks.
    – cgTag
    Mar 1 '15 at 16:20






  • 9




    This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
    – dtmland
    Mar 25 '15 at 16:16








  • 8




    Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
    – Hhut
    Sep 4 '15 at 8:19






  • 4




    This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
    – CMCDragonkai
    Apr 4 '16 at 14:17






  • 2




    Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
    – infoclogged
    Jun 29 '16 at 15:27



















257














Note that if you have an existing submodule which isn't tracking a branch yet, then (if you have git 1.8.2+):





  • Make sure the parent repo knows that its submodule now tracks a branch:



    cd /path/to/your/parent/repo
    git config -f .gitmodules submodule.<path>.branch <branch>



  • Make sure your submodule is actually at the latest of that branch:



    cd path/to/your/submodule
    git checkout -b branch --track origin/branch
    # if the master branch already exist:
    git branch -u origin/master master



        
(with 'origin' being the name of the upstream remote repo the submodule has been cloned from.

        
A git remote -v inside that submodule will display it. Usually, it is 'origin')





  • Don't forget to record the new state of your submodule in your parent repo:



    cd /path/to/your/parent/repo
    git add path/to/your/submodule
    git commit -m "Make submodule tracking a branch"



  • Subsequent update for that submodule will have to use the --remote option:



    # update your submodule
    # --remote will also fetch and ensure that
    # the latest commit from the branch is used
    git submodule update --remote

    # to avoid fetching use
    git submodule update --remote --no-fetch





Note that with Git 2.10+ (Q3 2016), you can use '.' as a branch name:




The name of the branch is recorded as submodule.<name>.branch in .gitmodules for update --remote.
A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.






If you want to update all your submodules following a branch:



    git submodule update --recursive --remote


Note that the result, for each updated submodule, will almost always be a detached HEAD, as Dan Cameron note in his answer.



(Clintm notes in the comments that, if you run git submodule update --remote and the resulting sha1 is the same as the branch the submodule is currently on, it won't do anything and leave the submodule still "on that branch" and not in detached head state.)



To ensure the branch is actually checked out (and that won't modify the SHA1 of the special entry representing the submodule for the parent repo), he suggests:



git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'


Each submodule will still reference the same SHA1, but if you do make new commits, you will be able to push them because they will be referenced by the branch you want the submodule to track.

After that push within a submodule, don't forget to go back to the parent repo, add, commit and push the new SHA1 for those modified submodules.



Note the use of $toplevel, recommended in the comments by Alexander Pogrebnyak.
$toplevel was introduced in git1.7.2 in May 2010: commit f030c96.




it contains the absolute path of the top level directory (where .gitmodules is).




dtmland adds in the comments:




The foreach script will fail to checkout submodules that are not following a branch.

However, this command gives you both:




 git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch' –


The same command but easier to read:



git submodule foreach -q --recursive 
'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)";
[ "$branch" = "" ] &&
git checkout master || git checkout $branch' –




umläute refines dtmland's command with a simplified version in the comments:



git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'


multiple lines:



git submodule foreach -q --recursive 
'git checkout
$(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'





share|improve this answer



















  • 1




    Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
    – BraveNewMath
    Sep 17 '13 at 20:41








  • 1




    The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
    – Alexander Pogrebnyak
    Nov 27 '13 at 18:19






  • 1




    The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
    – dtmland
    Jul 10 '15 at 19:09






  • 2




    here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
    – umläute
    Oct 15 '15 at 11:54






  • 1




    Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
    – GregTom
    Apr 17 '17 at 4:16





















160














Git 1.8.2 added the possibility to track branches.



# add submodule to track master branch
git submodule add -b master [URL to Git repo];

# update your submodule
git submodule update --remote


See also Git submodules






share|improve this answer

















  • 2




    Does this apply to tags as well?
    – ThorSummoner
    Jul 2 '14 at 21:28






  • 1




    How does adding submodule in such way reflect on .gitmodules file?
    – Eugene
    Jul 20 '14 at 12:22








  • 1




    Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
    – Dinis Cruz
    Dec 24 '14 at 2:03






  • 2




    You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
    – KCD
    Oct 16 '15 at 2:27








  • 2




    @KCD Which version of git can do that with tags. Mine doesn't work?
    – CMCDragonkai
    Apr 4 '16 at 13:56



















49














An example of how I use Git submodules.




  1. Create a new repository

  2. Then clone another repository as a submodule

  3. Then we have that submodule use a tag called V3.1.2

  4. And then we commit.


And that looks a little bit like this:



git init 
vi README
git add README
git commit
git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
git status

git submodule init
git submodule update

cd stm32_std_lib/
git reset --hard V3.1.2
cd ..
git commit -a

git submodule status


Maybe it helps (even though I use a tag and not a branch)?






share|improve this answer



















  • 4




    It's basically the same answer as djacobs7, but thanks anyway :)
    – Ivan
    Nov 22 '09 at 18:22






  • 1




    Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
    – Nick Radford
    Oct 10 '12 at 20:07






  • 1




    @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
    – Michel Müller
    Aug 12 '13 at 8:14










  • djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
    – CodeMonkey
    Aug 22 '17 at 6:04



















34














In my experience switching branches in the superproject or future checkouts will still cause detached HEADs of submodules regardless if the submodule is properly added and tracked (i.e. @djacobs7 and @Johnny Z answers).



And instead of manually checking out the correct branch manually or through a script git submodule foreach can be used.



This will check the submodule config file for the branch property and checkout the set branch.



git submodule foreach -q --recursive 'branch="$(git config -f <path>.gitmodules submodule.$name.branch)"; git checkout $branch'






share|improve this answer





















  • Nice. +1. I have included your command in my answer.
    – VonC
    Nov 9 '13 at 8:17



















28














Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.



This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.



Then imagine that someone commits a change to repository bar before you can make your clone.



When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.



Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.






share|improve this answer























  • I think it's time I start reading some git books, thanks for the recommendation.
    – Ivan
    Nov 22 '09 at 18:21










  • Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
    – momo
    Dec 1 '11 at 12:44






  • 5




    The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
    – jerclarke
    Oct 25 '12 at 18:23










  • @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
    – Alastair
    Nov 7 '12 at 4:01



















12














To switch branch for a submodule (assuming you already have the submodule as part of the repository):





  • cd to root of your repository containing the submodules

  • Open .gitmodules for editing

  • Add line below path = ... and url = ... that says branch = your-branch, for each submodule; save file .gitmodules.

  • then without changing directory do $ git submodule update --remote


...this should pull in the latest commits on the specified branch, for each submodule thus modified.






share|improve this answer































    7














    I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.



    [alias]

    ######################
    #
    #Submodules aliases
    #
    ######################


    #git sm-trackbranch : places all submodules on their respective branch specified in .gitmodules
    #This works if submodules are configured to track a branch, i.e if .gitmodules looks like :
    #[submodule "my-submodule"]
    # path = my-submodule
    # url = git@wherever.you.like/my-submodule.git
    # branch = my-branch
    sm-trackbranch = "! git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'"

    #sm-pullrebase :
    # - pull --rebase on the master repo
    # - sm-trackbranch on every submodule
    # - pull --rebase on each submodule
    #
    # Important note :
    #- have a clean master repo and subrepos before doing this !
    #- this is *not* equivalent to getting the last committed
    # master repo + its submodules: if some submodules are tracking branches
    # that have evolved since the last commit in the master repo,
    # they will be using those more recent commits !
    #
    # (Note : On the contrary, git submodule update will stick
    #to the last committed SHA1 in the master repo)
    #
    sm-pullrebase = "! git pull --rebase; git submodule update; git sm-trackbranch ; git submodule foreach 'git pull --rebase' "

    # git sm-diff will diff the master repo *and* its submodules
    sm-diff = "! git diff && git submodule foreach 'git diff' "

    #git sm-push will ask to push also submodules
    sm-push = push --recurse-submodules=on-demand

    #git alias : list all aliases
    #useful in order to learn git syntax
    alias = "!git config -l | grep alias | cut -c 7-"





    share|improve this answer































      3














      We use Quack to pull a specific module from another Git repository. We need to pull code without the whole code base of the provided repository - we need a very specific module / file from that huge repository and should be updated every time we run update.



      So we achieved it in this way:



      Create configuration



      name: Project Name

      modules:
      local/path:
      repository: https://github.com/<username>/<repo>.git
      path: repo/path
      branch: dev
      other/local/path/filename.txt:
      repository: https://github.com/<username>/<repo>.git
      hexsha: 9e3e9642cfea36f4ae216d27df100134920143b9
      path: repo/path/filename.txt

      profiles:
      init:
      tasks: ['modules']


      With the above configuration, it creates one directory from the provided GitHub repository as specified in first module configuration, and the other one is to pull and create a file from the given repository.



      Other developers just need to run



      $ quack


      And it pulls the code from the above configurations.






      share|improve this answer































        0














        The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.



        You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
        The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote.
        If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.



        With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with




        fatal: Needed a single revision



        Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'







        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1777854%2fhow-can-i-specify-a-branch-tag-when-adding-a-git-submodule%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          11 Answers
          11






          active

          oldest

          votes








          11 Answers
          11






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          614














          Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.





          It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.



          This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.



          This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.



          If you want to move the submodule to a particular tag:



          cd submodule_directory
          git checkout v1.0
          cd ..
          git add submodule_directory
          git commit -m "moved submodule to v1.0"
          git push


          Then, another developer who wants to have submodule_directory changed to that tag, does this



          git pull
          git submodule update


          git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.






          share|improve this answer



















          • 5




            That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
            – Ivan
            Nov 22 '09 at 18:19






          • 3




            This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
            – James A. Rosen
            May 4 '12 at 21:12






          • 3




            It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
            – Rudolf Olah
            Apr 30 '13 at 18:15






          • 8




            Is git checkout v1.0 a branch or a tag?
            – Bernhard Döbler
            Oct 1 '15 at 22:30






          • 6




            Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
            – deadbabykitten
            Feb 2 '16 at 0:13
















          614














          Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.





          It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.



          This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.



          This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.



          If you want to move the submodule to a particular tag:



          cd submodule_directory
          git checkout v1.0
          cd ..
          git add submodule_directory
          git commit -m "moved submodule to v1.0"
          git push


          Then, another developer who wants to have submodule_directory changed to that tag, does this



          git pull
          git submodule update


          git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.






          share|improve this answer



















          • 5




            That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
            – Ivan
            Nov 22 '09 at 18:19






          • 3




            This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
            – James A. Rosen
            May 4 '12 at 21:12






          • 3




            It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
            – Rudolf Olah
            Apr 30 '13 at 18:15






          • 8




            Is git checkout v1.0 a branch or a tag?
            – Bernhard Döbler
            Oct 1 '15 at 22:30






          • 6




            Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
            – deadbabykitten
            Feb 2 '16 at 0:13














          614












          614








          614






          Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.





          It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.



          This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.



          This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.



          If you want to move the submodule to a particular tag:



          cd submodule_directory
          git checkout v1.0
          cd ..
          git add submodule_directory
          git commit -m "moved submodule to v1.0"
          git push


          Then, another developer who wants to have submodule_directory changed to that tag, does this



          git pull
          git submodule update


          git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.






          share|improve this answer














          Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.





          It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.



          This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.



          This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.



          If you want to move the submodule to a particular tag:



          cd submodule_directory
          git checkout v1.0
          cd ..
          git add submodule_directory
          git commit -m "moved submodule to v1.0"
          git push


          Then, another developer who wants to have submodule_directory changed to that tag, does this



          git pull
          git submodule update


          git pull changes which commit their submodule directory points to. git submodule update actually merges in the new code.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 2 '14 at 14:59









          jlengstorf

          292129




          292129










          answered Nov 22 '09 at 9:19









          djacobs7

          7,05521727




          7,05521727








          • 5




            That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
            – Ivan
            Nov 22 '09 at 18:19






          • 3




            This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
            – James A. Rosen
            May 4 '12 at 21:12






          • 3




            It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
            – Rudolf Olah
            Apr 30 '13 at 18:15






          • 8




            Is git checkout v1.0 a branch or a tag?
            – Bernhard Döbler
            Oct 1 '15 at 22:30






          • 6




            Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
            – deadbabykitten
            Feb 2 '16 at 0:13














          • 5




            That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
            – Ivan
            Nov 22 '09 at 18:19






          • 3




            This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
            – James A. Rosen
            May 4 '12 at 21:12






          • 3




            It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
            – Rudolf Olah
            Apr 30 '13 at 18:15






          • 8




            Is git checkout v1.0 a branch or a tag?
            – Bernhard Döbler
            Oct 1 '15 at 22:30






          • 6




            Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
            – deadbabykitten
            Feb 2 '16 at 0:13








          5




          5




          That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
          – Ivan
          Nov 22 '09 at 18:19




          That's a very good explanation, thanks! And of course, after reading your answer, I realized the commit is saved inside the submodule itself (submodule/.git/HEAD).
          – Ivan
          Nov 22 '09 at 18:19




          3




          3




          This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
          – James A. Rosen
          May 4 '12 at 21:12




          This doesn't seem to work on git 1.7.4.4. cd my_submodule; git checkout [ref in submodule's repository yields fatal: reference is not a tree: .... It's as if git will only operate on the parent repository.
          – James A. Rosen
          May 4 '12 at 21:12




          3




          3




          It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
          – Rudolf Olah
          Apr 30 '13 at 18:15




          It's good to use git submodules even for projects that are updated often. The linux kernel uses it and it isn't so bad
          – Rudolf Olah
          Apr 30 '13 at 18:15




          8




          8




          Is git checkout v1.0 a branch or a tag?
          – Bernhard Döbler
          Oct 1 '15 at 22:30




          Is git checkout v1.0 a branch or a tag?
          – Bernhard Döbler
          Oct 1 '15 at 22:30




          6




          6




          Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
          – deadbabykitten
          Feb 2 '16 at 0:13




          Consider a tag a human readable alias to a commit. And a commit is a set of specific state for each file. A branch is essentially the same thing except you can make changes to it.
          – deadbabykitten
          Feb 2 '16 at 0:13













          439














          I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.



          You know you have a Git submodule when you have these two things.





          1. Your .gitmodules has an entry like so:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git



          2. You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.



            Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules.



            Now, all the -b does is add one line in your .gitmodules file. So following the same example it would look like this:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git
            branch = master


            EDIT: only branch name is supported above, not SHA or TAG.



            The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer:



            git submodule update --remote


            Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.



            git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.








          share|improve this answer



















          • 41




            This answer was the only one that made sense to me. thanks.
            – cgTag
            Mar 1 '15 at 16:20






          • 9




            This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
            – dtmland
            Mar 25 '15 at 16:16








          • 8




            Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
            – Hhut
            Sep 4 '15 at 8:19






          • 4




            This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
            – CMCDragonkai
            Apr 4 '16 at 14:17






          • 2




            Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
            – infoclogged
            Jun 29 '16 at 15:27
















          439














          I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.



          You know you have a Git submodule when you have these two things.





          1. Your .gitmodules has an entry like so:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git



          2. You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.



            Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules.



            Now, all the -b does is add one line in your .gitmodules file. So following the same example it would look like this:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git
            branch = master


            EDIT: only branch name is supported above, not SHA or TAG.



            The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer:



            git submodule update --remote


            Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.



            git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.








          share|improve this answer



















          • 41




            This answer was the only one that made sense to me. thanks.
            – cgTag
            Mar 1 '15 at 16:20






          • 9




            This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
            – dtmland
            Mar 25 '15 at 16:16








          • 8




            Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
            – Hhut
            Sep 4 '15 at 8:19






          • 4




            This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
            – CMCDragonkai
            Apr 4 '16 at 14:17






          • 2




            Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
            – infoclogged
            Jun 29 '16 at 15:27














          439












          439








          439






          I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.



          You know you have a Git submodule when you have these two things.





          1. Your .gitmodules has an entry like so:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git



          2. You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.



            Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules.



            Now, all the -b does is add one line in your .gitmodules file. So following the same example it would look like this:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git
            branch = master


            EDIT: only branch name is supported above, not SHA or TAG.



            The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer:



            git submodule update --remote


            Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.



            git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.








          share|improve this answer














          I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.



          You know you have a Git submodule when you have these two things.





          1. Your .gitmodules has an entry like so:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git



          2. You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do git submodule status from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.



            Whenever you do a git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the .gitmodules.



            Now, all the -b does is add one line in your .gitmodules file. So following the same example it would look like this:



            [submodule "SubmoduleTestRepo"]
            path = SubmoduleTestRepo
            url = https://github.com/jzaccone/SubmoduleTestRepo.git
            branch = master


            EDIT: only branch name is supported above, not SHA or TAG.



            The submodule object is still pointing at a specific commit. The only thing that the -b option buys you is the ability to add a --remote flag to your update as per Vogella's answer:



            git submodule update --remote


            Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.



            git submodule add -b is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the .gitmodules file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 19 '18 at 20:10









          Peter Mortensen

          13.5k1983111




          13.5k1983111










          answered Sep 14 '13 at 2:33









          Johnny Z

          6,98521932




          6,98521932








          • 41




            This answer was the only one that made sense to me. thanks.
            – cgTag
            Mar 1 '15 at 16:20






          • 9




            This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
            – dtmland
            Mar 25 '15 at 16:16








          • 8




            Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
            – Hhut
            Sep 4 '15 at 8:19






          • 4




            This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
            – CMCDragonkai
            Apr 4 '16 at 14:17






          • 2




            Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
            – infoclogged
            Jun 29 '16 at 15:27














          • 41




            This answer was the only one that made sense to me. thanks.
            – cgTag
            Mar 1 '15 at 16:20






          • 9




            This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
            – dtmland
            Mar 25 '15 at 16:16








          • 8




            Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
            – Hhut
            Sep 4 '15 at 8:19






          • 4




            This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
            – CMCDragonkai
            Apr 4 '16 at 14:17






          • 2




            Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
            – infoclogged
            Jun 29 '16 at 15:27








          41




          41




          This answer was the only one that made sense to me. thanks.
          – cgTag
          Mar 1 '15 at 16:20




          This answer was the only one that made sense to me. thanks.
          – cgTag
          Mar 1 '15 at 16:20




          9




          9




          This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
          – dtmland
          Mar 25 '15 at 16:16






          This answer should have more up-votes. I've been reading many posts for the past day and this clears up all the confusion. Coming from the SVN world and using externals - one wants to believe that git submodule branch tracking does magically keep everything up to date from the branch - but this is not true! You have to explicitly update them! As you mention, you must commit changed submodule objects.
          – dtmland
          Mar 25 '15 at 16:16






          8




          8




          Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
          – Hhut
          Sep 4 '15 at 8:19




          Does this branch tracking also work with tags? Instead of a branch I specified a tag in my .gitmodules and after doing $ git submodule update --init --remote TestModule I got an error saying fatal: Needed a single revision and Unable to find current origin/TestTag revision in submodule path 'TestModule'. When doing it with a real branch it works. Is there anyway to specify a tag in .gitmodules without having to specify the exact commit?
          – Hhut
          Sep 4 '15 at 8:19




          4




          4




          This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
          – CMCDragonkai
          Apr 4 '16 at 14:17




          This doesn't seem to work. I updated the hash in .gitmodules and ran git submodule update and nothing happened?
          – CMCDragonkai
          Apr 4 '16 at 14:17




          2




          2




          Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
          – infoclogged
          Jun 29 '16 at 15:27




          Somehow this doesnt work for me. With a SHA Commit Id, I always get an error "Unable to find current revision ( I double checked the revision number of HEAD and its correct ) . However if I use master it works.
          – infoclogged
          Jun 29 '16 at 15:27











          257














          Note that if you have an existing submodule which isn't tracking a branch yet, then (if you have git 1.8.2+):





          • Make sure the parent repo knows that its submodule now tracks a branch:



            cd /path/to/your/parent/repo
            git config -f .gitmodules submodule.<path>.branch <branch>



          • Make sure your submodule is actually at the latest of that branch:



            cd path/to/your/submodule
            git checkout -b branch --track origin/branch
            # if the master branch already exist:
            git branch -u origin/master master



                  
          (with 'origin' being the name of the upstream remote repo the submodule has been cloned from.

                  
          A git remote -v inside that submodule will display it. Usually, it is 'origin')





          • Don't forget to record the new state of your submodule in your parent repo:



            cd /path/to/your/parent/repo
            git add path/to/your/submodule
            git commit -m "Make submodule tracking a branch"



          • Subsequent update for that submodule will have to use the --remote option:



            # update your submodule
            # --remote will also fetch and ensure that
            # the latest commit from the branch is used
            git submodule update --remote

            # to avoid fetching use
            git submodule update --remote --no-fetch





          Note that with Git 2.10+ (Q3 2016), you can use '.' as a branch name:




          The name of the branch is recorded as submodule.<name>.branch in .gitmodules for update --remote.
          A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.






          If you want to update all your submodules following a branch:



              git submodule update --recursive --remote


          Note that the result, for each updated submodule, will almost always be a detached HEAD, as Dan Cameron note in his answer.



          (Clintm notes in the comments that, if you run git submodule update --remote and the resulting sha1 is the same as the branch the submodule is currently on, it won't do anything and leave the submodule still "on that branch" and not in detached head state.)



          To ensure the branch is actually checked out (and that won't modify the SHA1 of the special entry representing the submodule for the parent repo), he suggests:



          git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'


          Each submodule will still reference the same SHA1, but if you do make new commits, you will be able to push them because they will be referenced by the branch you want the submodule to track.

          After that push within a submodule, don't forget to go back to the parent repo, add, commit and push the new SHA1 for those modified submodules.



          Note the use of $toplevel, recommended in the comments by Alexander Pogrebnyak.
          $toplevel was introduced in git1.7.2 in May 2010: commit f030c96.




          it contains the absolute path of the top level directory (where .gitmodules is).




          dtmland adds in the comments:




          The foreach script will fail to checkout submodules that are not following a branch.

          However, this command gives you both:




           git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch' –


          The same command but easier to read:



          git submodule foreach -q --recursive 
          'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)";
          [ "$branch" = "" ] &&
          git checkout master || git checkout $branch' –




          umläute refines dtmland's command with a simplified version in the comments:



          git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'


          multiple lines:



          git submodule foreach -q --recursive 
          'git checkout
          $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'





          share|improve this answer



















          • 1




            Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
            – BraveNewMath
            Sep 17 '13 at 20:41








          • 1




            The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
            – Alexander Pogrebnyak
            Nov 27 '13 at 18:19






          • 1




            The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
            – dtmland
            Jul 10 '15 at 19:09






          • 2




            here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
            – umläute
            Oct 15 '15 at 11:54






          • 1




            Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
            – GregTom
            Apr 17 '17 at 4:16


















          257














          Note that if you have an existing submodule which isn't tracking a branch yet, then (if you have git 1.8.2+):





          • Make sure the parent repo knows that its submodule now tracks a branch:



            cd /path/to/your/parent/repo
            git config -f .gitmodules submodule.<path>.branch <branch>



          • Make sure your submodule is actually at the latest of that branch:



            cd path/to/your/submodule
            git checkout -b branch --track origin/branch
            # if the master branch already exist:
            git branch -u origin/master master



                  
          (with 'origin' being the name of the upstream remote repo the submodule has been cloned from.

                  
          A git remote -v inside that submodule will display it. Usually, it is 'origin')





          • Don't forget to record the new state of your submodule in your parent repo:



            cd /path/to/your/parent/repo
            git add path/to/your/submodule
            git commit -m "Make submodule tracking a branch"



          • Subsequent update for that submodule will have to use the --remote option:



            # update your submodule
            # --remote will also fetch and ensure that
            # the latest commit from the branch is used
            git submodule update --remote

            # to avoid fetching use
            git submodule update --remote --no-fetch





          Note that with Git 2.10+ (Q3 2016), you can use '.' as a branch name:




          The name of the branch is recorded as submodule.<name>.branch in .gitmodules for update --remote.
          A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.






          If you want to update all your submodules following a branch:



              git submodule update --recursive --remote


          Note that the result, for each updated submodule, will almost always be a detached HEAD, as Dan Cameron note in his answer.



          (Clintm notes in the comments that, if you run git submodule update --remote and the resulting sha1 is the same as the branch the submodule is currently on, it won't do anything and leave the submodule still "on that branch" and not in detached head state.)



          To ensure the branch is actually checked out (and that won't modify the SHA1 of the special entry representing the submodule for the parent repo), he suggests:



          git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'


          Each submodule will still reference the same SHA1, but if you do make new commits, you will be able to push them because they will be referenced by the branch you want the submodule to track.

          After that push within a submodule, don't forget to go back to the parent repo, add, commit and push the new SHA1 for those modified submodules.



          Note the use of $toplevel, recommended in the comments by Alexander Pogrebnyak.
          $toplevel was introduced in git1.7.2 in May 2010: commit f030c96.




          it contains the absolute path of the top level directory (where .gitmodules is).




          dtmland adds in the comments:




          The foreach script will fail to checkout submodules that are not following a branch.

          However, this command gives you both:




           git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch' –


          The same command but easier to read:



          git submodule foreach -q --recursive 
          'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)";
          [ "$branch" = "" ] &&
          git checkout master || git checkout $branch' –




          umläute refines dtmland's command with a simplified version in the comments:



          git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'


          multiple lines:



          git submodule foreach -q --recursive 
          'git checkout
          $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'





          share|improve this answer



















          • 1




            Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
            – BraveNewMath
            Sep 17 '13 at 20:41








          • 1




            The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
            – Alexander Pogrebnyak
            Nov 27 '13 at 18:19






          • 1




            The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
            – dtmland
            Jul 10 '15 at 19:09






          • 2




            here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
            – umläute
            Oct 15 '15 at 11:54






          • 1




            Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
            – GregTom
            Apr 17 '17 at 4:16
















          257












          257








          257






          Note that if you have an existing submodule which isn't tracking a branch yet, then (if you have git 1.8.2+):





          • Make sure the parent repo knows that its submodule now tracks a branch:



            cd /path/to/your/parent/repo
            git config -f .gitmodules submodule.<path>.branch <branch>



          • Make sure your submodule is actually at the latest of that branch:



            cd path/to/your/submodule
            git checkout -b branch --track origin/branch
            # if the master branch already exist:
            git branch -u origin/master master



                  
          (with 'origin' being the name of the upstream remote repo the submodule has been cloned from.

                  
          A git remote -v inside that submodule will display it. Usually, it is 'origin')





          • Don't forget to record the new state of your submodule in your parent repo:



            cd /path/to/your/parent/repo
            git add path/to/your/submodule
            git commit -m "Make submodule tracking a branch"



          • Subsequent update for that submodule will have to use the --remote option:



            # update your submodule
            # --remote will also fetch and ensure that
            # the latest commit from the branch is used
            git submodule update --remote

            # to avoid fetching use
            git submodule update --remote --no-fetch





          Note that with Git 2.10+ (Q3 2016), you can use '.' as a branch name:




          The name of the branch is recorded as submodule.<name>.branch in .gitmodules for update --remote.
          A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.






          If you want to update all your submodules following a branch:



              git submodule update --recursive --remote


          Note that the result, for each updated submodule, will almost always be a detached HEAD, as Dan Cameron note in his answer.



          (Clintm notes in the comments that, if you run git submodule update --remote and the resulting sha1 is the same as the branch the submodule is currently on, it won't do anything and leave the submodule still "on that branch" and not in detached head state.)



          To ensure the branch is actually checked out (and that won't modify the SHA1 of the special entry representing the submodule for the parent repo), he suggests:



          git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'


          Each submodule will still reference the same SHA1, but if you do make new commits, you will be able to push them because they will be referenced by the branch you want the submodule to track.

          After that push within a submodule, don't forget to go back to the parent repo, add, commit and push the new SHA1 for those modified submodules.



          Note the use of $toplevel, recommended in the comments by Alexander Pogrebnyak.
          $toplevel was introduced in git1.7.2 in May 2010: commit f030c96.




          it contains the absolute path of the top level directory (where .gitmodules is).




          dtmland adds in the comments:




          The foreach script will fail to checkout submodules that are not following a branch.

          However, this command gives you both:




           git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch' –


          The same command but easier to read:



          git submodule foreach -q --recursive 
          'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)";
          [ "$branch" = "" ] &&
          git checkout master || git checkout $branch' –




          umläute refines dtmland's command with a simplified version in the comments:



          git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'


          multiple lines:



          git submodule foreach -q --recursive 
          'git checkout
          $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'





          share|improve this answer














          Note that if you have an existing submodule which isn't tracking a branch yet, then (if you have git 1.8.2+):





          • Make sure the parent repo knows that its submodule now tracks a branch:



            cd /path/to/your/parent/repo
            git config -f .gitmodules submodule.<path>.branch <branch>



          • Make sure your submodule is actually at the latest of that branch:



            cd path/to/your/submodule
            git checkout -b branch --track origin/branch
            # if the master branch already exist:
            git branch -u origin/master master



                  
          (with 'origin' being the name of the upstream remote repo the submodule has been cloned from.

                  
          A git remote -v inside that submodule will display it. Usually, it is 'origin')





          • Don't forget to record the new state of your submodule in your parent repo:



            cd /path/to/your/parent/repo
            git add path/to/your/submodule
            git commit -m "Make submodule tracking a branch"



          • Subsequent update for that submodule will have to use the --remote option:



            # update your submodule
            # --remote will also fetch and ensure that
            # the latest commit from the branch is used
            git submodule update --remote

            # to avoid fetching use
            git submodule update --remote --no-fetch





          Note that with Git 2.10+ (Q3 2016), you can use '.' as a branch name:




          The name of the branch is recorded as submodule.<name>.branch in .gitmodules for update --remote.
          A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.






          If you want to update all your submodules following a branch:



              git submodule update --recursive --remote


          Note that the result, for each updated submodule, will almost always be a detached HEAD, as Dan Cameron note in his answer.



          (Clintm notes in the comments that, if you run git submodule update --remote and the resulting sha1 is the same as the branch the submodule is currently on, it won't do anything and leave the submodule still "on that branch" and not in detached head state.)



          To ensure the branch is actually checked out (and that won't modify the SHA1 of the special entry representing the submodule for the parent repo), he suggests:



          git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'


          Each submodule will still reference the same SHA1, but if you do make new commits, you will be able to push them because they will be referenced by the branch you want the submodule to track.

          After that push within a submodule, don't forget to go back to the parent repo, add, commit and push the new SHA1 for those modified submodules.



          Note the use of $toplevel, recommended in the comments by Alexander Pogrebnyak.
          $toplevel was introduced in git1.7.2 in May 2010: commit f030c96.




          it contains the absolute path of the top level directory (where .gitmodules is).




          dtmland adds in the comments:




          The foreach script will fail to checkout submodules that are not following a branch.

          However, this command gives you both:




           git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch' –


          The same command but easier to read:



          git submodule foreach -q --recursive 
          'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)";
          [ "$branch" = "" ] &&
          git checkout master || git checkout $branch' –




          umläute refines dtmland's command with a simplified version in the comments:



          git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'


          multiple lines:



          git submodule foreach -q --recursive 
          'git checkout
          $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 25 '18 at 19:51

























          answered Sep 14 '13 at 6:59









          VonC

          831k28926153160




          831k28926153160








          • 1




            Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
            – BraveNewMath
            Sep 17 '13 at 20:41








          • 1




            The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
            – Alexander Pogrebnyak
            Nov 27 '13 at 18:19






          • 1




            The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
            – dtmland
            Jul 10 '15 at 19:09






          • 2




            here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
            – umläute
            Oct 15 '15 at 11:54






          • 1




            Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
            – GregTom
            Apr 17 '17 at 4:16
















          • 1




            Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
            – BraveNewMath
            Sep 17 '13 at 20:41








          • 1




            The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
            – Alexander Pogrebnyak
            Nov 27 '13 at 18:19






          • 1




            The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
            – dtmland
            Jul 10 '15 at 19:09






          • 2




            here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
            – umläute
            Oct 15 '15 at 11:54






          • 1




            Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
            – GregTom
            Apr 17 '17 at 4:16










          1




          1




          Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
          – BraveNewMath
          Sep 17 '13 at 20:41






          Question: if i have folder subModule1 and wish to track master branch, would the resulting command look like this: git config -f .gitmodules submodule.subModule1.branch master
          – BraveNewMath
          Sep 17 '13 at 20:41






          1




          1




          The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
          – Alexander Pogrebnyak
          Nov 27 '13 at 18:19




          The foreach script will not depend on the hardcoded <path>, if you substitute <path> with $toplevel/.
          – Alexander Pogrebnyak
          Nov 27 '13 at 18:19




          1




          1




          The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
          – dtmland
          Jul 10 '15 at 19:09




          The foreach script will fail to checkout submodules that are not following a branch. However, this command gives you both: git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout master || git checkout $branch'
          – dtmland
          Jul 10 '15 at 19:09




          2




          2




          here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
          – umläute
          Oct 15 '15 at 11:54




          here's a simplified version of @dtmland's script: git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
          – umläute
          Oct 15 '15 at 11:54




          1




          1




          Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
          – GregTom
          Apr 17 '17 at 4:16






          Ohh! Actually the foreach script is unnecessary. We have to execute the submodule update with the --merge or --rebase switch: git submodule update --remote --merge or git submodule update --remote --rebase. These commands do the tracking of the remote branch.
          – GregTom
          Apr 17 '17 at 4:16













          160














          Git 1.8.2 added the possibility to track branches.



          # add submodule to track master branch
          git submodule add -b master [URL to Git repo];

          # update your submodule
          git submodule update --remote


          See also Git submodules






          share|improve this answer

















          • 2




            Does this apply to tags as well?
            – ThorSummoner
            Jul 2 '14 at 21:28






          • 1




            How does adding submodule in such way reflect on .gitmodules file?
            – Eugene
            Jul 20 '14 at 12:22








          • 1




            Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
            – Dinis Cruz
            Dec 24 '14 at 2:03






          • 2




            You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
            – KCD
            Oct 16 '15 at 2:27








          • 2




            @KCD Which version of git can do that with tags. Mine doesn't work?
            – CMCDragonkai
            Apr 4 '16 at 13:56
















          160














          Git 1.8.2 added the possibility to track branches.



          # add submodule to track master branch
          git submodule add -b master [URL to Git repo];

          # update your submodule
          git submodule update --remote


          See also Git submodules






          share|improve this answer

















          • 2




            Does this apply to tags as well?
            – ThorSummoner
            Jul 2 '14 at 21:28






          • 1




            How does adding submodule in such way reflect on .gitmodules file?
            – Eugene
            Jul 20 '14 at 12:22








          • 1




            Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
            – Dinis Cruz
            Dec 24 '14 at 2:03






          • 2




            You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
            – KCD
            Oct 16 '15 at 2:27








          • 2




            @KCD Which version of git can do that with tags. Mine doesn't work?
            – CMCDragonkai
            Apr 4 '16 at 13:56














          160












          160








          160






          Git 1.8.2 added the possibility to track branches.



          # add submodule to track master branch
          git submodule add -b master [URL to Git repo];

          # update your submodule
          git submodule update --remote


          See also Git submodules






          share|improve this answer












          Git 1.8.2 added the possibility to track branches.



          # add submodule to track master branch
          git submodule add -b master [URL to Git repo];

          # update your submodule
          git submodule update --remote


          See also Git submodules







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 3 '13 at 8:44









          vogella

          13.7k42321




          13.7k42321








          • 2




            Does this apply to tags as well?
            – ThorSummoner
            Jul 2 '14 at 21:28






          • 1




            How does adding submodule in such way reflect on .gitmodules file?
            – Eugene
            Jul 20 '14 at 12:22








          • 1




            Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
            – Dinis Cruz
            Dec 24 '14 at 2:03






          • 2




            You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
            – KCD
            Oct 16 '15 at 2:27








          • 2




            @KCD Which version of git can do that with tags. Mine doesn't work?
            – CMCDragonkai
            Apr 4 '16 at 13:56














          • 2




            Does this apply to tags as well?
            – ThorSummoner
            Jul 2 '14 at 21:28






          • 1




            How does adding submodule in such way reflect on .gitmodules file?
            – Eugene
            Jul 20 '14 at 12:22








          • 1




            Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
            – Dinis Cruz
            Dec 24 '14 at 2:03






          • 2




            You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
            – KCD
            Oct 16 '15 at 2:27








          • 2




            @KCD Which version of git can do that with tags. Mine doesn't work?
            – CMCDragonkai
            Apr 4 '16 at 13:56








          2




          2




          Does this apply to tags as well?
          – ThorSummoner
          Jul 2 '14 at 21:28




          Does this apply to tags as well?
          – ThorSummoner
          Jul 2 '14 at 21:28




          1




          1




          How does adding submodule in such way reflect on .gitmodules file?
          – Eugene
          Jul 20 '14 at 12:22






          How does adding submodule in such way reflect on .gitmodules file?
          – Eugene
          Jul 20 '14 at 12:22






          1




          1




          Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
          – Dinis Cruz
          Dec 24 '14 at 2:03




          Thanks I just used the info about to help me to create a submodule folder that is synced with a GitHub gh-pages website: full example at github.com/o2platform/fluentnode/issues/22
          – Dinis Cruz
          Dec 24 '14 at 2:03




          2




          2




          You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
          – KCD
          Oct 16 '15 at 2:27






          You can lock to a tag with git submodule add -b tags/<sometag> <url> which you can see as the line branch = tags/<sometag> in .gitmodules
          – KCD
          Oct 16 '15 at 2:27






          2




          2




          @KCD Which version of git can do that with tags. Mine doesn't work?
          – CMCDragonkai
          Apr 4 '16 at 13:56




          @KCD Which version of git can do that with tags. Mine doesn't work?
          – CMCDragonkai
          Apr 4 '16 at 13:56











          49














          An example of how I use Git submodules.




          1. Create a new repository

          2. Then clone another repository as a submodule

          3. Then we have that submodule use a tag called V3.1.2

          4. And then we commit.


          And that looks a little bit like this:



          git init 
          vi README
          git add README
          git commit
          git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
          git status

          git submodule init
          git submodule update

          cd stm32_std_lib/
          git reset --hard V3.1.2
          cd ..
          git commit -a

          git submodule status


          Maybe it helps (even though I use a tag and not a branch)?






          share|improve this answer



















          • 4




            It's basically the same answer as djacobs7, but thanks anyway :)
            – Ivan
            Nov 22 '09 at 18:22






          • 1




            Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
            – Nick Radford
            Oct 10 '12 at 20:07






          • 1




            @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
            – Michel Müller
            Aug 12 '13 at 8:14










          • djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
            – CodeMonkey
            Aug 22 '17 at 6:04
















          49














          An example of how I use Git submodules.




          1. Create a new repository

          2. Then clone another repository as a submodule

          3. Then we have that submodule use a tag called V3.1.2

          4. And then we commit.


          And that looks a little bit like this:



          git init 
          vi README
          git add README
          git commit
          git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
          git status

          git submodule init
          git submodule update

          cd stm32_std_lib/
          git reset --hard V3.1.2
          cd ..
          git commit -a

          git submodule status


          Maybe it helps (even though I use a tag and not a branch)?






          share|improve this answer



















          • 4




            It's basically the same answer as djacobs7, but thanks anyway :)
            – Ivan
            Nov 22 '09 at 18:22






          • 1




            Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
            – Nick Radford
            Oct 10 '12 at 20:07






          • 1




            @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
            – Michel Müller
            Aug 12 '13 at 8:14










          • djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
            – CodeMonkey
            Aug 22 '17 at 6:04














          49












          49








          49






          An example of how I use Git submodules.




          1. Create a new repository

          2. Then clone another repository as a submodule

          3. Then we have that submodule use a tag called V3.1.2

          4. And then we commit.


          And that looks a little bit like this:



          git init 
          vi README
          git add README
          git commit
          git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
          git status

          git submodule init
          git submodule update

          cd stm32_std_lib/
          git reset --hard V3.1.2
          cd ..
          git commit -a

          git submodule status


          Maybe it helps (even though I use a tag and not a branch)?






          share|improve this answer














          An example of how I use Git submodules.




          1. Create a new repository

          2. Then clone another repository as a submodule

          3. Then we have that submodule use a tag called V3.1.2

          4. And then we commit.


          And that looks a little bit like this:



          git init 
          vi README
          git add README
          git commit
          git submodule add git://github.com/XXXXX/xxx.yyyy.git stm32_std_lib
          git status

          git submodule init
          git submodule update

          cd stm32_std_lib/
          git reset --hard V3.1.2
          cd ..
          git commit -a

          git submodule status


          Maybe it helps (even though I use a tag and not a branch)?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 30 '18 at 10:43









          Peter Mortensen

          13.5k1983111




          13.5k1983111










          answered Nov 22 '09 at 9:53









          Johan

          10.1k2479105




          10.1k2479105








          • 4




            It's basically the same answer as djacobs7, but thanks anyway :)
            – Ivan
            Nov 22 '09 at 18:22






          • 1




            Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
            – Nick Radford
            Oct 10 '12 at 20:07






          • 1




            @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
            – Michel Müller
            Aug 12 '13 at 8:14










          • djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
            – CodeMonkey
            Aug 22 '17 at 6:04














          • 4




            It's basically the same answer as djacobs7, but thanks anyway :)
            – Ivan
            Nov 22 '09 at 18:22






          • 1




            Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
            – Nick Radford
            Oct 10 '12 at 20:07






          • 1




            @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
            – Michel Müller
            Aug 12 '13 at 8:14










          • djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
            – CodeMonkey
            Aug 22 '17 at 6:04








          4




          4




          It's basically the same answer as djacobs7, but thanks anyway :)
          – Ivan
          Nov 22 '09 at 18:22




          It's basically the same answer as djacobs7, but thanks anyway :)
          – Ivan
          Nov 22 '09 at 18:22




          1




          1




          Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
          – Nick Radford
          Oct 10 '12 at 20:07




          Should you be able to commit a change after your git reset --hard V3.1.2? I just get a "nothing to commit" with a git status of the parent directory.
          – Nick Radford
          Oct 10 '12 at 20:07




          1




          1




          @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
          – Michel Müller
          Aug 12 '13 at 8:14




          @Ivan: Could you explain how this is the same as djacobs7's response? As far as I see, his response doesn't even include the 'submodule add' command, instead the repo is added directly, without any link to the module's original git repo. At least when I tried this approach there was no link in .gitmodules.
          – Michel Müller
          Aug 12 '13 at 8:14












          djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
          – CodeMonkey
          Aug 22 '17 at 6:04




          djacobs7's response doesn't include the whole explanation starting from adding the submodule. He assumes you already have it.
          – CodeMonkey
          Aug 22 '17 at 6:04











          34














          In my experience switching branches in the superproject or future checkouts will still cause detached HEADs of submodules regardless if the submodule is properly added and tracked (i.e. @djacobs7 and @Johnny Z answers).



          And instead of manually checking out the correct branch manually or through a script git submodule foreach can be used.



          This will check the submodule config file for the branch property and checkout the set branch.



          git submodule foreach -q --recursive 'branch="$(git config -f <path>.gitmodules submodule.$name.branch)"; git checkout $branch'






          share|improve this answer





















          • Nice. +1. I have included your command in my answer.
            – VonC
            Nov 9 '13 at 8:17
















          34














          In my experience switching branches in the superproject or future checkouts will still cause detached HEADs of submodules regardless if the submodule is properly added and tracked (i.e. @djacobs7 and @Johnny Z answers).



          And instead of manually checking out the correct branch manually or through a script git submodule foreach can be used.



          This will check the submodule config file for the branch property and checkout the set branch.



          git submodule foreach -q --recursive 'branch="$(git config -f <path>.gitmodules submodule.$name.branch)"; git checkout $branch'






          share|improve this answer





















          • Nice. +1. I have included your command in my answer.
            – VonC
            Nov 9 '13 at 8:17














          34












          34








          34






          In my experience switching branches in the superproject or future checkouts will still cause detached HEADs of submodules regardless if the submodule is properly added and tracked (i.e. @djacobs7 and @Johnny Z answers).



          And instead of manually checking out the correct branch manually or through a script git submodule foreach can be used.



          This will check the submodule config file for the branch property and checkout the set branch.



          git submodule foreach -q --recursive 'branch="$(git config -f <path>.gitmodules submodule.$name.branch)"; git checkout $branch'






          share|improve this answer












          In my experience switching branches in the superproject or future checkouts will still cause detached HEADs of submodules regardless if the submodule is properly added and tracked (i.e. @djacobs7 and @Johnny Z answers).



          And instead of manually checking out the correct branch manually or through a script git submodule foreach can be used.



          This will check the submodule config file for the branch property and checkout the set branch.



          git submodule foreach -q --recursive 'branch="$(git config -f <path>.gitmodules submodule.$name.branch)"; git checkout $branch'







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 '13 at 4:54









          Dan Cameron

          64668




          64668












          • Nice. +1. I have included your command in my answer.
            – VonC
            Nov 9 '13 at 8:17


















          • Nice. +1. I have included your command in my answer.
            – VonC
            Nov 9 '13 at 8:17
















          Nice. +1. I have included your command in my answer.
          – VonC
          Nov 9 '13 at 8:17




          Nice. +1. I have included your command in my answer.
          – VonC
          Nov 9 '13 at 8:17











          28














          Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.



          This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.



          Then imagine that someone commits a change to repository bar before you can make your clone.



          When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.



          Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.






          share|improve this answer























          • I think it's time I start reading some git books, thanks for the recommendation.
            – Ivan
            Nov 22 '09 at 18:21










          • Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
            – momo
            Dec 1 '11 at 12:44






          • 5




            The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
            – jerclarke
            Oct 25 '12 at 18:23










          • @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
            – Alastair
            Nov 7 '12 at 4:01
















          28














          Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.



          This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.



          Then imagine that someone commits a change to repository bar before you can make your clone.



          When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.



          Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.






          share|improve this answer























          • I think it's time I start reading some git books, thanks for the recommendation.
            – Ivan
            Nov 22 '09 at 18:21










          • Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
            – momo
            Dec 1 '11 at 12:44






          • 5




            The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
            – jerclarke
            Oct 25 '12 at 18:23










          • @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
            – Alastair
            Nov 7 '12 at 4:01














          28












          28








          28






          Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.



          This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.



          Then imagine that someone commits a change to repository bar before you can make your clone.



          When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.



          Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.






          share|improve this answer














          Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.



          This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.



          Then imagine that someone commits a change to repository bar before you can make your clone.



          When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.



          Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 19 '18 at 20:06









          Peter Mortensen

          13.5k1983111




          13.5k1983111










          answered Nov 22 '09 at 5:22









          Neall

          19.9k33944




          19.9k33944












          • I think it's time I start reading some git books, thanks for the recommendation.
            – Ivan
            Nov 22 '09 at 18:21










          • Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
            – momo
            Dec 1 '11 at 12:44






          • 5




            The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
            – jerclarke
            Oct 25 '12 at 18:23










          • @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
            – Alastair
            Nov 7 '12 at 4:01


















          • I think it's time I start reading some git books, thanks for the recommendation.
            – Ivan
            Nov 22 '09 at 18:21










          • Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
            – momo
            Dec 1 '11 at 12:44






          • 5




            The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
            – jerclarke
            Oct 25 '12 at 18:23










          • @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
            – Alastair
            Nov 7 '12 at 4:01
















          I think it's time I start reading some git books, thanks for the recommendation.
          – Ivan
          Nov 22 '09 at 18:21




          I think it's time I start reading some git books, thanks for the recommendation.
          – Ivan
          Nov 22 '09 at 18:21












          Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
          – momo
          Dec 1 '11 at 12:44




          Sorry, but you didn't clarify if one would get the same as you pushed to a7402be , or get the latest of bar, though your version of foo. Thanks :)
          – momo
          Dec 1 '11 at 12:44




          5




          5




          The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
          – jerclarke
          Oct 25 '12 at 18:23




          The issue is that there should be an option to say "keep this submodule on branch X" so that if you WANT it to automatically update itself then you can make that happen. It would make submodules much more useful for managing e.g. a WordPress installation where plugins are all Git repos without having to re-save the superproject for every plugin that updates.
          – jerclarke
          Oct 25 '12 at 18:23












          @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
          – Alastair
          Nov 7 '12 at 4:01




          @jeremyclark git clone git://github.com/git/git.git and push that feature...? =D
          – Alastair
          Nov 7 '12 at 4:01











          12














          To switch branch for a submodule (assuming you already have the submodule as part of the repository):





          • cd to root of your repository containing the submodules

          • Open .gitmodules for editing

          • Add line below path = ... and url = ... that says branch = your-branch, for each submodule; save file .gitmodules.

          • then without changing directory do $ git submodule update --remote


          ...this should pull in the latest commits on the specified branch, for each submodule thus modified.






          share|improve this answer




























            12














            To switch branch for a submodule (assuming you already have the submodule as part of the repository):





            • cd to root of your repository containing the submodules

            • Open .gitmodules for editing

            • Add line below path = ... and url = ... that says branch = your-branch, for each submodule; save file .gitmodules.

            • then without changing directory do $ git submodule update --remote


            ...this should pull in the latest commits on the specified branch, for each submodule thus modified.






            share|improve this answer


























              12












              12








              12






              To switch branch for a submodule (assuming you already have the submodule as part of the repository):





              • cd to root of your repository containing the submodules

              • Open .gitmodules for editing

              • Add line below path = ... and url = ... that says branch = your-branch, for each submodule; save file .gitmodules.

              • then without changing directory do $ git submodule update --remote


              ...this should pull in the latest commits on the specified branch, for each submodule thus modified.






              share|improve this answer














              To switch branch for a submodule (assuming you already have the submodule as part of the repository):





              • cd to root of your repository containing the submodules

              • Open .gitmodules for editing

              • Add line below path = ... and url = ... that says branch = your-branch, for each submodule; save file .gitmodules.

              • then without changing directory do $ git submodule update --remote


              ...this should pull in the latest commits on the specified branch, for each submodule thus modified.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 19 '18 at 20:14









              Peter Mortensen

              13.5k1983111




              13.5k1983111










              answered Jun 12 '16 at 13:34









              Arcane Engineer

              4,69254684




              4,69254684























                  7














                  I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.



                  [alias]

                  ######################
                  #
                  #Submodules aliases
                  #
                  ######################


                  #git sm-trackbranch : places all submodules on their respective branch specified in .gitmodules
                  #This works if submodules are configured to track a branch, i.e if .gitmodules looks like :
                  #[submodule "my-submodule"]
                  # path = my-submodule
                  # url = git@wherever.you.like/my-submodule.git
                  # branch = my-branch
                  sm-trackbranch = "! git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'"

                  #sm-pullrebase :
                  # - pull --rebase on the master repo
                  # - sm-trackbranch on every submodule
                  # - pull --rebase on each submodule
                  #
                  # Important note :
                  #- have a clean master repo and subrepos before doing this !
                  #- this is *not* equivalent to getting the last committed
                  # master repo + its submodules: if some submodules are tracking branches
                  # that have evolved since the last commit in the master repo,
                  # they will be using those more recent commits !
                  #
                  # (Note : On the contrary, git submodule update will stick
                  #to the last committed SHA1 in the master repo)
                  #
                  sm-pullrebase = "! git pull --rebase; git submodule update; git sm-trackbranch ; git submodule foreach 'git pull --rebase' "

                  # git sm-diff will diff the master repo *and* its submodules
                  sm-diff = "! git diff && git submodule foreach 'git diff' "

                  #git sm-push will ask to push also submodules
                  sm-push = push --recurse-submodules=on-demand

                  #git alias : list all aliases
                  #useful in order to learn git syntax
                  alias = "!git config -l | grep alias | cut -c 7-"





                  share|improve this answer




























                    7














                    I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.



                    [alias]

                    ######################
                    #
                    #Submodules aliases
                    #
                    ######################


                    #git sm-trackbranch : places all submodules on their respective branch specified in .gitmodules
                    #This works if submodules are configured to track a branch, i.e if .gitmodules looks like :
                    #[submodule "my-submodule"]
                    # path = my-submodule
                    # url = git@wherever.you.like/my-submodule.git
                    # branch = my-branch
                    sm-trackbranch = "! git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'"

                    #sm-pullrebase :
                    # - pull --rebase on the master repo
                    # - sm-trackbranch on every submodule
                    # - pull --rebase on each submodule
                    #
                    # Important note :
                    #- have a clean master repo and subrepos before doing this !
                    #- this is *not* equivalent to getting the last committed
                    # master repo + its submodules: if some submodules are tracking branches
                    # that have evolved since the last commit in the master repo,
                    # they will be using those more recent commits !
                    #
                    # (Note : On the contrary, git submodule update will stick
                    #to the last committed SHA1 in the master repo)
                    #
                    sm-pullrebase = "! git pull --rebase; git submodule update; git sm-trackbranch ; git submodule foreach 'git pull --rebase' "

                    # git sm-diff will diff the master repo *and* its submodules
                    sm-diff = "! git diff && git submodule foreach 'git diff' "

                    #git sm-push will ask to push also submodules
                    sm-push = push --recurse-submodules=on-demand

                    #git alias : list all aliases
                    #useful in order to learn git syntax
                    alias = "!git config -l | grep alias | cut -c 7-"





                    share|improve this answer


























                      7












                      7








                      7






                      I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.



                      [alias]

                      ######################
                      #
                      #Submodules aliases
                      #
                      ######################


                      #git sm-trackbranch : places all submodules on their respective branch specified in .gitmodules
                      #This works if submodules are configured to track a branch, i.e if .gitmodules looks like :
                      #[submodule "my-submodule"]
                      # path = my-submodule
                      # url = git@wherever.you.like/my-submodule.git
                      # branch = my-branch
                      sm-trackbranch = "! git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'"

                      #sm-pullrebase :
                      # - pull --rebase on the master repo
                      # - sm-trackbranch on every submodule
                      # - pull --rebase on each submodule
                      #
                      # Important note :
                      #- have a clean master repo and subrepos before doing this !
                      #- this is *not* equivalent to getting the last committed
                      # master repo + its submodules: if some submodules are tracking branches
                      # that have evolved since the last commit in the master repo,
                      # they will be using those more recent commits !
                      #
                      # (Note : On the contrary, git submodule update will stick
                      #to the last committed SHA1 in the master repo)
                      #
                      sm-pullrebase = "! git pull --rebase; git submodule update; git sm-trackbranch ; git submodule foreach 'git pull --rebase' "

                      # git sm-diff will diff the master repo *and* its submodules
                      sm-diff = "! git diff && git submodule foreach 'git diff' "

                      #git sm-push will ask to push also submodules
                      sm-push = push --recurse-submodules=on-demand

                      #git alias : list all aliases
                      #useful in order to learn git syntax
                      alias = "!git config -l | grep alias | cut -c 7-"





                      share|improve this answer














                      I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.



                      [alias]

                      ######################
                      #
                      #Submodules aliases
                      #
                      ######################


                      #git sm-trackbranch : places all submodules on their respective branch specified in .gitmodules
                      #This works if submodules are configured to track a branch, i.e if .gitmodules looks like :
                      #[submodule "my-submodule"]
                      # path = my-submodule
                      # url = git@wherever.you.like/my-submodule.git
                      # branch = my-branch
                      sm-trackbranch = "! git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'"

                      #sm-pullrebase :
                      # - pull --rebase on the master repo
                      # - sm-trackbranch on every submodule
                      # - pull --rebase on each submodule
                      #
                      # Important note :
                      #- have a clean master repo and subrepos before doing this !
                      #- this is *not* equivalent to getting the last committed
                      # master repo + its submodules: if some submodules are tracking branches
                      # that have evolved since the last commit in the master repo,
                      # they will be using those more recent commits !
                      #
                      # (Note : On the contrary, git submodule update will stick
                      #to the last committed SHA1 in the master repo)
                      #
                      sm-pullrebase = "! git pull --rebase; git submodule update; git sm-trackbranch ; git submodule foreach 'git pull --rebase' "

                      # git sm-diff will diff the master repo *and* its submodules
                      sm-diff = "! git diff && git submodule foreach 'git diff' "

                      #git sm-push will ask to push also submodules
                      sm-push = push --recurse-submodules=on-demand

                      #git alias : list all aliases
                      #useful in order to learn git syntax
                      alias = "!git config -l | grep alias | cut -c 7-"






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 1 '15 at 16:41

























                      answered Apr 1 '15 at 16:27









                      Pascal T.

                      1,95832429




                      1,95832429























                          3














                          We use Quack to pull a specific module from another Git repository. We need to pull code without the whole code base of the provided repository - we need a very specific module / file from that huge repository and should be updated every time we run update.



                          So we achieved it in this way:



                          Create configuration



                          name: Project Name

                          modules:
                          local/path:
                          repository: https://github.com/<username>/<repo>.git
                          path: repo/path
                          branch: dev
                          other/local/path/filename.txt:
                          repository: https://github.com/<username>/<repo>.git
                          hexsha: 9e3e9642cfea36f4ae216d27df100134920143b9
                          path: repo/path/filename.txt

                          profiles:
                          init:
                          tasks: ['modules']


                          With the above configuration, it creates one directory from the provided GitHub repository as specified in first module configuration, and the other one is to pull and create a file from the given repository.



                          Other developers just need to run



                          $ quack


                          And it pulls the code from the above configurations.






                          share|improve this answer




























                            3














                            We use Quack to pull a specific module from another Git repository. We need to pull code without the whole code base of the provided repository - we need a very specific module / file from that huge repository and should be updated every time we run update.



                            So we achieved it in this way:



                            Create configuration



                            name: Project Name

                            modules:
                            local/path:
                            repository: https://github.com/<username>/<repo>.git
                            path: repo/path
                            branch: dev
                            other/local/path/filename.txt:
                            repository: https://github.com/<username>/<repo>.git
                            hexsha: 9e3e9642cfea36f4ae216d27df100134920143b9
                            path: repo/path/filename.txt

                            profiles:
                            init:
                            tasks: ['modules']


                            With the above configuration, it creates one directory from the provided GitHub repository as specified in first module configuration, and the other one is to pull and create a file from the given repository.



                            Other developers just need to run



                            $ quack


                            And it pulls the code from the above configurations.






                            share|improve this answer


























                              3












                              3








                              3






                              We use Quack to pull a specific module from another Git repository. We need to pull code without the whole code base of the provided repository - we need a very specific module / file from that huge repository and should be updated every time we run update.



                              So we achieved it in this way:



                              Create configuration



                              name: Project Name

                              modules:
                              local/path:
                              repository: https://github.com/<username>/<repo>.git
                              path: repo/path
                              branch: dev
                              other/local/path/filename.txt:
                              repository: https://github.com/<username>/<repo>.git
                              hexsha: 9e3e9642cfea36f4ae216d27df100134920143b9
                              path: repo/path/filename.txt

                              profiles:
                              init:
                              tasks: ['modules']


                              With the above configuration, it creates one directory from the provided GitHub repository as specified in first module configuration, and the other one is to pull and create a file from the given repository.



                              Other developers just need to run



                              $ quack


                              And it pulls the code from the above configurations.






                              share|improve this answer














                              We use Quack to pull a specific module from another Git repository. We need to pull code without the whole code base of the provided repository - we need a very specific module / file from that huge repository and should be updated every time we run update.



                              So we achieved it in this way:



                              Create configuration



                              name: Project Name

                              modules:
                              local/path:
                              repository: https://github.com/<username>/<repo>.git
                              path: repo/path
                              branch: dev
                              other/local/path/filename.txt:
                              repository: https://github.com/<username>/<repo>.git
                              hexsha: 9e3e9642cfea36f4ae216d27df100134920143b9
                              path: repo/path/filename.txt

                              profiles:
                              init:
                              tasks: ['modules']


                              With the above configuration, it creates one directory from the provided GitHub repository as specified in first module configuration, and the other one is to pull and create a file from the given repository.



                              Other developers just need to run



                              $ quack


                              And it pulls the code from the above configurations.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 19 '18 at 20:14









                              Peter Mortensen

                              13.5k1983111




                              13.5k1983111










                              answered Oct 22 '15 at 14:35









                              Love Sharma

                              1,55211331




                              1,55211331























                                  0














                                  The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.



                                  You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
                                  The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote.
                                  If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.



                                  With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with




                                  fatal: Needed a single revision



                                  Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'







                                  share|improve this answer




























                                    0














                                    The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.



                                    You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
                                    The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote.
                                    If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.



                                    With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with




                                    fatal: Needed a single revision



                                    Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'







                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.



                                      You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
                                      The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote.
                                      If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.



                                      With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with




                                      fatal: Needed a single revision



                                      Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'







                                      share|improve this answer














                                      The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.



                                      You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
                                      The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote.
                                      If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.



                                      With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with




                                      fatal: Needed a single revision



                                      Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'








                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 14 '18 at 12:01

























                                      answered May 29 '18 at 15:52









                                      LuKePicci

                                      112




                                      112






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1777854%2fhow-can-i-specify-a-branch-tag-when-adding-a-git-submodule%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Full-time equivalent

                                          Bicuculline

                                          さくらももこ