Gitチュートリアル


Gitと{{title}}


Gitの貢献


Git Advanced


Git Undo




Gitブランチマージ


ブランチをマージする

緊急修正の準備ができたので、マスターブランチと緊急修正ブランチをマージしましょう。

まず、マスターブランチに変更する必要があります。

git checkout master
Switched to branch 'master'

次に、現在のブランチ(マスター)をemergency-fixとマージします。

git merge emergency-fix
Updating 09f4acd..dfa79db
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

緊急修正ブランチはマスターから直接取得され、作業中にマスターに他の変更が加えられていないため、Gitはこれをマスターの継続と見なします。したがって、マスターと緊急修正の両方を同じコミットに向けるだけで、「早送り」できます。

masterとemergency-fixは基本的に同じであるため、emergency-fixは不要になったため、削除できます。

git branch -d emergency-fix
Deleted branch emergency-fix (was dfa79db).

競合のマージ

これで、hello-world-imagesに移動して作業を続けることができます。別の画像ファイル(img_hello_git.jpg)を追加し、index.htmlを変更して、次のように表示します。

git checkout hello-world-images
Switched to branch 'hello-world-images'

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>

</body>
</html>

これで、ここでの作業が完了し、このブランチをステージングしてコミットできます。

git add --all
git commit -m "added new image"
[hello-world-images 1f1584e] added new image
 2 files changed, 1 insertion(+)
 create mode 100644 img_hello_git.jpg

index.htmlが両方のブランチで変更されていることがわかります。これで、hello-world-imagesをマスターにマージする準備が整いました。しかし、最近マスターで行った変更はどうなりますか?

git checkout master
git merge hello-world-images
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

index.htmlのバージョン間に競合があるため、マージは失敗しました。ステータスを確認しましょう:

git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg

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

これは、index.htmlに競合があることを確認しますが、イメージファイルは準備ができており、コミットするためにステージングされています。

したがって、その競合を修正する必要があります。エディターでファイルを開きます。

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<<<<<<< HEAD
<p>This line is here to show how merging works.</p>
=======
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>
>>>>>>> hello-world-images

</body>
</html>

バージョン間の違いを確認し、必要に応じて編集できます。

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>This line is here to show how merging works.</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>

</body>
</html>

これで、index.htmlをステージングして、ステータスを確認できます。

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

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg
        modified:   index.html

競合は修正され、commitを使用してマージを完了することができます。

git commit -m "merged with hello-world-images after fixing conflicts"
[master e0b6038] merged with hello-world-images after fixing conflicts

そして、hello-world-imagesブランチを削除します。

git branch -d hello-world-images
Deleted branch hello-world-images (was 1f1584e).

これで、ブランチとマージがどのように機能するかをよりよく理解できました。リモートリポジトリでの作業を開始する時が来ました!

エクササイズで自分をテストする

エクササイズ:

hello-youブランチを現在のブランチとマージします。

git  hello-you