zoukankan      html  css  js  c++  java
  • git submodule 使用小结

    git submodule 使用小结

    原文链接 http://blog.gezhiqiang.com/2017/03/08/git-submodule/######

    Git Submodule 允许一个git仓库,作为另一个git仓库的子目录,并且保持父项目和子项目相互独立。

    添加子仓库

    $ git submodule add <仓库地址> <本地路径>
    

    新建一个父仓库main,一个子仓库sub。将父仓库克隆到本地。

    $ git clone ssh://git@10.2.237.56:23/dennis/main.git
    

    进入父仓库,并添加子仓库。

    $ git submodule add ssh://git@10.2.237.56:23/dennis/sub.git lib
    

    添加成功后,在父仓库根目录增加了.gitmodule文件。

    [submodule "sub"]
        path = lib
        url = ssh://git@10.2.237.56:23/dennis/sub.git
    

    并且在父仓库的git 配置文件中加入了submodule段。

    $ cat .git/config
    
    // 加了submodule段
    [submodule "sub"]
        url = ssh://git@10.2.237.56:23/dennis/sub.git
    

    注意:添加子仓库之后,主仓库的对应目录下(这里为lib),并不是sub仓库的文件,而是对应的commit id。如图所示:

     
    添加子仓库

    检出(checkout)

    克隆一个包含子仓库的仓库目录,并不会clone下子仓库的文件,只是会克隆下.gitmodule描述文件,需要进一步克隆子仓库文件。

    // 初始化本地配置文件
    $ git submodule init
    
    // 检出父仓库列出的commit
    $ git submodule update
    

    或者使用组合指令。

    $ git submodule update --init --recursive
    

    此时子目录在一个未命名分支,此时子仓库有改动并没有检测到。

    $ git branch
    * (HEAD detached at 46a27af)
      master
    

    在子仓库,切换到master分支,并git pull最新代码之后,回到主仓库目录,会显示子仓库修改,需要在主仓库提交修改,即修改指定的commit id。

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   lib (new commits)
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    更新

    如果在本地修改子仓库,在主仓库 git status会显示子仓库有修改。

    $git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
      (commit or discard the untracked or modified content in submodules)
    
        modified:   lib (modified content)
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    需要现在子仓库提交,然后再到主仓库提交代码。

    删除子仓库

    • 删除.gitsubmodule里相关部分
    • 删除.git/config 文件里相关字段
    • 删除子仓库目录。
    $ git rm --cached <本地路径>
    

    如果未按照上述步骤删除,可能残留在.git/modudles文件夹内。

    参考

    [1] Git submodule实战

     
     
    22人点赞
     
    Git
     
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/mouseleo/p/12002173.html
Copyright © 2011-2022 走看看