この記事は3年以上前に書かれた記事で内容が古い可能性があります
Gitの基本操作_その2
2018-08-05
Gitの基本操作の続き
前提
Githubは複数名で開発ができるツールであり、Branchというそれぞれのリポジトリを持つことができる
それぞれがそれぞれのBranchで開発を進めて、よきところで合体させるというイメージ
ただし、今回は、他の人(someone)が同じファイルを編集していて、うまいこと合体(merge)できなかったので、
fetchとmerge toolを使ってうまいことmergeさせる、というシナリオもやる

ブランチを作る
何はともあれ、まずはbranchを作る
名前はyoshiとする
remotes/origin/HEAD -> origin/master |
変更してpushしたりするとremote repositoryの方でもbranchができたのが確認できる

選択すると、yoshi branchの変更点が反映されている

pullリクエストとmaster branchへのmerge
yoshi branchで変更した内容をmaster branchへmergeさせる
Githubの画面で操作となる
yoshi branchの画面で「Compare & pull request」をクリック

適当に変更についてのコメントをつけつつ、「Create pull request」をクリック

Pull requestのレビュー画面へ飛ぶ
本当はここでレビュアーがやんやコメントを書きつつ、修正しつつ、やんややんや議論をする
そして良ければ、「Merge pull request」



これでMaster branchにyoshi branchの変更内容がMergeされる

変更してpush失敗まで
そしてここからは、自分が編集しているうちに他の人がMaster branchを変更して、自分の変更がpushできないケース
変更差分を作る
% echo "yoshi change2" >> README.md |
% git commit -m "yoshi change2" |
[yoshi 8d6f7a7] yoshi change2 |
1 file changed, 1 insertion(+) |
git pushしようとすると失敗する
! [rejected] master -> master (fetch first) |
hint: Updates were rejected because the remote contains work that you do |
hint: not have locally. This is usually caused by another repository pushing |
hint: to the same ref. You may want to first integrate the remote changes |
hint: (e.g., 'git pull ...' ) before pushing again. |
hint: See the 'Note about fast-forwards' in 'git push --help' for details. |
git fetch
こんな時は、一度fetchを使って、他の人の変更差分をダウンロードする
branchはyoshiのまま
% git fetch origin master |
remote: Counting objects: 4, done . |
remote: Compressing objects: 100% (2/2), done . |
remote: Total 4 (delta 0), reused 3 (delta 0), pack-reused 0 |
Unpacking objects: 100% (4/4), done . |
* branch master -> FETCH_HEAD |
04cccee..44215a2 master -> origin/master |
merge toolで変更編集
このままでは、mergeできないので、どのように合体させるか編集させる必要がある
% git merge origin master |
CONFLICT (content): Merge conflict in README.md |
Automatic merge failed; fix conflicts and then commit the result. |
色々やり方があると思うが、git merge toolを使う
※前提としてvimが使えるようになっている必要がある
あと、以下の設定も入れておく
% git config --global merge.tool vimdiff |
「git mergetool」と入れるとvimの画面が立ち上がる

基本的に一番下の画面を、変更したい内容に変更して、「:wq」で保存終了

Normal merge conflict for 'README.md' : |
これで変更がlocal repositoryに反映される
ここまでくればremote repositoryにpushできる
All conflicts fixed but you are still merging. |
(use "git commit" to conclude merge) |
(use "git add <file>..." to include in what will be committed) |
% git commit -m "yoshi change2 and merge" |
Counting objects: 6, done . |
Delta compression using up to 4 threads. |
Compressing objects: 100% (3/3), done . |
Writing objects: 100% (6/6), 548 bytes | 548.00 KiB/s, done . |
Total 6 (delta 0), reused 0 (delta 0) |
* [new branch] yoshi -> yoshi |

