A merge conflict marks a decision Git cannot make about the intended result. Create two different edits to the same setting, read them alongside their common ancestor, and decide what the final file should contain. Verify that result before completing the merge.
To avoid touching existing projects, we will use a new repository called conflict-lab. It runs in Linux Bash or Git Bash, and author information assumes the state set previously.
Git · GitOps All Series · 09 / 24
On this page
1. Create different values from the same starting point
cd ~/git-lab
mkdir conflict-lab
cd conflict-lab
git init -b main
printf 'timeout=30\n' > app.conf
git add app.conf
git commit -m "Set initial timeout"
git switch -c feature/long-timeout
printf 'timeout=60\n' > app.conf
git commit -am "Increase timeout for slow requests"
git switch main
printf 'timeout=45\n' > app.conf
git commit -am "Adjust timeout for the default service"
git merge feature/long-timeout
commit -am stages and commits modifications and deletions of files it already tracks. Since new files are not included automatically, I wrote a separate add in the first commit. It is normal for the final merge to cause a conflict in this example.
Git compares both results to a common ancestor
A typical split history merge uses three states: common ancestor, current branch, and branch to merge. The common ancestor’s timeout=30 changed to 45 on one side and 60 on the other. Even though Git knows the grammar of the value, it cannot know the policy desired by the service.
git status
git diff -- app.conf
git ls-files -u
cat app.conf
The file will be marked with a conflict mark as shown below. If you commit with the two lines between marks left, the configuration itself may be incorrect.
<<<<<<< HEAD
timeout=45
=======
timeout=60
>>>>>>> feature/long-timeout
The HEAD page is the contents of the currently checked out main. In rebase, the meaning of ours and theirs may seem counterintuitive, so you don’t just memorize the “Select Mine” button. Read both the comparison and the actual final file.
2. Write the intended end content
For this exercise, we’ll assume that the default service also decides to spend 60 seconds. This value is not a recommended measure of service performance, but an example for conflict resolution.
printf 'timeout=60\n' > app.conf
git add app.conf
git diff --staged
git diff --staged --check
git merge --continue
git log --oneline --graph --decorate -8
add also indicates that the conflict in this file has been resolved. When the editor opens in merge --continue, check the reason for merging and save. In automated labs where an editor is not available, you can also leave a merge commit as git commit -m "Resolve timeout policy".
In actual applications, we run configuration grammar checks and tests and then share them. The fact that the conflict indicator has been removed is not proof that the program is behaving correctly. Sometimes you need a third result that satisfies the requirements of both changes.
When canceling a merge and preparing again
# Only while a merge is in progress:
git merge --abort
git status
This command does not revert a merge that has already been committed. If there are uncommitted modifications before merging, it may be difficult to completely reconstruct the original state, which is a reason to clean up the work tree before starting.
Overlapping format and function changes, even in different lines of the same file, make review difficult. By splitting large automatic formats and behavior fixes into separate commits or PRs and reducing long open branches, you can keep the scope for conflict small. Detailed operations can be found in the conflict description of git merge, , and the selection direction during rebase can be found in git restore.
Previous · [Git and GitOps 08] Create and Merge Branches: switch and Fast-Forward · Next · [Git and GitOps 10] Work with Remotes: clone, fetch, pull and push · Series list and example files



