Gitに入門する -その8-

blog.takanabe.tokyo

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

「ひとりでつかう - 過去を改変」より。


準備

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

$ mkdir my_third_workspace
$ cd my_third_workspace
$ git init

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

$ git add .
$ git commit

$ git graph
* f3f13c1  (HEAD -> master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

①ファイルの編集を行う

unify_stylesブランチを切ってチェックアウト

$ git checkout -b unify_styles
Switched to a new branch 'unify_styles'

ファイルを編集する。
編集が終わった。
ここで別の編集依頼がくる。
とりあえず今の編集箇所をコミットしておく。

$ git graph
* 109bf9a  (HEAD -> unify_styles) 2015-12-05 user_name 作業途中だがとりあえずコミット
* f3f13c1  (master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

別の編集を行うためにhotfixブランチを作って移動。

$ git checkout -b hotfix
Switched to a new branch 'hotfix'

それではファイルを編集して、
コミットして、マージして、
hotfixブランチを削除しよう。

$ git graph
* 3589c9d  (HEAD -> hotfix) 2015-12-05 user_name まずい表現を修正
| * 109bf9a  (unify_styles) 2015-12-05 user_name 作業途中だがとりあえずコミット
|/
* f3f13c1  (master) 2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

$ git checkout master
Switched to branch 'master'

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

$ git branch -d hotfix
Deleted branch hotfix (was 3589c9d).

$ git graph
*   d42bcc7  (HEAD -> master) 2015-12-05 user_name Merge branch 'hotfix'
|\
| * 3589c9d  2015-12-05 user_name まずい表現を修正
|/
| * 109bf9a  (unify_styles) 2015-12-05 user_name 作業途中だがとりあえずコミット
|/
* f3f13c1  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

②リベースする。

$ git checkout unify_styles
Switched to branch 'unify_styles'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: 作業途中だがとりあえずコミット

$ git graph
* e366f69  (HEAD -> unify_styles) 2015-12-05 user_name 作業途中だがとりあえずコミット
*   d42bcc7  (master) 2015-12-05 user_name Merge branch 'hotfix'
|\
| * 3589c9d  2015-12-05 user_name まずい表現を修正
|/
* f3f13c1  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

何が起きたか?
・「作業途中だがとりあえずコミット」の親コミットが「Merge branch 'hotfix'」に変更された。
・ファイルの中身もhotfixで行った作業が反映されている。

もっと細かくいくと、
・unify_stylesを巻き戻す
・masterから新しくブランチを作りなおす
・それぞれの反映を取り込んで新しいコミットオブジェクトを作り出す
ってことであってるのかな?
よくわからん。

③コミットを改変する

「作業途中だがとりあえずコミット」のコミットを改変する。

とりあえずファイルを編集して、

$ git add .
$ git commit --amend

すると、
前回のコミットメッセージがエディタで立ち上がる。
メッセージを変更して終了。
masterにマージしてunify_stylesブランチを削除して終了。

$ git graph
* aa67562  (HEAD -> unify_styles) 2015-12-05 user_name 文体を統一
*   d42bcc7  (master) 2015-12-05 user_name Merge branch 'hotfix'
|\
| * 3589c9d  2015-12-05 user_name まずい表現を修正
|/
* f3f13c1  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成

$ git checkout master
Switched to branch 'master'

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

$ git branch
* master
 unify_styles

$ git branch -d unify_styles
Deleted branch unify_styles (was aa67562).

$ git graph
*   490073c  (HEAD -> master) 2015-12-05 user_name Merge branch 'unify_styles'
|\
| * aa67562  2015-12-05 user_name 文体を統一
|/
*   d42bcc7  2015-12-05 user_name Merge branch 'hotfix'
|\
| * 3589c9d  2015-12-05 user_name まずい表現を修正
|/
* f3f13c1  2015-12-05 user_name cat_hater_said.txtとcat_lover_said.txtを作成