More Related Content Similar to git 初めの一歩 (20) git 初めの一歩3. introduction
目的 git を使い、man を読むための最低限の知識をつ
ける
対象者 git を触った事が有るけれど、良く分からない人
注意 git はデフォルト設定で動作していると仮定
(設定次第では、この通りには動作しない)
注意 git の best practice ではありません
8. 中で起きている事
使ってみる
workspace
$ git config --global user.name ’Your Name’
$ git config --global user.email ’your@address’
home directory 直下の config file を編集
workspace, cached, repository には影響無し
24. git add コマンド補足
使ってみる
git add path1 [path2 [path3 ...]]2
で複数の path を一度に workspace へコピー可能
path が ディレクトリの場合、そのディレクトリ以下の
ファイルを再起的に git add する
workspace のトップディレクトリで git add . と行うと全
ファイルを git add できる
2
[ ] でくくってあるパラメータは省略可能という意味
25. git commit コマンド補足
git commit
でエディタが立ち上がり、そこで commit message を入
力すると新 commit が作成される
git commit -m message
でエディタを立ち上げずに commit message を指定する
事も可能
message の無い commit を作成する事はできない
39. git mv コマンド補足
使ってみる
git mv [-f] source destination
で source を destination に移動
通常の mv コマンドのように、destination がディレクト
リの場合、source は destination 以下のディレクトリに
移動
-f をつけると、既存のファイルを上書き出来る
(-f が無い場合、エラーとなる)
40. git rm コマンド補足
使ってみる
git rm [-r] [-f] path1 [path2 [path3 ...]]
で複数の path を削除可能
通常の rm コマンドのように、ディレクトリを削除する
場合は -r オプションが必要
workspace と cached の内容が違う時などは、誤操作防止
のために git rm はエラーになる
そのような場合、-f を使うと正常に削除可能
42. ここまでのまとめ
使ってみる
git commit は cached に message を添えて
commit を作成
HEAD が branch を向いている時は
branch を新しい commit へ向ける
branch は commit へのリンクの様な物
HEAD は現在の commit へのリンクの様な物
通常、HEAD は branch を指している事が多い
60. git branch コマンド補足 (一覧、作成)
branch を使用する
git branch [-a]
で現在の branch 一覧を表示
-a オプションをつけると、remote branch3 も表示する
git branch branch-name [start-point]
で start-point をさす branch を作成
start-point は branch, HEAD, ハッシュ値 4 等で指定
start-point のデフォルト値は HEAD
3
4
後述
後述
71. git checkout コマンド補足
branch を使用する
git checkout commit
で HEAD の commit へ切り替え
workspace, cached も会わせて変更
commit は branch, tag5 , ハッシュ値 6 等で指定
リリース時以外は HEAD はどこかの branch を指してい
る状態にすると良い
workspace や cached が clean でない場合 7 は
情報が失われる可能性があるので注意
5
後述
後述
7
未 commit のファイルがある場合
6
86. git merge コマンド補足
branch を使用する
実際には branch の状態によって merge 後の
repository の状態は異なる
(ここで挙げた挙動は一例)
コマンド実施後に message 入力画面が出てくる事と
出てこない事があるが
今回の範囲を超えるので詳細は割愛
いずれの場合でも、最新状態では 2 個の branch の
変更は全て含まれる
95. branch (削除)
branch を使用する
git branch -d branch1 [branch2 [branch3 ] ...]
で複数 branch の削除が可能
branch を削除しても、commit は消えない
branch を削除すると、その commit が
見つけにくくなる場合がある 8
そのような場合、誤操作防止の為に
git branch -d はエラーとなる
git branch -D を使用すると、上記の場合でも
branch の強制削除可能
8
後述
96. ここまでのまとめ
branch を使用する
git branch は branch の一覧、作成、削除
git checkout は HEAD の切り替え
会わせて、workspace, cached も変更
git merge は他の branch の変更を取り入れる
branch の作成、削除で commit が
作成、削除される事は無い
98. log
過去の履歴を活用する
過去の履歴を閲覧
$ git log --graph
* commit 86ba5efc1bce4f65f517eb80a3cc5c21113bc637
| Merge: ac24086 365850a
| | Author: Your Name <your@address>
| | Date: Wed Jan 1 02:42:46 2014 +0900
||
||
Merge branch ’develop’
||
| * commit 365850a9b09bb6fbcfc3f762b630380053b46575
| | Author: Your Name <your@address>
| | Date: Wed Jan 1 02:42:32 2014 +0900
||
||
Add e.
||
...
103. git log コマンド補足
過去の履歴を活用する
git log --graph [commit]
で commit の先祖を表示
commit は branch, HEAD, ハッシュ値等で指定
commit のデフォルトは HEAD
ハッシュ値はその commit の本名の要な物
git branch や git checkout で使用可能
(自信が無い場合は、git checkout で commit のハッシュ
値を指定する事はやめた方が無難)
git log で簡単に調べられるのは各 branch やその先祖
例えば、現在の最新状態の branch を削除すると最新
commit を見つける事が困難になる
(git branch -d でエラーが出るのはそのような場合)
104. reset
過去の履歴を活用する
branch と HEAD を一度に切り替える
(良く、過去のバージョンに戻すのに使用)
戻したいバージョン (例えば、“Add e.” という commit ) の
commit のハッシュ値を確認
$ git log --graph
...
(commit のハッシュ値 commit する度に異なるので注意)
今回は 365850a9b09bb6fbcfc3f762b630380053b46575 とする
106. reset
過去の履歴を活用する
branch と HEAD を一度に切り替える
(良く、過去のバージョンに戻すのに使用)
HEAD だけ古いバージョンに戻す
$ git checkout for soft
$ git reset --soft 36589
9
重複が無ければ、hash 値は先頭 4 文字以上で大丈夫
108. reset
過去の履歴を活用する
branch と HEAD を一度に切り替える
(良く、過去のバージョンに戻すのに使用)
HEAD, cached, workspace の全てを古いバージョンに戻す
$ git checkout for hard
$ git reset --hard 3658
125. git reset コマンド補足
過去の履歴を活用する
git reset [--soft | --mixed | --hard] [comit]
で HEAD, branch の切り替えと (オプションによって)
workspace や cached の変更を実施
オプションのデフォルトは --mixed、
commit は branch, HEAD, ハッシュ値等で指定
commit のデフォルトは HEAD
git reset とだけ実行すると、HEAD は変わらず cached
が HEAD と同じ状態になる
間違えて git add してしまった場合などに使用
HEAD を切り替えるので、git reset は git checkout と少
し似ている
違いは、branch も一緒に切り替えるかどうか
126. ここまでのまとめ
git log は過去の履歴や commit のハッシュ値を表示
commit のハッシュ値はその commit の名前のように使用
git reset は HEAD の移動や workspace, cached のクリア
等を実施
(オプションによって挙動は異なる)
128. status
レポジトリの状態を確認する
現在の cached の状態を確認
$ git status
Changes to be committed:
(use ”git reset HEAD <file>...” to unstage)
new file: f
Changes not staged for commit:
(use ”git add <file>...” to update what will be committed)
(use ”git checkout -- <file>...” to discard changes in working directory)
modified: f
Untracked files:
(use ”git add <file>...” to include in what will be committed)
g
132. status
レポジトリの状態を確認する
現在の cached の状態を確認
$ git add f
$ git status
Changes to be committed:
(use ”git reset HEAD <file>...” to unstage)
new file: f
Untracked files:
(use ”git add <file>...” to include in what will be committed)
g
workspace と cached の差分が無くなる
135. diff
レポジトリの状態を確認する
workspace, cached, commit 間の差分をパッチレベルで表示
$ git diff 3658
workspace と commit の差分を表示
diff --git a/c b/c
new file mode 100644
index 0000000..e69de29
diff --git a/f b/f
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/f
@@ -0,0 +1 @@
137. git diff コマンド補足
レポジトリの状態を確認する
git diff [--]9 [path1 [path2 [...]]]10
で path の workspace と cached の差分を表示
git diff commit [--] [path1 [path2 [...]]]
で path の workspace と commit の差分を表示
commit は HEAD や branch, ハッシュ値等で指定
git diff --cached commit [--] [path1 [path2 [...]]]
で path の commit と cached の差分を表示
git diff commit1 commit2 [--] [path1 [path2 [...]]]
で path の commit 間の差分を表示
9
10
曖昧さ回避の為のオプション、通常は必要ない
path が省略されると、git 管理下の全ファイル
138. ここまでのまとめ
レポジトリの状態を確認する
git status は workspace, cached, HEAD の状態を確認
git diff は workspace, cached, commit 間の差分を表示
git status はファイルの更新有無のみ、
git diff はパッチレベルの差分 (ファイルの中身) を表示
commit 前に git status で確認するのは良い習慣
142. git init コマンド補足 (リモート)
使ってみる
多くの git server では URI を ssh や https で指定するが
それはネットワーク的な問題
repository 自体はこの用法で作成している事も多い
この方法で作成した remote repository は
workspace が無いので
普通の local repository としては使用不可
その他、git の各種デフォルト設定が異なっている
163. git push コマンド補足
使ってみる
git push URI source-branch[:destination-branch]
で source-branch を URI の destination-branch にコピー
destination-branch のデフォルトは source-branch
git push URI :destination-branch
で URI の destination-branch を削除
(空の branch をコピーすると思えば良い)
remote repository の branch を削除しても local
repository の branch は残る
commit のコピーとは、commit 自身, その先祖の全
commit, 各 commit で必要な全ファイル、ディレクトリ
のコピー
176. git clone コマンド補足
複数人で開発する
git clone URI [directory ]
で remote repository の clone を directory に作成
directory のデフォルトは URI の reopsitory 名
git の top directory はいつでも変更してよい
git clone 時に指定した URI は、origin というエイリアス
で登録される
git branch -a
で全ての remote branch が閲覧可能
master 以外の必要な branch は
git branch branch-name start-point で作成
(remote branch は閲覧専用として使用するべき)
187. ここまでのまとめ
複数人で開発する
git clone は remote repository の clone を作成
clone には remote repository の全 branch が remote
branch として登録済み
remote repository にある branch を local に作成するには
git branch を使う
git fetch は remote branch を最新状態に更新
remote repository の更新を適用するには
fetch の後に、remote branch を merge すれば良い
remote repository は読み込み専用で使うべき
188. 同時更新
更新が競合したら
local rep と clone rep で同時更新
local rep で更新
$
$
$
$
$
cd ˜/local rep
touch h
git add h
git commit -m ’Add h.’
git push origin master
189. 同時更新
更新が競合したら
local rep と clone rep で同時更新
clone rep で更新
$
$
$
$
$
cd ˜/clone rep
touch i
git add i
git commit -m ’Add i.’
git push origin master (失敗)
190. 同時更新
更新が競合したら
local rep と clone rep で同時更新
clone rep で local rep の更新を取り込み、再 push
$ git fetch --all -p
$ git merge remotes/origin/master
$ git push origin master
191. 同時更新
更新が競合したら
local rep と clone rep で同時更新
local rep に clone rep の更新を取り込む
$ cd ˜/local rep
$ git fetch --all -p
$ git merge remotes/origin/master
248. conflict 補足
更新が競合したら
git merge --abort の過信は禁物
workspace や cached が clean で無い場合 11 、正しく戻ら
ない可能性がある
conflict 解決時、勝手に他人の更新を変更する事は危険
自分の更新を変更するか、競合する更新をした人に確認
するべき
abort せずに直接 conflict を解消する際は、
競合部分の削除と順番入れ替えのみで対応するべき
(先祖の commit に含まれない更新を merge commit に入
れるべきではない)
(これ以外の手段で conflict を解消する方法は evil merge
と言われている)
11
未 commit のファイルがある状態
250. ここまでのまとめ
更新が競合したら
local repository に remote repository の最新情報が含まれ
ていないと git push は失敗する
そのような場合は local repository を最新にしてから再
push すると良い
merge 時に conflict が発生した際に取る方法は、merge
取りやめか conflict 解消の 2 通り
merge を取りやめて、衝突しないように commit してか
ら再度 merge する事も可能
git blame はファイルの更新者を行単位で表示
251. revert
その他
特定の commit を打ち消す commit を行う
(commit とその親 commit の差分を打ち消す)
戻したいバージョン (例えば、“Add h.” という commit ) の
commit のハッシュ値を確認
$ git log --graph
...
(環境ごとに commit のハッシュ値は異なるので注意)
今回は 06d957bc3322557eb76fc86ee87b66c0288d3c08 とする
258. git revert コマンド補足
その他
git revert commit1 [commit2 [commit3 ...]]
で特定の commit の変更を打ち消す commit を行う
親が複数存在する commit13 を指定するには、-m という
オプションが必要
(今回の範囲を超えるので詳細は割愛)
13
git merge で作成された commit
264. tag
コマンド補足
git tag tag-name [commit]
で commit を指す tag を作成
commit は branch, HEAD, ハッシュ値等で指定
commit のデフォルトは HEAD
tag を remote repository へ転送するには
git push URI tag tag-name
remote repository の tag を削除するには
git push URI :refs/tags/tag-name
remote repository の tag も git fetch --all -p で取得可能
merge や checkout の commit 指定に tag も使用可能
ただし、その場合 HEAD が branch から外れるので注意
265. ここまでのまとめ
その他
git revert は過去の commit を打ち消す commit 作成
remote repository に push する前なら、他の方法 14 で
代用出来ないか考えた方が良い
(不要な commit と打ち消す commit が有るより、両方無
い方が良い場合が多い)
git tag は tag 一覧表示、作成、削除
branch や HEAD は移動するが tag は移動しないので、
リリースヴァージョン管理等に使用可能
14
git reset 等
268. 最後に
man の探し方
git の man は、“git” と最初の引数を “-” でつなげた物を読む
例) $ man git-add
最初の引数まで入力して --help と入力しても同じ
例) $ git add --help
269. 最後に
man の探し方
git の man は、“git” と最初の引数を “-” でつなげた物を読む
例) $ man git-add
最初の引数まで入力して --help と入力しても同じ
例) $ git add --help
そもそも最初の引数が分からない場合や、
git でどんな事が出来るか知りたい場合は
git の man を引く
$ man git
271. 最後に
$ git help -g
The common Git guides are:
attributes Defining attributes per path
glossary A Git glossary
ignore Specifies intentionally untracked files to ignore
modules Defining submodule properties
revisions Specifying revisions and ranges for Git
tutorial A tutorial introduction to Git (for version 1.5.1 or newer)
workflows An overview of recommended workflows with Git
’git help -a’ and ’git help -g’ lists available subcommands and some
concept guides. See ’git help <command>’ or ’git help
<concept>’
to read about a specific subcommand or concept.