モデルから知るgit
DESCRIPTION
Git初心者向けの解説です。図が中心。TRANSCRIPT
俯瞰図
リポジトリ作業dir
リポジトリ作業dir
bareリポジトリ
リポジトリ作業dir
hoge/.git/configrefs...
hoge/.gitignorehoge.txt...
hoge.git/configrefs...
コミットとツリーAuthor: hirataraDate: 2/25 13:43:42
typoを修正
index.htmlimages/flower.gifcss/index.cssjs/index.jsツリー
コミット
マージコミット
ツリーオブジェクト• ある時点のコンテンツのスナップショット
• 厳密には、以下の組み合わせツリー = ディレクトリ
ブロブ = ファイル index.html
flower.gif index.js
ブランチ=refs/heads
• コミットへのrefは、全て.git/refsにある
• .git/refs/tags/ タグ
• .git/refs/heads/ ブランチ
• .git/refs/remotes/ 追跡ブランチ
refの検索の例• 以下の2つがあるとき
heads/hiratara/master
remotes/hiratara/master
• hiratara/master だとローカルブランチ
• remotes/hiratara/master が追跡ブランチ
リモート名
コミットのID
• ツリー(ある時点でのコンテンツ)に対してIDをつける→IDが一緒ならコンテンツも一緒
• コミットに対してIDをつける
→ポインタまで含めたID
→IDが一緒なら一連の履歴が完全一致
IDはSHA1
• こんなの
• ちょっとでも違う物には違うID
コミットコメントが違うだけでも
% git log --format=onelineb825e9d877e19804389494c349f4feb6e61af698 4th7eb449172702e32d08bb69a5527f0f99f157aa86 3rd99e990675c72155d1a8d2b5059b954bd852f53ca seconde282d0c2a31318f35eca789372c4579c53cfd292 initial
reflog
• タグのないコミットを探すのに便利
• git reflog show master
masterが指したコミットの履歴% git reflog show master835beaf master@{0}: rebase -i (finish): refs/heads/master onto 7e1871f29f2b87 master@{1}: commit: Added C57cd6f6 master@{2}: commit: Added Bc958196 master@{3}: commit: Added A7e1871f master@{4}: commit (initial): initial
コミットCを発見
.git ディレクトリ3領域とコマンド
リポジトリ
インデックス 作業ディレクトリ
D’
D
D’’
CBA
commitcommit -acommit filename
add
master
topic
リモートリポジトリ
A masterB C
A masterB C X
[email protected]:him/hoge.git
~/git-repos/hoge
直接リモートのデータは参照できない
D
git remote add
A masterB C
A masterB C X
github => [email protected]:him/hoge.git
~/git-repos/hoge
D
git remote add github git@...
git fetch
A masterB C
A masterB C X
github => [email protected]:him/hoge.git
~/git-repos/hoge
D
github/masterD
git fetch github(又はgit remote update)
refspec• 転送元・先を指定するフォーマット
• push, fetch で必要(fetchでは省略することが多い)
heads/fromheads/to
heads/toheads/from
github
git push github from:to
git fetch github from:to※普通はheads以下にはしない
fetchとrefspec
• git remote addした時のデフォルト[remote "github"]fetch = +refs/heads/*:refs/remotes/github/*
• 「+」はforceの意
mergeかrebaseする
A masterB C
A masterB C X
github => [email protected]:him/hoge.git
~/git-repos/hoge
D
github/masterD
X`
git rebase github/master
git push
A masterB C
A masterB C X
github => [email protected]:him/hoge.git
~/git-repos/hoge
D
github/masterD
X`
X`
git push github
push と refspec
• refspecを省略した時の振舞いに注意→"カレントブランチをpush"ではない
• push.default で振舞いを変えられるnothing, matching, tracking, current
• remote.<name>.pushは使ったことない
(F/Wの都合でfetch出来ない時など利用)
fetchとpullとpush(1)
heads/masterheads/another
remotes/github/masterremotes/github/anotherheads/masterheads/local-branchheads/another
github => [email protected]:him/hoge.git
~/git-repos/hogegit fetch githubremote.<name>.fetchによる
fetchとpullとpush(2)
heads/masterheads/another
remotes/github/masterremotes/github/anotherheads/masterheads/local-branchheads/another
github => [email protected]:him/hoge.git
~/git-repos/hoge git pull = fetch + merge
branch.<name>.mergeによる
branch.<name>.remoteによる
fetchとpullとpush(3)
heads/masterheads/another
remotes/github/masterremotes/github/anotherheads/masterheads/local-branchheads/another
github => [email protected]:him/hoge.git
~/git-repos/hogegit push githubpush.default=matchingによる
hanakoがtaroをfork
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1 master
git remote -f add
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1 master
hanako/topic1
hanako/master
git push hanako topic2
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1
hanako/topic1
hanako/mastertopic2
master
hanako/topic2
tarouへpull-req
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1
hanako/topic1
hanako/mastertopic2
master
pull request
hanako/topic2
taroがremote add
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1
hanako/topic1
hanako/mastertopic2
master
hanako/topic1
hanako/master
hanako/topic2
hanako/topic2
merge hanako/topic2
topic1 master
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1
hanako/topic1
hanako/mastertopic2
master
hanako/topic1
hanako/master
hanako/topic2
hanako/topic2
git push origin master
topic1
masterorigin/topic1
origin/master
origin/topic1
origin/master
topic2
topic1
hanako/topic1
hanako/mastertopic2
master
hanako/topic1
hanako/master
hanako/topic2
hanako/topic2
masterorigin/master