Gitに入門する -その7-

blog.takanabe.tokyo

ステップ2の「Gitを初めからていねいに」をやる。

「ひとりでつかう - もっとマージ」より。


メモ

Git 運用のコツとして、なにか変更を行うときにはブランチを作ってそちらのブランチで作業するとよい、というのがあります。

準備

my_second_workspaceという作業ディレクトリとリポジトリを作成。

$ mkdir my_second_workspace
$ cd my_second_workspace
$ git init

cat_lover_said.txtとcat_hater_said.txtを作成してコミットする。

$ git add .
$ git commit

$ git graph
* 7240214  (HEAD -> master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

①ファイルの編集を行う。

例:文体の統一

まずブランチを切る。

$ git branch unify_styles
$ git checkout unify_styles 

# $ git checkout -b unify_styles
# とやるとgit branchとgit checkoutが同時にできる。
# つまりブランチを切って切り替えまでがまとめてできる。

$ git graph
* 7240214  (HEAD -> unify_styles, master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

文章を修正して、git addして、git commitする。

$ git graph
* 8525d74  (HEAD -> unify_styles) 2015-12-05 user_name 文体を統一
* 7240214  (master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

②同じファイルの別の編集を行う。

例:8525d74のコミットをリリースする前に別の編集をする。

いったんmasterに戻って編集する。

$ git checkout master

$ git graph
* 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
* 7240214  (HEAD -> master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

これで文体を統一する前の状態のファイルに戻った。

文体統一とは別の編集のためのブランチを切る。

$ git checkout -b hotfix

$ git graph
* 8525d74  (unify_styles) 2015-12-05 y-nakajima 文体を統一
* 7240214  (HEAD -> hotfix, master) 2015-12-05 y-nakajima cat_hater_said.txtとcat_lover_said.txtを追加

そしてhotfixブランチ上で問題のある表現を修正してcommitする。

$ git graph
* 395fc0c  (HEAD -> hotfix) 2015-12-05 user_name 問題のある表現を修正
| * 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
|/
* 7240214  (master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

③マージする

例:hotfixで行った変更をmasterブランチに取り込む。

masterブランチに切り替えて、マージする。

$ git checkout master
Switched to branch 'master'

$ git merge hotfix
Updating 7240214..395fc0c
Fast-forward
cat_hater_said.txt | 2 +-
cat_lover_said.txt | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

$ git graph
* 395fc0c  (HEAD -> master, hotfix) 2015-12-05 user_name 問題のある表現を修正
| * 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

前回はgit mergeするとエディタが立ち上がったのだが、
今回は立ち上がらない。
それは、7240214のコミットと395fc0cのコミットが分岐してないかららしい。
そういうときは、masterをhotfixのところまで進めるにとどまるらしい。
こういうマージの仕方はFast-forwardというらしい。

④マージを戻す

$ git reset --hard 7240214
HEAD is now at 7240214 cat_hater_said.txtとcat_lover_said.txtを追加

$ git graph
* 395fc0c  (hotfix) 2015-12-05 user_name 問題のある表現を修正
| * 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
|/
* 7240214  (HEAD -> master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

戻りますた。

⑤Fast-forwardを使わないマージ

$ git merge --no-ff hotfix
Merge made by the 'recursive' strategy.
cat_hater_said.txt | 2 +-
cat_lover_said.txt | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

$ git graph
*   8c4a06b  (HEAD -> master) 2015-12-05 user_name Merge branch 'hotfix'
|\
| * 395fc0c  (hotfix) 2015-12-05 user_name 問題のある表現を修正
|/
| * 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

マージしたというコミットがあるのでこっちの方がいいかもしれないです。

hotfixブランチは用済みなので削除する。

$ git branch -d hotfix
Deleted branch hotfix (was 395fc0c).

$ git graph
*   8c4a06b  (HEAD -> master) 2015-12-05 user_name Merge branch 'hotfix'
|\
| * 395fc0c  2015-12-05 user_name 問題のある表現を修正
|/
| * 8525d74  (unify_styles) 2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

$ git branch
* master
 unify_styles

⑥①の修正にさらに修正が発生

①とりあえずunify_stylesブランチに戻り、
②ファイルを編集し、
③コミットする。

$ git graph
* 821eb6e  (HEAD -> unify_styles) 2015-12-05 user_name へっへっへという表現を復活
| *   8c4a06b  (master) 2015-12-05 user_name Merge branch 'hotfix'
| |\
| | * 395fc0c  2015-12-05 user_name 問題のある表現を修正
| |/
* | 8525d74  2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

masterブランチにマージする。

$ git checkout master
Switched to branch 'master'

$ git merge unify_styles
Auto-merging cat_lover_said.txt
CONFLICT (content): Merge conflict in cat_lover_said.txt
Auto-merging cat_hater_said.txt
CONFLICT (content): Merge conflict in cat_hater_said.txt
Automatic merge failed; fix conflicts and then commit the result.

conflictが出ました。
編集内容がバッティングしたようです。
自動マージ失敗:conflictを修正してコミットしてくださいとのこと。

git statusで状況を確認する!

$ git status
On branch master
You have unmerged paths.
 (fix conflicts and run "git commit")

Unmerged paths:
 (use "git add <file>..." to mark resolution)

       both modified:   cat_hater_said.txt
       both modified:   cat_lover_said.txt

no changes added to commit (use "git add" and/or "git commit -a")

both modifiedと出てるファイルの中身を見てみる。

~(略)~<<<<<<< HEAD
猫好きな人間がいるのが不思議。
=======
猫が好きな人間は、頭がおかしいのではないだろうか。
>>>>>>> unify_styles

「<<<<<<< HEAD」から「=======」までがHEADで編集された内容で、
「=======」から「>>>>>>> unify_styles」までがunify_stylesブランチで編集された内容。
なのでそこを手動で修正する。
修正できたらgit addしてみる。

$ git add cat_hater_said.txt

$ git status
On branch master
You have unmerged paths.
 (fix conflicts and run "git commit")

Changes to be committed:

       modified:   cat_hater_said.txt

Unmerged paths:
 (use "git add <file>..." to mark resolution)

       both modified:   cat_lover_said.txt

おk。

ではもう一個のファイルもconflictを解消してgit add。

$ git add cat_lover_said.txt

$ git status
On branch master
All conflicts fixed but you are still merging.
 (use "git commit" to conclude merge)

Changes to be committed:

       modified:   cat_hater_said.txt
       modified:   cat_lover_said.txt

すべてのconflictが解消されたけどまだマージ中ですとのこと。
マージを完了させるにはgit commitしてくださいと。

$ git commit
[master c56cd0a] Merge branch 'unify_styles'

エディタが立ち上がる。

Merge branch 'unify_styles'

Conflicts:
cat_hater_said.txt
cat_lover_said.txt
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
# .git/MERGE_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# All conflicts fixed but you are still merging.
# (use "git commit" to conclude merge)
#
# Changes to be committed:
#
# modified: cat_hater_said.txt
# modified: cat_lover_said.txt

unify_stylesブランチをマージします。
conflictしたファイル:
cat_hater_said.txt
cat_lover_said.txt
#マージをコミットしているようですね。
#違うならこのファイルを消してください。
# .git/MERGE_HEAD
#そんでtry again.

問題ないので保存して終了。

$ git graph
*   c56cd0a  (HEAD -> master) 2015-12-05 user_name Merge branch 'unify_styles'
|\
| * 821eb6e  (unify_styles) 2015-12-05 user_name へっへっへという表現を復活
* |   8c4a06b  2015-12-05 user_name Merge branch 'hotfix'
|\ \
| * | 395fc0c  2015-12-05 user_name 問題のある表現を修正
|/ /
| * 8525d74  2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

マージコミットができました。

そしたらunify_stylesブランチは用済みなので削除。

$ git branch -d unify_styles
Deleted branch unify_styles (was 821eb6e).

$ git graph
*   c56cd0a  (HEAD -> master) 2015-12-05 user_name Merge branch 'unify_styles'
|\
| * 821eb6e  2015-12-05 user_name へっへっへという表現を復活
* |   8c4a06b  2015-12-05 user_name Merge branch 'hotfix'
|\ \
| * | 395fc0c  2015-12-05 user_name 問題のある表現を修正
|/ /
| * 8525d74  2015-12-05 user_name 文体を統一
|/
* 7240214  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを追加

すごいぐにゃぐにゃしててわけわかんなくなりそ。
ひとまず完成!