Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Last active February 22, 2016 02:55
Show Gist options
  • Save tkawachi/db199f0da69b4403b90d to your computer and use it in GitHub Desktop.
Save tkawachi/db199f0da69b4403b90d to your computer and use it in GitHub Desktop.

class: center, middle

Pull request を送れるようになるために覚えておくこと


class: center, middle

Pull request を送るために覚えておく概念


概念: commit

ファイル群の内容を特定することができるもの。

  • ファイル内容のハッシュ値
  • コミットメッセージ
  • コミットユーザ
  • コミット日

などで構成される。

commit id (上の構成物から作成したSHA1 ハッシュ値) で区別される。 47eb01894d2e1fcdccac84fe2316b50b5f2ee711 ←こういうやつ

commit は immutable


概念: branch

一連の commit の先頭を表す名前。 「現在のbranch」という概念がある。

特定の commit から複数の子 commit を作成したい場合に使う。

branch は mutable (指し示すcommitは都度変化する)

.git/refs/heads/ の下にあるファイルが local branch を示している。 各ファイルの中身が先頭の commit id。

.git/refs/remotes/remote名/ の下にあるファイルは remote branch を示している。


概念: HEAD

現在の branch の最新 commit。


概念: repository

ひとかたまりの commit や branch 群を含むデータストア。 .git/ ディレクトリ内に格納されていることが多い。


概念: remote (repository)

特定の repository に対して、他の場所にある repository を名前をつけて保存できる。 保存したものを remote repository または単に remote と呼ぶ。

remote は commit や branch を転送する際に使う。


概念: working copy/tree

repository に含まれるファイルの作業用コピー。

.git/ ディレクトリと並ぶ場所に展開されていることが多い。


概念: index

working copy に対して行った変更のうち、次の commit に含める一連の変更。

ref. git-add(1)


概念: upstream (branch)

local branch に対応する remote の branch。


class: center, middle

Pull request を送るために覚えておく操作

かなり数があるので、よく使うものだけを紹介


git clone

別の repository を元にして repository を作る。

origin remote が clone 元に設定される。

$ git clone [email protected]:foo/bar.git

git remote

remote の追加削除など。

# [email protected]:my/bar.git を myremote という名前で追加
$ git remote add myremote [email protected]:my/bar.git

# myremote を削除
$ git remote remove myremote

git log

一連の commit 履歴を参照する

$ git log

git show

特定の commit 内容を参照する。 commit id を指定しない場合は現在 branch の最新 commit の内容。

$ git show [commit id]

git diff

index と working copy の差分や、commit と working copy の差分、commit と commit の差分を表示する。

# index と working copy の差分
$ git diff

# 現在 branch の最新 commit (HEAD) と working copy の差分
$ git diff HEAD

git status

HEAD から変更された working copy のファイル一覧を表示する。

$ git status

git add

working copy での変更を index へ登録する。

git commit するまで commit は作成されない。

# foo.txt を登録
$ git add foo.txt

# すべての変更を登録
$ git add -A

git rm

ファイルの削除を index に登録。

git commit するまで commit は作成されない。

$ git rm foo.txt

git commit

index の内容から commit を作成。

# コミットメッセージを指定して commit を作成
$ git commit -m 'My sample commit'

# commit を作成。実行するとエディタが開くのでコミットメッセージを記入。
# エディタは GIT_EDITOR 環境変数などで設定可能。
$ git commit

git branch

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

git checkout

現在の branch を移動

# 現在の branch を foo に移動
$ git checkout foo

git fetch

remote の branch や commit を取得

# 全ての remote から取得
$ git fetch --all

git merge

他の branch を現在の branch に合流する。

# foo branch を現在の branch に合流する
$ git merge foo

git rebase

他の branch の先頭に現在の branch の commit を積み重ねる。 HEAD は積み重ねた先に移動する。

注意!: これは歴史の改変行為です

# foo の先頭に現在の branch の commit を積み重ねる
$ git rebase foo

git push

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

Pull request の作り方

GitHub をブラウザで開いて、緑色のボタンから作りましょう。

Using pull requests


典型的な作業フロー (前準備)

  1. GitHub 上で作業したいレポジトリの fork を自分用に作る
  2. git clone (forkしたレポジトリ) で local に repository と working copy を作る
  3. git remote add foo (fork 元のレポジトリ) で fork 元を remote として追加

典型的な作業フロー (pull request づくり)

  1. fork元から最新を取得 git remote fetch foo
  2. 作業 branch の作成 git checkout -b bar foo/master
  3. ファイル変更
  4. commit 作成 git add xyz.txt, git commit
  5. fork先に branch を push git push origin foo
  6. GitHub のウェブサイトから pull request を作成

man の見方

困ったらマニュアルを。 sub command を - でつなげた名前で登録されている。

# git add の man を表示
$ man git-add

Reference でブラウザからも見える。


さらにこの先

  • Pro Git -- 無償公開されている Git の本
  • Reference -- 各 git サブコマンドのマニュアル
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment