Merge#
Branches merge is opertion that allows you to get changes from intependend branches in one common commit. The git merge command is crucial for this page.
To merge branches you need to git checkout to brunch to which we merge all the changes, and execute command git merge <branch to be merged>.
Fast-forward#
The simplest case is when you create a branch, make a few commits, and want to add those changes from an ancestor branch that hasn’t changed.
Simply put, you just tell the parent branch to just start referencing the last commit of the branch you want to merge into the parent branch.
Consider the corresponding example.
The following cell illustrates a scenario in which an “example branch” that is created from the “initial commit” in the main branch.
%init
echo "content" > test_file
git add --all
git commit -m "initial commit" &> /dev/null
git checkout -b example_branch &> /dev/null
echo "content2" > test_file
git commit -am "branch commit" &> /dev/null
git log --decorate --graph
* commit d8c45de49e1910b95336a842d0371da6b346f9ba (HEAD -> example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Thu Oct 30 23:30:30 2025 +0300
|
| branch commit
|
* commit a725eb28b0d67d4e49130dadbe73c8ec0314e69b (master)
Author: fedorkobak <kobfedsur@gmail.com>
Date: Thu Oct 30 23:30:30 2025 +0300
initial commit
The following cell checks out to the “main” branch and merges the “example_branch” into it.
git checkout master &> /dev/null
git merge example_branch
Updating 1956595..8659f34
Fast-forward
test_file | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
As the result the commit tree looks like presented in the following cell.
git log --decorate --graph
* commit 8659f342f775f3461847c9e910d34b10dd81fb81 (HEAD -> master, example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Thu Oct 30 23:27:18 2025 +0300
|
| branch commit
|
* commit 19565951c5cb967ddabf39037159787bda7016b9
Author: fedorkobak <kobfedsur@gmail.com>
Date: Thu Oct 30 23:27:16 2025 +0300
basic commit
The master is “pulled” to the example_branch.
Three-Way Merge#
In the case when both branches you intend to merge have diverged (i.e., both have new commits relative to their closest common ancestor), the merging process will result in Git performing a three-way merge. The system will then automatically create a merge commit that “joins” the two separate lines of development.
The following cell generates a Git history corresponding to the case in question.
%init
echo "content" > file
git add --all
git commit -m "basic commit" &> /dev/null
git checkout -b example_branch &> /dev/null
echo "content" > branch_file
git add --all
git commit -m "branch commit" &> /dev/null
git checkout master &> /dev/null
echo "master content" > file
git commit -am "master commit" &> /dev/null
git log --decorate --graph --all
* commit 46267d0192969dc0c68cb43a1753f703c63109b1 (example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:23:08 2025 +0100
|
| branch commit
|
| * commit f36877029ce3bc142a92bb32f620573914c2787d (HEAD -> master)
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:23:08 2025 +0100
|
| master commit
|
* commit 3234248174eb54c295070946178d1442866abd5e
Author: fedorkobak <kobfedsur@gmail.com>
Date: Fri Oct 31 07:23:08 2025 +0100
basic commit
The next cell merges the example_branch with the main and shows the corresponding output.
git merge example_branch -m "merge commit"
git log --decorate --graph --all
Merge made by the 'ort' strategy.
branch_file | 1 +
1 file changed, 1 insertion(+)
create mode 100644 branch_file
* commit 2065d26a68bfb1ea451877a7460b8f249320915d (HEAD -> master)
|\ Merge: f368770 46267d0
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Fri Oct 31 07:23:24 2025 +0100
| |
| | merge commit
| |
| * commit 46267d0192969dc0c68cb43a1753f703c63109b1 (example_branch)
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Fri Oct 31 07:23:08 2025 +0100
| |
| | branch commit
| |
* | commit f36877029ce3bc142a92bb32f620573914c2787d
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:23:08 2025 +0100
|
| master commit
|
* commit 3234248174eb54c295070946178d1442866abd5e
Author: fedorkobak <kobfedsur@gmail.com>
Date: Fri Oct 31 07:23:08 2025 +0100
basic commit
Solve conflict#
The same file may have been modified in the both commits that you are attempting to merge. This situation is called a merge conflict. The conflicting file will contain the content of both branches in the following format:
<<<<<<< HEAD
<content of the branch we are merging into>
===========
<content of the branch we merge into another>
>>>>>>> <branch ot be merged name>
In this case, you must create the versions of all conflicting files and fix them in a merge commit.
To apply the changes from one of the branches use git checkout --thers/ours <file_name>.
The following cell creates a git history in which the two branches intend to modify the file in a different ways.
%init
echo "content for basic commit" > file
git add --all
git commit -m "basic commit" &> /dev/null
git checkout -b example_branch &> /dev/null
echo "content for example branch" > file
git commit -am "commit in example_branch" &> /dev/null
git checkout master &> /dev/null
echo "content for master branch" > file
git commit -am "commit in master" &> /dev/null
git log --decorate --graph --all
* commit 73095508558ded1759bc13a3f8afdc4f37b99f85 (example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:32:43 2025 +0100
|
| commit in example_branch
|
| * commit 95d59aa007c0ecf98bc14df8bf6974491d51adf0 (HEAD -> master)
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:32:43 2025 +0100
|
| commit in master
|
* commit 80f3a21ebda928f420f04329140812fe8c5350a6
Author: fedorkobak <kobfedsur@gmail.com>
Date: Fri Oct 31 07:32:43 2025 +0100
basic commit
As a result, attempting to merge these branches results in a merging error.
git merge example_branch
git status
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: file
no changes added to commit (use "git add" and/or "git commit -a")
Git offers to set the correct state of the file, add it and then commit.
Consider how the current appearance of the file in the working directory:
cat file
<<<<<<< HEAD
content for master branch
=======
content for example branch
>>>>>>> example_branch
The options from both branches are highlighted there.
The following cell sets the content of the file, commits its current state displays the resulting history.
echo "after merge content" > file
git add file
git commit -am "my after merge commit" &> /dev/null
git log --all --decorate --graph
* commit 7b5848d65237c67d74df095c6466e434c2782ecb (HEAD -> master)
|\ Merge: 95d59aa 7309550
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Fri Oct 31 07:33:42 2025 +0100
| |
| | my after merge commit
| |
| * commit 73095508558ded1759bc13a3f8afdc4f37b99f85 (example_branch)
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Fri Oct 31 07:32:43 2025 +0100
| |
| | commit in example_branch
| |
* | commit 95d59aa007c0ecf98bc14df8bf6974491d51adf0
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Fri Oct 31 07:32:43 2025 +0100
|
| commit in master
|
* commit 80f3a21ebda928f420f04329140812fe8c5350a6
Author: fedorkobak <kobfedsur@gmail.com>
Date: Fri Oct 31 07:32:43 2025 +0100
basic commit
Select version#
If your repository is in a merge conflict state, you can select the changes that you want to keep:
Use
git checkout --ours <file>to take the version of the branch you are currently on (the destination branch, i.e., the branch to which you intended to merge).Use
git checkout --theirs <file>to take the version of the branch you are merging into the current one (the source branch, i.e., the branch from which you intended to merge).
The following cell creates the repository that will contain the merging conflict between the main and new_branch over file.
%init
echo "initial content" > file
git add --all
git commit -m "initial commit" &> /dev/null
git checkout -b new_branch &> /dev/null
echo "new_branch content" > file
git commit -am "commit from new_branch" &> /dev/null
git checkout master &> /dev/null
echo "main contetn" > file
git commit -am "commit from master" &> /dev/null
The results of the merging attempt are shown in the following cell.
git merge new_branch &> /dev/null
cat file
<<<<<<< HEAD
main contetn
=======
new_branch content
>>>>>>> new_branch
The following cell displays the output of the git checkout --ours file command.
git checkout --ours file &> /dev/null
cat file
main contetn
The file takes main content.
The following cell shows --theirs option.
git checkout --theirs file &> /dev/null
cat file
new_branch content
The file contains the content that corresponding to the new_branch state.