マージの衝突を解決

マージの衝突を解決
#15 マージの衝突を解決してみよう (1)
http://dotinstall.com/lessons/basic_git/6715
を参考に
マージの衝突を解決する
新しくブランチをつくって
さらに、そのブランチに移動する
つまり checkout まで行うには
git checkout -b hogehoge
というようにすればOK
書式としては
git checkout -b ブランチ名
となる
確認のため
git branch とすると
hoge
* hogehoge
master
となっていて
作成した hogehoge に移動しているのが確認できる
ここからは git のマージの衝突を実行
vim index.html
でファイルを
line1 から line first へ変更して保存
git add .
でインデックス化して
git commit -m “not 1 but fitst”
とすることで
vim を起動せずにリポジトリに保存
ここまではいままでと同じ
ここからが衝突
まず、ブランチを hogehoge から master に変えるので
git checkout master
そして、 vim index.html で編集
中身は、ブランチごとにわけられているので
line1
をmaster のほうでは
line 1st
と変更して保存
そして、同じように
git add .
git commit -m “not 1 but 1st”
と実行
この状態でマージすると衝突が発生する
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
このように、問題発生して
解決しないと git commit できないという状況になる
この解決方法が動画で紹介されているのが
#16 マージの衝突を解決してみよう (2)
http://dotinstall.com/lessons/basic_git/6716
のほう
現状をまずは git status で確認
# On branch master
# Unmerged paths:
# (use “git add/rm …” as appropriate to mark resolution)
#
# both modified: index.html
#
no changes added to commit (use “git add” and/or “git commit -a”)
とメッセージがでて
修正を促される
このため、vim index.html でファイルを修正する
ファイルを開くと
<<<<<<< HEAD line 1st ======= line first >>>>>>> hogehoge
line2
となっている
現在のブランチは master のほうで
<<<<<<< HEAD line 1st ======= が master のほう line first >>>>>>> hogehoge
line2
が hogehoge ブランチのほうで
このうちどちらかを残すか、もしくは修正することになる
今回は master のほうを残しておきたいので
修正するには
<<<<<<< HEAD line 1st ======= line first >>>>>>> hogehoge
line2

line 1st
line2
というように、いらない部分を削除して保存
あとは git add .
git commit -m “confilict fixed”
というように、ふつうに git commit できるようになる
確認するには
git log

commit 734665444c30621d32b3800d56ffd1dcd5583201
Merge: 6379343 a989662
Author: snowpool
Date: Thu May 30 20:09:07 2013 +0900
confilict fixed
commit 637934389be80431ac468343f50129bb0a587018
Author: snowpool
Date: Thu May 30 19:57:21 2013 +0900
not 1 but 1st
commit a989662710cb5542428878d9de9028c89920c2f7
Author: snowpool
Date: Thu May 30 19:52:36 2013 +0900
not 1 but fitst
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
(END)
commit a989662710cb5542428878d9de9028c89920c2f7
Author: snowpool
Date: Thu May 30 19:52:36 2013 +0900
not 1 but fitst
以下略
というように
commit できているのが確認できる

git でブランチをマージする

git でブランチをマージする
#14 ブランチをマージしてみよう
http://dotinstall.com/lessons/basic_git/6714
を参考に
ブランチをマージする方法を学習
前回作成した hoge の中で作成した
myscript を
master のほうへ反映させたいときには
git merge
を使う
まず、 git checkout master
で hoge から master にブランチを移動する
次に
git merge hoge
とする
書式としては
git merge 取り込みたいブランチ名
というかんじ
結果に
Updating 1110869..cce3862
Fast-forward
myscript.js | 1 +
1 file changed, 1 insertion(+)
create mode 100644 myscript.js
とでて反映される
本当に反映されているか確認したいときには
git log
を実行して
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
というように、script added が追加されていることを確認できる
もし、作成した hoge が
もう機能を取り込んだので、不必要なので消したい
というときには
git branch -d hoge
このように、テスト環境のブランチを作成し
いらなくなったら
git branch -d ブランチ名
で削除する
というようにしていくことで、効率的な開発が可能になる

http://dotinstall.com/lessons/basic_git/6714

の補足情報に
ほかにもいろいろ載っているので
こちらも参考にしながら使っていこうと思います

git でブランチ

git でブランチ
#13 ブランチを使ってみよう
http://dotinstall.com/lessons/basic_git/6713
を参考に、ブランチを行ってみた
ブランチとは
分岐、枝分かれという意味
複数バージョンを平行して開発するときに使う
git branch
とすると
いまあるブランチ一覧がみれる
最初は
master
のみ
ブランチを作成するには
git branch ブランチ名
とする
今回は
git branch hoge
この状態で git branch すると
hoge
*master
というように2つになっている
master の横に*がついているけど
これは今いるブランチを示している
なので、hoge に移動する
移動するときには
git checkout ブランチ名
今回は
git checkout hoge
で成功すると
Switched to branch ‘hoge’
と表示される
この状態で git branch で確認すると
* hoge
master
というように
hoge のほうに移動して*がついているのが確認できる
これにより、各ブランチごとに状態をわけることが可能
つまり、データベースに近い感覚
使うデータベースが ブランチ
中のテーブルとかが それぞれのバージョン
というかんじになる
試しに
vim myscript.js
を作成して、中身を
alert();
だけ記述して保存
git add .
git commit -m “script added”
で追加
git log
で確認すると
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
となっている
この状態から
master のほうへ戻りたいときには
git checkout master
を実行
そして、 git log を実行して状態を確認してみると
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
というように、
hoge のほうでは追加した script added
が入っていないことが確認できる
web 開発をするときに最初にベースを作って
そこから派生して作成するというときに便利

git reset の取り消し方法

git reset の取り消し方法
#12 過去のバージョンに戻ってみよう (2)
http://dotinstall.com/lessons/basic_git/6712
を参考に
git reset の取り消し方法を学習
前回の状態が
ORIG_HEAD に保存されているため
git reset を実行した状態から
前回の状態に戻したいのなら
git reset –hard ORIG_HEAD
とする
これで
line1
だけの現状から
line1
line2
となっていた1つ前のファイル状態へ戻ることができる

git で過去バージョンへ戻す方法

git で過去バージョンへ戻す方法
#11 過去のバージョンに戻ってみよう (1)
http://dotinstall.com/lessons/basic_git/6711
を参考に過去バージョンに戻す方法を学習
バージョン管理システムの便利なのは
過去に戻って変更点をなかったことにできること
vim index.html
でファイルを編集
まず
line1
line2
となっているので
line3
を最終行に追記
git add .
でインデックス化
本来なら、git commit するけど
これを git log をみて直前の状態に戻したい場合には
git reset
を使う
一気に戻したいときには
オプションの
–hard を付ける
今回は何回か commit を繰り返しているけど
直前の commit 状態まで戻したいなら
HEAD を使う
オプションまでつけたコマンドは
git reset –hard HEAD
となる
現在の変更したindex.html は
line1
line2
line3
これを
git reset –hard HEAD
を実行すると
HEAD is now at 1110869 line2 を追加
と表示され
前回の commit 状態である
line1
line2
に戻っている
2つ前の状態まで戻したいのなら
git reset –hard HEAD^
というように
^ を追記する
また、もっとさらに前に戻したいのなら
git log で履歴をだして
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
となっている部分の cpmmit ID を指定すればいい
今回は、もっと前の状態まで戻したいので
commit 53d156650c331b31b33c8907f25527e61978dfcf
のところまで戻してみた
git reset –hard 53d156650c
IDは全部いれずに最低最初から7ケタあれば十分
あとは、中身を確認すると
cat index.html
line1
というように、最初の状態まで戻すことができる

git の直前のコミットの変更

直前のコミットの変更
#10 直前のコミットを変更する
http://dotinstall.com/lessons/basic_git/6710
を参考に
コミットについて学習
すでに、前回
git add .
でインデックス状態なので
git commit
を実行するけど、一行しかコメントをつけないのなら
fit commit -m “line2 を追加”
というようにすれば
commit できる
実行結果は
[master f754810] line2 を追加
Committer: snowpool
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config –global user.name “Your Name”
git config –global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit –amend –reset-author
2 files changed, 2 insertions(+)
create mode 100644 .gitignore
結果を確認するため
git log
とすると
commit f7548100f0a9eedc4fee017f677ca19bfc5cd1cb
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
となり、
入力したコメントが反映されている
書式としては
git commit -m “メッセージ”
となる
あと git commit したファイルの修正について
これは
vim index.html
で編集して
git add .
で再びインデックス化
そして
git commit –amend
と実行する
すると vim がたちあがり
コメントを変更するかどうか聞かれるので
:wq
でそのまま保存
これで余計に commit を増やさずに
変更を保存できる
ちなみに、git log で確認した結果は
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
というように
変更がされていないことが確認できる

git に特定のファイルを含めない方法

git に特定のファイルを含めない方法
#09 git管理に含めない設定について
http://dotinstall.com/lessons/basic_git/6709
を参考に git について学習
とりあえず、
vim error.log
でファイルを作成してみた
git add .
で一気にファイルをインデックスにできるけど
インデックスに含めたくないファイルもある
例えばログファイル
そんなときには
vim .gitignore
で設定ファイルを作成し
*.log
というようにファイル拡張子を指定することで
ログ関連ファイルはインデックス化しないようになる
とりあえず、現在のファイルは
index.html
error.log
.gitignore
となっているけど
git status を実行した結果は
# On branch master
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
#
# modified: index.html
#
# Untracked files:
# (use “git add …” to include in what will be committed)
#
# .gitignore
となっていて
error.log が対象から外れているのが確認できる
これも実際にやったほうが分かるので
git add .
で一気にインデックス化して
git status
で確認すると
# On branch master
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
#
# new file: .gitignore
# modified: index.html
#
というように
error.log が対象から外れてインデックス化されたことが
確認できる
.gitignore ファイルの有効範囲は
作成したディレクトリから下全部になる
このため、一番上に書くと
そこからのサブディレクトリまで設定は反映されるので注意

git でのファイル操作

git でのファイル操作
git で
git add するときのオプション
ファイルの削除、移動するときのコマンドを
#08 gitでのファイル操作について
http://dotinstall.com/lessons/basic_git/6708
を参考に学習
いままでの学習だとファイルを1つずつ実行していたけど
現実のプロジェクトではまとめて行うことになる
こんなときには
git add .
とする
これは、今のディレクトリより下のファイルを全て
git add するという意味になる
また、git add でインデックス化したファイルの削除は
git rm index.html
というようにして
移動するときにも
git mv index.html
というようにする
これは Linux コマンドで rm とか mv で移動してしまうと
いままで管理しているファイルがどこにいったのか
把握できなくなるので、
git 管理下においたファイルは
git rm
git mv
といったコマンドを使って管理することになる

git で差分確認

git で差分確認
#07 差分を確認してみよう
http://dotinstall.com/lessons/basic_git/6707
を元に
git での差分確認
これができると、どこを修正したかわかるようになる
わかりやすさを求めるので
index.htmlを編集して
line2 を最終行へ追記
この状態で
さきほどのように
git status
とすると
まだ add も commit もされていないということと
index.html が編集されたということがでてくる
そして、今回は変更されたところを見たいので
git diff
と実行
すると
diff –git a/index.html b/index.html
index a29bdeb..c0d0fb4 100644
— a/index.html
+++ b/index.html
@@ -1 +1,2 @@
line1
+line2
と表示される
ただし、確認できるのは
git add でインデックスに追加していない状態のものを
確認するもの
git add を行ったものは別のコマンドを使って確認することになる
それが
実験のため
git add index.html でインデックス登録
そして
git status
で確認すると
# On branch master
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
#
# modified: index.html
#
となり
index.html は編集されたけど
まだ commit されていないということが表示される
このインデックス状態で差分を調べるには
git diff –cached
とする
これで、 git commit する前、つまりインデックス登録状態の差分を表示できる
ちなみに、実行結果は
diff –git a/index.html b/index.html
index a29bdeb..c0d0fb4 100644
— a/index.html
+++ b/index.html
@@ -1 +1,2 @@
line1
+line2

git の状態把握

git の状態把握
git で
どのファイルがどの状態で管理されているかを把握する
ことについて学習
#06 現在の状態を把握しよう
http://dotinstall.com/lessons/basic_git/6706
を、もとに学習
vim index.html

最終行へ line2 を追記
そしたら、
git status
で現状の確認
すると
# On branch master
# Changes not staged for commit:
# (use “git add …” to update what will be committed)
# (use “git checkout — …” to discard changes in working directory)
#
# modified: index.html
#
no changes added to commit (use “git add” and/or “git commit -a”)
と表示され
modified: index.html
と表示される
modified は変更されたという意味なので
modified : index.html

index.html が変更されたという状態を示す
no changes added to commit (use “git add” and/or “git commit -a”)
の部分は
まだ add  も commit もされていないということ
# (use “git add …” to update what will be committed)
は変更を保存するなら
git add
しましょうという意味で
# (use “git checkout — …” to discard changes in working directory)
間違えているのなら
git checkout
を使って修正しましょう
という意味
が表示されている
今回は、修正の仕方がでているので
この機会に実践
git checkout — index.html
これを実行した後に
cat index.html
を実行すると
line1
line2
だったのが
line1
だけに修正されている
今回は単純な修正だったけど
プログラミングのソースの修正には非常に役立つ
すべて修正するのはかなり面倒だけど
こうやって管理できると非常に楽