class: center, middle
class: center, middle
ファイル群の内容を特定することができるもの。
- ファイル内容のハッシュ値
- コミットメッセージ
- コミットユーザ
- コミット日
などで構成される。
commit id (上の構成物から作成したSHA1 ハッシュ値) で区別される。
47eb01894d2e1fcdccac84fe2316b50b5f2ee711
←こういうやつ
commit は immutable
一連の commit の先頭を表す名前。 「現在のbranch」という概念がある。
特定の commit から複数の子 commit を作成したい場合に使う。
branch は mutable (指し示すcommitは都度変化する)
.git/refs/heads/
の下にあるファイルが local branch を示している。
各ファイルの中身が先頭の commit id。
.git/refs/remotes/remote名/
の下にあるファイルは remote branch を示している。
現在の branch の最新 commit。
ひとかたまりの commit や branch 群を含むデータストア。
.git/
ディレクトリ内に格納されていることが多い。
特定の repository に対して、他の場所にある repository を名前をつけて保存できる。 保存したものを remote repository または単に remote と呼ぶ。
remote は commit や branch を転送する際に使う。
repository に含まれるファイルの作業用コピー。
.git/
ディレクトリと並ぶ場所に展開されていることが多い。
working copy に対して行った変更のうち、次の commit に含める一連の変更。
ref. git-add(1)
local branch に対応する remote の branch。
class: center, middle
かなり数があるので、よく使うものだけを紹介
別の repository を元にして repository を作る。
origin remote が clone 元に設定される。
$ git clone [email protected]:foo/bar.git
remote の追加削除など。
# [email protected]:my/bar.git を myremote という名前で追加
$ git remote add myremote [email protected]:my/bar.git
# myremote を削除
$ git remote remove myremote
一連の commit 履歴を参照する
$ git log
特定の commit 内容を参照する。 commit id を指定しない場合は現在 branch の最新 commit の内容。
$ git show [commit id]
index と working copy の差分や、commit と working copy の差分、commit と commit の差分を表示する。
# index と working copy の差分
$ git diff
# 現在 branch の最新 commit (HEAD) と working copy の差分
$ git diff HEAD
HEAD から変更された working copy のファイル一覧を表示する。
$ git status
working copy での変更を index へ登録する。
git commit
するまで commit は作成されない。
# foo.txt を登録
$ git add foo.txt
# すべての変更を登録
$ git add -A
ファイルの削除を index に登録。
git commit
するまで commit は作成されない。
$ git rm foo.txt
index の内容から commit を作成。
# コミットメッセージを指定して commit を作成
$ git commit -m 'My sample commit'
# commit を作成。実行するとエディタが開くのでコミットメッセージを記入。
# エディタは GIT_EDITOR 環境変数などで設定可能。
$ git commit
branch の操作
# branch 一覧を今いる branch を表示
$ git branch
# remote の branch を表示
$ git branch -r
# (commit id を先頭にした) branch foo を作成
$ git branch foo [commit id]
# branch foo を削除
$ git branch -d foo
現在の branch を移動
# 現在の branch を foo に移動
$ git checkout foo
remote の branch や commit を取得
# 全ての remote から取得
$ git fetch --all
他の branch を現在の branch に合流する。
# foo branch を現在の branch に合流する
$ git merge foo
他の branch の先頭に現在の branch の commit を積み重ねる。 HEAD は積み重ねた先に移動する。
注意!: これは歴史の改変行為です
# foo の先頭に現在の branch の commit を積み重ねる
$ git rebase foo
branch を remote に送信する。
# origin remote に master branch を送信
$ git push origin master
# 現在の branch を upstream に送信
$ git push
# origin remote に foo branch を送信。foo の upstream を origin/foo に設定。
$ git push origin foo -u
GitHub をブラウザで開いて、緑色のボタンから作りましょう。
- GitHub 上で作業したいレポジトリの fork を自分用に作る
git clone (forkしたレポジトリ)
で local に repository と working copy を作るgit remote add foo (fork 元のレポジトリ)
で fork 元を remote として追加
- fork元から最新を取得
git remote fetch foo
- 作業 branch の作成
git checkout -b bar foo/master
- ファイル変更
- commit 作成
git add xyz.txt
,git commit
- fork先に branch を push
git push origin foo
- GitHub のウェブサイトから pull request を作成
困ったらマニュアルを。
sub command を -
でつなげた名前で登録されている。
# git add の man を表示
$ man git-add
Reference でブラウザからも見える。