非エンジニアでも知らないとヤバイCPU Part1
そもそもCPUとは CPUとは、Central Processing Unitの略で、日本語では中央演算処理装置と言い、別名はプロッセッサーです。 入力装置などから受けっとった値を処理し、出力装置などで結果を出力します。 CPUの内部 ...
投稿日 : 2018.03.25 | カテゴリー : 非エンジニアでも知らないとヤバイ
今回は競合の解決について説明する。
前回までの記事はこちら
非エンジニアでも知らないとヤバイGit Part1
非エンジニアでも知らないとヤバイGit Part2
リモートリポジトリとローカルリポジトリで同じ箇所を変更していた場合に競合が発生する。
このような場合はどちらの変更を採用するかを自動的に判断することができないためにエラーが発生する。
同じリモートリポジトリを共有している2人のユーザー、User1とUser2を用意する。
まずはUser1でindex.phpにコードを追加する。
Gitを簡単に使用できるGUI、SourceTreeでコミット・プッシュを行う。(コミットメッセージ:おはようございます。を追加)
次に、User1が追加したものと異なるコードを、User2で同じ箇所に追加する。
準備ができたのでターミナルからGitの操作をする。
まず、変更したファイルをステージングに追加する。
git add (ファイル名) git add index.php
次にコミットメッセージを入力してコミットする。
git commit -m 'コミットメッセージ' git commit -m 'Good Morning!を追加'
この状態でプッシュするとこのようなエラーが発生する。
git push
To https://bitbucket.org/username/test.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://username@bitbucket.org/username/test.git' 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 pull
remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From https://bitbucket.org/username/test 4754cf1..f75253f master -> origin/master Auto-merging index.php CONFLICT (content): Merge conflict in index.php Automatic merge failed; fix conflicts and then commit the result.
index.phpで競合が発生してると書かれている。
エディターで競合しているファイルを開くと競合箇所が表示されている。
今回はUser2の変更内容で競合を解決する。
ピンク色のUse meボタンを押して、競合を解決し再び対象のファイルをステージングに追加し、コミットメッセージに競合解決したことがわかるようにメッセージを入力し、プッシュする。
git add index.php git commit -m '競合を解決' git push
User1のSourceTreeでプルをして変更を確認。
User1のローカルリポジトリのindex.php
今回は競合について説明した。
次回はSourceTreeについて説明する。