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

前端 未结 12 1246
旧巷少年郎
旧巷少年郎 2020-11-21 06:39

How does git submodule add -b work?

After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init

相关标签:
12条回答
  • 2020-11-21 07:00

    Git 1.8.2 added the possibility to track branches.

    # add submodule to track master branch
    git submodule add -b branch_name URL_to_Git_repo optional_directory_rename
    
    # update your submodule
    git submodule update --remote 
    

    See also Git submodules

    0 讨论(0)
  • 2020-11-21 07:03

    git submodule add -b develop --name branch-name -- https://branch.git

    0 讨论(0)
  • 2020-11-21 07:04

    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
      

      Note: only branch name is supported in a .gitmodules file, but SHA and TAG are not supported! (instead of that, the branch's commit of each module can be tracked and updated using "git add .", for example like git add ./SubmoduleTestRepo, and you do not need to change the .gitmodules file each time)

      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.

    0 讨论(0)
  • 2020-11-21 07:04

    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'

    0 讨论(0)
  • 2020-11-21 07:06

    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)?

    0 讨论(0)
  • 2020-11-21 07:07

    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.

    0 讨论(0)
提交回复
热议问题