Gitのオブジェクトの仕組みについてよく書かれた資料。

Git from the bottom up - Lost in Technopolis
http://www.newartisans.com/blog/2008/04/git-from-the-bottom-up.html
http://www.newartisans.com/blog_assets/git.from.bottom.up.pdf

In my pursuit to understand Git, it's been helpful for me to understand it from the bottom up - rather than look at it only in terms of its high-level commands.

以下自分にとって重要なポイントをメモ。

commit, tree, blob

  • ファイルシステムにたとえると、treeはdirectoryに、blobはi-nodeに似て いる。

(個人的にはジャガイモに似ていると感じた。commitは茎、treeは地下茎、 blobは芋。親から枝分かれしたり合流したりしてつながっている網目のような 一連のcommitのそれぞれに、treeがぶら下がっていて、treeの下にはサブtree やblobがぶら下がっている。)

branch

  • branchはcommitに付けられた別名(alias)に過ぎない。ただし (commitをhashで指定する場合と違って)branchの名前が指す ものは更新されるので注意。

  • tagも特定のcommitに付けられた別名という点では同じ。

rebase

  • git rebaseには潜在的な危険があるので注意。

  • tip: privateなリポジトリではrebaseでよい。 publicなリポジトリではmergeを使うこと。

index

  • 意味から言えばstageとでも呼ぶべきもの。
  • indexは、次にcommitする際のtreeとなる。
  • tip: commitコマンドに-aオプションを付ければ、 ほとんどindexを気にせずに作業できる。

reset

  • git resetのオプションによる影響の違いと危険性

    • resetはref, index, wtに影響を与えうる。
  • --mixed: refを切り替え、index(の一部)を適宜revertする。

  • --soft : refを切り替える(indexはそのまま)。 (場合によっては下流が困る場合がある。)

  • --hard : refを切り替え、wtをheadに完全に合わせる。

    • checkoutは、indexがあればwtをそれに合わせる。 なければheadに合わせる。
  • tip: reset --hardは単独では実行せず、直前に必ずstashを 使うとよい。

stash

  • stashを使うと作業履歴を手元のblobに残せる。

  • stash --clearは使わず、reflog expire --expire=30.daysを使うとよい。

参考資料

上記を読んだ後に次の資料を読むとおさらいができる。

Tv's cobweb: Git for Computer Scientists
http://eagain.net/articles/git-for-computer-scientists/