ログイン
編集不可のページディスカッション情報添付ファイル
"alstamber/git"の差分

MMA
3と5のリビジョン間の差分 (その間の編集: 2回)
2012-09-04 23:04:30時点のリビジョン3
サイズ: 6894
編集者: alstamber
コメント:
2012-09-04 23:42:54時点のリビジョン5
サイズ: 9665
編集者: alstamber
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 5: 行 5:

== local repositoryとremote repository ==
 * gitは分散バージョン管理システム。
 * 分散バージョン管理システムではファイルの変更履歴を保持しているrepositoryが各開発者の手許に一つある。
 * 開発者は普段の開発ではプログラムを書いてそれを手許のrepository(local repository)に反映するという作業をやる。
  * この反映する作業をcommitという。
 * commitが溜まってきたら、それを各開発者で共有しているrepositoryにアップロードする。
  * 会社の場合だと会社の共有サーバなんかにおいてあるrepository。たいていremoteにあるのでremote repositoryという。
  * この作業をpushという。
 * 開発者はremote repositoryを見に行ってその内容を自分のlocal repositoryに統合するという作業をやる。
  * こうすることで自分のlocal repositoryに常に他の開発者の成果が反映されるようになる。
  * この作業をpullという。
 * local repositoryは一から作る場合と、元からあるremote repositoryをパチってきて作る場合がある。
  * 後者をcloneという。

== working directoryとindexとrepository ==
 * local repositoryは3段構えになっている。
 * 実際にファイルを置いといて弄るworking directory
 * ファイルの変更履歴を保持しているrepository
 * その仲立ちをするindex
 * commitするときは、まずindexにcommitしたいファイルを登録(addという)する。それからcommitする。
行 45: 行 66:
=== check differences of file ===
{{{
git diff <filename>
}}}
行 75: 行 100:
=== commit all file edited ===
 * git addする必要なし。
{{{
git commit -a
}}}
行 88: 行 119:
 * <repository>に<branch>をpushする。  * <repository>の<target_branch>にlocal repositoryの<source_branch>をpushする。
 * <target_branch>を省略すると、<source_branch>と同じ所にpushされる。
行 91: 行 123:
git push <repository> <branch>... git push <repository> <source_branch>:<target_branch>
行 95: 行 127:
 * <repository>をとってきて<branch>にmergeする。
{{{
git pull <repository> <branch>
}}}
 * <repository>の<source_branch>をとってきて自分のlocal repositoryの<target_branch>にmergeする。
{{{
git pull <repository> <source_branch>:<target_branch>
}}}
 * 例えばoriginのmasterをpullしたい時は
{{{
git pull origin master:master
}}}
 * pushと同じように略記が可能。
{{{
git pull origin master
}}}
行 139: 行 180:
=== create branch and checkout branch ===
 * branchつくって切替を同時にやる。
{{{
git checkout -b <branchname>
}}}
行 217: 行 263:
=== reset commit ===
 * 一つ前のcommitをsoftモードで消す。
  * HEAD^はHEADの一つ前という意味。
{{{
git reset --soft HEAD^
}}}
 * 消したcommitはORIG_HEADという名前で参照可能だから
{{{
git diff ORIG_HEAD
}}}
とすれば差分を閲覧できる。
行 228: 行 285:
=== amendを使う ===
{{{
git commit(あっ間違えた!)
(間違いを修正)
git commit --amend
}}}

== other ==
=== garbage correction ===
{{{
git gc
}}}

なにこれ

gitを運用する上での知識の整理。

local repositoryとremote repository

  • gitは分散バージョン管理システム。
  • 分散バージョン管理システムではファイルの変更履歴を保持しているrepositoryが各開発者の手許に一つある。
  • 開発者は普段の開発ではプログラムを書いてそれを手許のrepository(local repository)に反映するという作業をやる。
    • この反映する作業をcommitという。
  • commitが溜まってきたら、それを各開発者で共有しているrepositoryにアップロードする。
    • 会社の場合だと会社の共有サーバなんかにおいてあるrepository。たいていremoteにあるのでremote repositoryという。
    • この作業をpushという。
  • 開発者はremote repositoryを見に行ってその内容を自分のlocal repositoryに統合するという作業をやる。
    • こうすることで自分のlocal repositoryに常に他の開発者の成果が反映されるようになる。
    • この作業をpullという。
  • local repositoryは一から作る場合と、元からあるremote repositoryをパチってきて作る場合がある。
    • 後者をcloneという。

working directoryとindexとrepository

  • local repositoryは3段構えになっている。
  • 実際にファイルを置いといて弄るworking directory
  • ファイルの変更履歴を保持しているrepository
  • その仲立ちをするindex
  • commitするときは、まずindexにcommitしたいファイルを登録(addという)する。それからcommitする。

initial settings

git config --global user.name "<username>"
git config --global user.email "<mail address>"

create

create new repository

git init

create new public repository

  • 慣例としてディレクトリ名に.gitをつける。
  • --bareをつけると、bare repositoryになる。bare repositoryは管理情報のみを持つ。
    • commitなどができない。

git --bare init

clone

clone local repository

  • 既存のlocal repositoryを複製する。

git clone /path/to/repository

clone remote repository

  • 既存のremote repositoryを複製する。

git clone username@host:/path/to/repository

check

check status

  • working directoryとindexの状態を確認する。

git status

check differences of file

git diff <filename>

check repository log

  • repositoryの変更履歴を確認する。

git log

add/remove

add files

  • indexにaddする。

git add <filename>
  • すべてをaddする。

git add *

