Skip to content

Instantly share code, notes, and snippets.

@timakin
Last active October 22, 2022 07:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timakin/34255f56dd5595cee421 to your computer and use it in GitHub Desktop.
Save timakin/34255f56dd5595cee421 to your computer and use it in GitHub Desktop.
Gitのsubtreeについてのまとめ

前提

  • まとめた目的

    • プロジェクトのルートディレクトリにsubtree先を展開できないかなーという話を検証したので、その記録
      subtreedDir/
        /A
        /B
      とかがあるとき、それをsubtreeとして呼び出す、childプロジェクトがあるとする。このとき、
      child/
        /subtreedDir
          /A
          /B
      じゃなくて、
      child/
        /A
        /B
      みたく、階層が深くなるの嫌だから、subtreedDirの中身だけそのまま持って来たいよね、という話を検証した。
    
  • subtreeとsubmoduleの違い

    • subtreeは別ブランチ切るようなもので、もとのブランチにコミットできる
    • submoduleはリポジトリをただただ参照するイメージ。
  • subtree-mergeとgit-subtreeの違い

    • まず、そんな違いはない。しいていうならprefix(subtreeから引っ張ってきたファイルの置き場所)の指定くらい。

      • Atlassianの資料
        • ここだと「何らかの理由で git subtree コマンドが利用できない場合であっても、マージ戦略を利用することは可能です。」程度にしか違いが書かれていない
        • たぶん、git-subtreeの方が、どこにsubtreeの内容があるのか意識しなくて済む、とかだと思う。
    • git-subtreeコマンド

    • subtree-merge(戦略というかパターンというか。とりあえずコマンドではない。)

初回:root_directoryにsubtreeリポジトリの中身を展開したいとき

  git remote add ansible git@github.com:timakin/timakin_vm_ansible.git
  git fetch ansible
  git branch ansible ansible/master

  # パターン1(展開成功ケース)
    git read-tree --prefix=/ -u ansible(できる。けど後述のパターン2で変更反映できない)

  # パターン2(展開失敗ケース)
    git subtree add --prefix=. ansible master(prefix already exists error)

  # subtree-mergeをmasterに反映
    git add .
    git commit -am 'test merge of subtree'

pullとかpushとか:変更を反映したいとき

# パターン1(失敗:subtree mergeパターン)
git checkout ansible
git pull
git checkout master
git diff-tree -p ansible

  # subtree参照先の変更を反映(失敗:subtree以外のファイルが全部消える)
    git merge -s subtree ansible master
    git pull -s subtree ansible master

# パターン2(�失敗:git-subtreeパターン)

# subtree参照先の変更を反映(微妙)
  git subtree pull -P . ansible master(conflictする。解消すればOKだが、、)
# subtree参照先にローカルの変更を反映(失敗)
  git subtree push -P . ansible master(pushできない。おそらくsubtreeのリポジトリに本来ふくまれないファイル(ルートディレクトリの他のファイル)がpushされようとしてるから)

通常通り子ディレクトリにsubtreeの中身を持ってくるケース(これは成功する)

git remote add ansible git@github.com:timakin/timakin_vm_ansible.git
git subtree add -P ansible/ ansible master
git subtree pull -P ansible/ ansible master
git subtree push -P ansible/ ansible master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment