How do I make sense of branches and merge conflicts?

November 21, 2026 5 answers
Asked by Kivaro+8n

I use feature branches for an Ansible project, then push changes and pull them onto the Ansible host. Shared files like the inventory and documentation sometimes conflict when I try to merge.

What do ahead and behind counts actually mean, and where does the resolved file end up? I want to understand the process rather than blindly accept a suggested fix.

Answers

Ravlixo+2g

Think of a branch as a movable pointer to a commit. When you commit on your feature branch, that pointer advances. If main gets new commits too, the two branches have diverged.

Compared with main, your branch is ahead by commits reachable from your branch but not main, and behind by commits reachable from main but not your branch. The counts describe history, not how many files conflict or how risky a merge is. Fetch first if you are comparing against a remote tracking branch, otherwise its view of the remote may be stale.

For a quick look, run git fetch origin, then git status and git log --oneline --graph --decorate --all. Check which two branches a hosting interface is comparing, since ahead and behind are always relative to something.

Reply to this answer
5e+bracketmarble_482

A conflict means Git couldn't safely combine some changes on its own. It doesn't mean you have to choose one whole file. You can keep either side, combine them, or write a corrected version.

For a merge, first check git status to see which branch is checked out and which files are unmerged. Open a conflicted file and look for the conflict markers. Edit the section into the content you actually want, remove the markers, and check that the surrounding file still makes sense. Then run git add on that file. Once all conflicts are staged, git status will tell you how to finish the merge, usually with git commit.

The resolved file is in your working tree while you edit it. After you stage and commit it, the resolution is recorded in a merge commit on the branch you had checked out when you started the merge. If you were on the feature branch and merged main into it, the resolution goes on the feature branch.

Nexdaro-6v:

In an editor's conflict view, check the branch and operation before clicking a side. Labels like current and incoming depend on what you are merging and can be confusing during a rebase.

Reply to this answer
5x~gimbalwicket_938

For separate tasks, start each branch from an up to date main rather than accidentally branching one unfinished task from another. Merge finished work back regularly. If another branch has been open while main changed, bring those changes into it before trying the final merge.

Editing the same shared file doesn't automatically cause a conflict, but edits to the same area often do. Coordinate changes to inventory or documentation when possible. You don't have to split every file between people, just make overlapping work visible early.

Kivaro+8n:

So even with one active feature branch, it can fall behind if main changes while that work is in progress?

Mivquaro+8j:

Yes. Fetch the remote updates and compare before merging main into the feature branch. If nobody else has changed main, there may be nothing new to bring in.

Reply to this answer
Vexnaro.2q

Keep the safety steps boring. Commit or otherwise save your work before starting a merge, read git status at each step, and review the resolved diff before committing. For Ansible files, check the YAML and run whatever validation is appropriate before deploying.

If a merge is going badly, git merge --abort can cancel an in-progress merge. It is still worth saving uncommitted work first, since aborting may not restore every pre-merge change. Avoid reaching for git reset --hard as a general conflict fix because it can discard local changes.

Reply to this answer
_k73~fendrix.8

The laptop to remote to host loop is separate from the branching question. You could automate the host update with a script once the Git steps are clear. Just make sure it updates the intended branch and that deployment checks happen before changes are applied.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions