Skip to content

Instantly share code, notes, and snippets.

@qiulang
Last active March 13, 2018 02:24
Show Gist options
  • Save qiulang/8d439cb1b0abd629c427fcaf6253f01b to your computer and use it in GitHub Desktop.
Save qiulang/8d439cb1b0abd629c427fcaf6253f01b to your computer and use it in GitHub Desktop.
git subtree 使用总结

使用subtree初衷

  1. 原有项目只有一个repo,每个子目录对应一个子项目
  2. 子项目越来越复杂,把他们从原repo拆分出来,独立成一个自己repo
  3. 每个子项目还能以某种出现在主项目的子目录,方便主项目统一管理
  4. 对于拆分,通过调查发现有 git filter-branch 和 git subtree split -P
  5. 对于子项目管理有 git subtree add -P 和 git submodules, 网上都说submodules问题很多,还是用subtree好 ,当然我们没有submodules 使用经验,不知道问题在哪。
  6. 但是subtree 同时可以完成两个任务,所以是我们首选工具,并做了仔细调查研究

拆分用哪个命令好,到底有什么不同

  1. git filter-branch vs git subtree split,到底什么不同,用哪个好
  2. SO上讨论 Detach (move) subdirectory into separate Git repository
  3. github 推荐 filter-branch
  4. 他们到底有什么不同?
  5. 从我们自己使用情况,就是用法不太一样,如果有n个子目录要拆出来,用filter-branch之前要clone n次,用git subtree要 git rm n 次

子模块管理,为什么在Sourcetree看不到subtree

  1. 拆完后再用subtree add加回主项目,从目录结构上看和没拆之前没有不同自然最好。
  2. 但是为什么Sourcetree根本看不到subtree的显示?原来这还不是一个简单的问题。
  3. 首先, git没有提供直接命令看一个repo里有没有subtree
  4. 变通办法 git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq
  5. 由于这个原因,Sourcetree没法直通在命令行下加subtree有哪些;而且即便通过 Sourcetree加subtree, 被clone后,在新的repo还是看不到。
  6. 用git remote add 把子项目作为一个remote源是一个变通办法,这样也便于指定子repo
  7. Soucetree不能显示subtree问题被开过几个同样bug,解决办法 编辑sourcetreeconfig文件
  1. Open "REPO.git\sourcetreeconfig"
  2. Find the "SubtreeLinks" section.
  3. Copy and paste the "SubtreeLinks" section to the other machine's "sourcetreeconfig" file.

还有别的管理子模块的办法吗?

  1. 使用子模块一个主要原因是该子模块被几个模块复用。

    • 如果子模块只是作为第三方库引入到主项目里,主项目可以不需要知道(也不需要关心)他的git history, 这时候可以用git subtree add --squash 这样版本树上只显示一次commit;
    • 如果没有用 squash 开关,子模块完整commit 历史都会显示在主模块里。
  2. 前端代码和服务器端代码属于同一项目,放在一起统一管理很自然。

    • 前端打包生成的代码直接放到服务器端相应目录下,打包生成文件不进入版本管理。
    • 如果没有复用情况,确实也没有必要通过subtree方式管理,而是直接放在同一项目不同子目录下。不然在主项目里修改子项目代码,要再push back upstream
  3. Git and project dependencies 提到不用git 用项目相应打包管理工具也可以,比如node就用npm

  4. rebase子repo会带来问题 , 没有深入研究

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment