Git Branch Merge

Git Branch Merge

Merge Branches

We’ll merge the master and emergency-fix branches.

Let’s change the master branch:

[user@localhost] $ git checkout master.

Switched to branch ‘master’

Now we merge the current branch (master) with emergency-fix:

Example:

[user@localhost] $ git merge emergency-fix

Updating 09f4acd..dfa79db

Fast-forward

index.html | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

Since, the emergency-fix branch came emerges directly from the master, and no other changes are made to the master while working on it, Git sees this as a continuation of master. Therefore , it can “Fast-forward”, pointing to master and emergency-fix to the same commit.

As master and emergency-fix are almost similar, emergency-fix can be deleted, as it is no longer required:

Example:

[user@localhost] $ git branch -d emergency-fix

Deleted branch emergency-fix (was dfa79db).

Merge Conflict

Moving over to hello-world-images and keep working. Add another image file (img_hello_git.jpg) and change index.html, so it displays it:

Example:

[user@localhost] $ git checkout hello-world-images

Switched to branch ‘hello-world-images’

Example:

<!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>

Our work is done here and can stage and commit to this branch:

Example:

[user@localhost] $ git add –all

[user@localhost] $ 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

In both the branches index.html has been changed. Now, hello-world-images can be merged into master.

Example:

[user@localhost] $ git checkout master

[user@localhost] $ 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.

The merge failed due to the conflict that occurred between the versions for index.html. Let us check the status:

Example:

[user@localhost] $ 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 …” to mark resolution)

both modified:   index.html

Hence, the conflict in index.html has been confirmed but the image files are ready and stagedto be committed.

Now, this conflict needs to be fixed. Open the file in our editor:

Example:

<!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>

The differences between the versions  can be seen and edited as per requirement

Example:

<!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>

The index.html  can be staged and then let’s check the status:

[user@localhost] $ git add index.html

[user@localhost] $ 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

The conflict has been fixed, and commit can be used to conclude the merge:

Example:

[user@localhost] $ git commit -m “merged with hello-world-images after fixing conflicts”

[master e0b6038] merged with hello-world-images after fixing conflicts

And delete the hello-world-images branch:

Example:

[user@localhost] $ git branch -d hello-world-images

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