My Brain
My Brain

Merge

The merge command can sometimes confuse students early on. Remember these two merging concepts:

  • We merge branches, not commits.
  • We always merge to the current HEAD branch.

To merge, follow these two steps:

  1. Switch to or checkout the branch you want to merge the changes into (the receiving branch)
  2. Use the git merge command to merge changes from a specific branch into the current branch.

To merge the bugfix branch into master:

1

  • Switch to the master branch
git switch master

2

(see how head moved!)

  • And merge bugfix into master
git merge bugfix

3

This is called a Fast-Forward Merge

In other words, master just caught up on bugfix

(See how HEAD moved again!)

But not all merges are Fast-Forward

.

This is what happens when we (or a colleague) committed on master:

4

This is an extremely common situation!

Then you try to merge, but master cannot perform a fast-forward merge

5

We end up with a merge commit and git will ask you to enter a commit message:

6

Merge Conflicts

Depending on the specific changes your are trying to merge, Git may not be able to automatically perform the merging. This results in merge conflicts, which you need to manually resolve.

When git encounters a merge conflict, it warns you in the console that it could not merge automatically.

It also changes the contents of your files to indicate the conflicts that it wants you to resolve.

   <<<<<<< HEAD
   This is is code
   in the branch RECEIVING
   =======
   This is conflicting code
   from the branch I'm trying to merge
   >>>>>>> bugfix

Code contained between <<<<<<< HEAD and ======= belongs to the receiving branchit.

Code between ======= and >>>>>>> <branch-name> belongs to the merging branch

Resolving Conflicts

Whenever you encounter merge conflicts, follow these steps to resolve them:

  • Open the file or files with conflicts
  • Edit the file(s) to remove the conflicts. Decide which branch's content you want to keep. Or even keep the content from both branches.
  • Remove all conflict markers (<<<<<<< HEAD - ======= and >>>>>>> <branch-name>)
  • Save the file(s), add it (them) (git add <file(s)>) and make a commit.

Deleting Branches

To delete the branch bugfix:

7

git branch -d bugfix

8

Backlinks