remove files

  • indexからremoveする。

git rm <filename>

commit

commit

git commit -m <message>

commit all file edited

  • git addする必要なし。

git commit -a

push/pull

add remote repository

  • push/pullするためにremote repositoryを予め登録しておく。

git remote add <name> <url>
  • 通例次のように使う。
    • push/pullでrepository名を省略するとoriginという名前につなぎに行こうとするので。

git remote add origin <remote repository url>

push

  • <repository>の<target_branch>にlocal repositoryの<source_branch>をpushする。

  • <target_branch>を省略すると、<source_branch>と同じ所にpushされる。

  • cloneして生成したrepositoryではgit pushだけでgit push origin masterと同じ事になる。

git push <repository> <source_branch>:<target_branch>

pull

  • <repository>の<source_branch>をとってきて自分のlocal repositoryの<target_branch>にmergeする。

git pull <repository> <source_branch>:<target_branch>
  • 例えばoriginのmasterをpullしたい時は

git pull origin master:master
  • pushと同じように略記が可能。

git pull origin master

branch

  • 最初はmasterというbranchが作られる。

masterとtopic branch

  • masterからtopic branchをを作成。
  • topic branchでの変更が終わったら、masterへmerge。

  • 現在使用しているbranchの先頭。
    • デフォではmasterの先頭。

stash

  • branchに対して何らかの変更を行った状態でbranchを切り替えると、その変更は切替先のbranchに引き継がれる。
  • 同じファイルが切替先のbranchで既に変更されているとconflictしてしまう。
  • 切替元branchにcommitしてから切り替えるか、一時的にstashと呼ばれる領域に変更を退避する。

merge

  • branchを統合する事を考える。
  • branch Aからbranch Bが分岐し、branch Bに変更を行った後branch Aとbranch Bをmergeすることを考える。
  • branch Aに何の変更も行われていなければ、branch Aの先頭をhead(=branch Bの先頭)にするだけでmerge完了。
    • fast-forward mergeという。
  • branch Aに変更が行われている場合は、両方の変更を取り込んだ新しいcommitを作成する。
    • headは新しいcommitに移動する。

rebase

  • branch Aからbranch Bが分岐し、branch Bに変更を行った状態を考える。
  • この状態でrebaseするとbranch Aの後ろにbranch Bの変更が付け足される。
  • branch Aはそのままなので、branch Aの先頭をheadに移動するにはmergeを行えば良い。

A successful git branching model

create branch

git branch <branchname>

list branch

git branch

checkout branch

  • branchの切替

git checkout <branch>

create branch and checkout branch

  • branchつくって切替を同時にやる。

git checkout -b <branchname>

merge branch

  • <commit>という名前のbranchがheadの指しているbranchにmergeされる。

git merge <commit>

delete branch

git branch -d <branchname>

correct conflict

  • conflictによってmergeに失敗した時はどうするか。
  • conflict箇所を修正してからcommitすればよい。

rebase branch

  • <branch>にたいしてrebase。

git rebase <branch>
  • conflictによって失敗した場合は該当箇所を修正し、

git rebase --continue

show differences between two branches

git diff <source_branch> <target_branch>

pullとmerge

  • pullはremoteから内容をとってきて、mergeするという作業をしているだけ。
  • 内容を取ってくるためのコマンドとしてfetchというものがあるので、内部的にはfetch→mergeしてるだけ。
  • pullの際のmergeの挙動とは?

local repositoryのbranchに何も変更を加えていない時

  • fast-forward mergeされるだけ。

local repositoryのbranchに変更をくわえている場合

  • 頑張ってmergeする。conflictする場合は知らせてくれるので、修正してcommit。

fetch

  • 前項で触れたが、remoteから内容を取ってくるだけのコマンドとしてfetchが存在する。
  • fetchでとってきた内容はFETCH_HEADという名前でcheckoutして参照可能。

tag

  • commitに名前をつける。

軽量タグと注釈付きタグ

  • 軽量タグは名前だけ付けられる。
  • 注釈付きタグは名前とコメント、署名を付けられる。
  • 軽量タグは使い捨て、注釈付きタグはリリースの際につけるフォーマルなもの。

add lightweight tag

  • headにlightweight tagをつける。

git tag <tagname>

add annotated tag

  • headにannotated tagをつける。

git tag -a <tagname>
  • commentをつける時は-mオプション。

list tag

git tag

delete tag

git tag -d <tagname>

correct commit

revert

  • 直前のcommitを帳消しにするcommitを新たに作る。

reset

  • commitを消す。
  • modeが3つある。
    • softではheadの位置だけを戻す。
    • mixedでは加えてindexも戻す。
    • hardではworking directoryも戻す。

reset commit

  • 一つ前のcommitをsoftモードで消す。
    • HEAD^はHEADの一つ前という意味。

git reset --soft HEAD^
  • 消したcommitはORIG_HEADという名前で参照可能だから

git diff ORIG_HEAD

とすれば差分を閲覧できる。

edit commit

commit --amend

  • 直前のcommitを編集する。

cherry-pick

  • 別のbranchから指定したcommitを現在のbranchに取り込む。

rebase -i

  • 過去のcommitの入れ替え、統合、削除、書き換え。

squash

  • mergeのオプション。
  • あるbranchのcommitすべてをまとめてひとつのcommitにして現在のbranchにmerge

amendを使う

git commit(あっ間違えた!)
(間違いを修正)
git commit --amend

other

garbage correction

git gc

alstamber/git (最終更新日時 2012-09-07 22:43:21 更新者 alstamber)