Skip to content
Git Basics: Resolving a Merge Conflict

Git Basics: Resolving a Merge Conflict

October 9, 2026

When two branches change the same line differently, Git may need you to decide the final contents. This exercise creates a small conflict, resolves it, and records a merge commit.

Run these commands in Bash with Git 2.28 or newer installed. Start a fresh temporary repository for this article:

git_practice=$(mktemp -d) || exit
cd "$git_practice" || exit
git init -q -b main
git config user.name 'Practice User'
git config user.email 'practice@example.invalid'
git config commit.gpgsign false

The identity and signing settings apply only to this repository. Use your own identity in real projects. The temporary files remain afterward. Examples assume default Git settings; if a pager opens, press q to leave it.

Change the same line on two branches

printf 'port=8080\n' > app.conf
git add app.conf
git commit -q -m 'Add application config'
git switch -q -c change-port
printf 'port=9090\n' > app.conf
git add app.conf
git commit -q -m 'Use port 9090'
git switch -q main
printf 'port=8000\n' > app.conf
git add app.conf
git commit -q -m 'Use port 8000'

Both branches now have their own commit after the shared starting point. The working tree is clean before the merge.

Start the merge

git merge change-port
Auto-merging app.conf
CONFLICT (content): Merge conflict in app.conf
Automatic merge failed; fix conflicts and then commit the result.

This command deliberately exits with status 1. Stop here if you see a different error. The merge remains in progress; do not run another merge.

git status --short
cat app.conf
UU app.conf
<<<<<<< HEAD
port=8000
=======
port=9090
>>>>>>> change-port

UU means both branches modified the file and its conflict is unresolved. With the default conflict style, the section above ======= comes from the current branch; the section below comes from the branch being merged.

Choose the final contents

For this practice file, keep port 9090. The next command replaces the whole file, so use it only in this temporary exercise. In a real project, edit each conflict while preserving the rest of the file.

printf 'port=9090\n' > app.conf
git add app.conf
git diff --cached --stat
 app.conf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Remove the conflict markers, review the staged change, and run the project’s tests before committing. git add tells Git that you have resolved this file; it cannot judge whether your chosen result is correct.

git commit -q -m 'Merge change-port and keep port 9090'
cat app.conf
git status --short
port=9090

The status command prints nothing, and the merge is complete. To cancel instead, use git merge --abort while the merge is still in progress. It attempts to restore the pre-merge state; starting with uncommitted work can make that restoration difficult.

Reference

Git merge manual

Acknowledgements

This post was created with assistance from AI.

Last updated on