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 b4f0fcaacd6fe8b64f939e519f26c2a028944e7b (HEAD -> example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:13 2025 +0300
|
| branch commit
|
* commit 30b953bc10ecfb54458302e0a6a364f676bf3f5c (master)
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:13 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 30b953b..b4f0fca
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 b4f0fcaacd6fe8b64f939e519f26c2a028944e7b (HEAD -> master, example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:13 2025 +0300
|
| branch commit
|
* commit 30b953bc10ecfb54458302e0a6a364f676bf3f5c
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:13 2025 +0300
initial 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 b31fa2476443af49720c088e3a37e6f44008be5d (example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:23 2025 +0300
|
| branch commit
|
| * commit 0fe03ff7b6511b8dd393ba3c3c499982947c41c9 (HEAD -> master)
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:23 2025 +0300
|
| master commit
|
* commit 8023dd35ba8311ebae4c08a09cfca80e507c04ed
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:23 2025 +0300
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 b238dc8b534b63d72ea5ee3d5c03c3dff9bb1a30 (HEAD -> master)
|\ Merge: 0fe03ff b31fa24
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Sat Nov 29 22:58:26 2025 +0300
| |
| | merge commit
| |
| * commit b31fa2476443af49720c088e3a37e6f44008be5d (example_branch)
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Sat Nov 29 22:58:23 2025 +0300
| |
| | branch commit
| |
* | commit 0fe03ff7b6511b8dd393ba3c3c499982947c41c9
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:23 2025 +0300
|
| master commit
|
* commit 8023dd35ba8311ebae4c08a09cfca80e507c04ed
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:23 2025 +0300
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 04b1bc1dcd0640d0af741104cb67b91535c59085 (example_branch)
| Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:31 2025 +0300
|
| commit in example_branch
|
| * commit c2e73a04f3327787c1d2488d835e76bb8ac13731 (HEAD -> master)
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:31 2025 +0300
|
| commit in master
|
* commit a621329c5af98cabda6ed1b4b82776a033d4cae5
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:30 2025 +0300
basic commit
As a result, attempting to merge these branches results in a merging error.
git merge example_branch
git status
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
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 d8dbf56922690b5cb255d9d7fd85bd1930f9586d (HEAD -> master)
|\ Merge: c2e73a0 04b1bc1
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Sat Nov 29 22:58:47 2025 +0300
| |
| | my after merge commit
| |
| * commit 04b1bc1dcd0640d0af741104cb67b91535c59085 (example_branch)
| | Author: fedorkobak <kobfedsur@gmail.com>
| | Date: Sat Nov 29 22:58:31 2025 +0300
| |
| | commit in example_branch
| |
* | commit c2e73a04f3327787c1d2488d835e76bb8ac13731
|/ Author: fedorkobak <kobfedsur@gmail.com>
| Date: Sat Nov 29 22:58:31 2025 +0300
|
| commit in master
|
* commit a621329c5af98cabda6ed1b4b82776a033d4cae5
Author: fedorkobak <kobfedsur@gmail.com>
Date: Sat Nov 29 22:58:30 2025 +0300
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 content" > 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 content
=======
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 content
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.