Git Kata 3: Merging

Learn about merging branches with divergent histories.

Merging is essentially the opposite of branching. Branches allow changes in a repository to be separated. Merging is the process of combining work that’s separated between branches.

Step 1: Merge changes between branches

The command to merge changes is given below.

Press + to interact
git merge newbranch

The output will be something like this:

Command's Parameters

Command / Parameter

Description

merge

This applies the changes from one branch to another branch.

newbranch

This is the branch from which we take commits and apply them to the current branch.

The git merge command replays the changes from another branch on the current branch. Git will attempt to combine the commits from the source branch to the current branch. The result of the merge operation depends on the differences (if there are any) between the branches. The output of this command is:

Output

Message

Meaning

"Updating c70b36e..1e19013"

This is the abbreviated source and the target commit hashes.

"Fast-forward"

This is the merge strategy used to merge the source and target branches.

"storelist.txt | 4++--"

These are the files affected by the merge and the lines that were modified.

"1 file changed, 2 insertions(+), 2 deletions(-)"

This is a summary of the files changed and the modifications made.

The previous step made a modification to storelist.txt in newbranch. The git merge newbranch command instructs Git to take the changes from newbranch and apply them to the current branch.

Git has several methods, called merge strategies, it can use to determine how to merge changes. This execution of Git merge uses the fast-forward strategy.

The main branch had no commits on it since the newbranch commit was added. Git was able to merge them efficiently by simply moving the HEAD and main refs to point to the latest commit in the commit history. When Git executes a fast-forward merge, only the refs are updated, so no new commits are necessary. Git merged the changes by updating the HEAD and main refs to point to the commit in newbranch. The next step will demonstrate merging where different versions of the same file ...