Tools & practice / CHIEFLETTER JOURNAL
Resolve Git Merge Conflicts Without Losing Work
A safe, repeatable method for understanding Git conflict markers, choosing the right changes, and checking the result before you commit.
Pause and find out what Git is combining
A conflict means Git cannot choose how to combine two edits to the same area. It does not mean your work is gone. First check the command you ran and the branch you are on. Run `git status` and read the list of conflicted files. Do not start deleting markers until you understand which two changes are involved.
If the conflict appeared during a merge, Git is combining your current branch with the branch named in the merge command. During a rebase, it is applying your commits one at a time on top of another branch. The labels Git shows differ by operation, so confirm which side represents your work before you choose.
A conflict in a generated file needs a different response from a conflict in source code. Check whether the project expects you to edit the generated output or rebuild it from source. If a lockfile conflicts, use the package manager to create a valid file after resolving the source dependency choices. Do not hand-edit a large generated file without a clear reason.
Protect the work you already have
Save any uncommitted changes before starting another merge or rebase. If you are unsure whether a change is committed, inspect `git status` and `git diff`. Copy important uncommitted work to a safe patch or commit it on a branch. Avoid running reset commands from a forum without reading what they remove.
If you started a merge and want to return to the state before it, `git merge --abort` often provides a clean exit. During a rebase, `git rebase --abort` returns to the earlier state. These commands are useful only when you understand the operation and have saved unrelated work. Check status afterward to confirm what happened.
Look at the commits around the conflict with `git log --oneline --graph`. Read the commit messages and, when needed, inspect each patch. A two-line conflict may represent two larger decisions. The history shows why the lines changed and helps you avoid keeping text whose feature was later removed.
Read the conflict markers as boundaries
Open one conflicted file. A conflict block starts with `<<<<<<<`, separates the two versions at `=======`, and ends at `>>>>>>>`. The first version is commonly the current branch and the second is the incoming change, but rebase labels can feel reversed. Read nearby code and the commit history instead of trusting a label at a glance.
The right resolution may keep the first version, keep the second, combine both, or replace the block with a new implementation. Remove every marker after making that decision. Do not accept both versions automatically. Two individually valid edits can produce duplicate checks, repeated list items, or logic that contradicts itself.
For a conflict in a test, compare the behavior each branch expects. Keep or write a test for the combined behavior, then run it. A test conflict often signals that the product requirements changed in two places. Resolving the text without agreeing on the expected behavior leaves the same disagreement for a later release.
If both branches change a test, resolve the code and keep the new behavior from each branch. Run the focused test before continuing.
Use the feature’s behavior to choose
Ask what each change was meant to do. If one branch added input validation and the other changed the form layout, a correct result might preserve both. If both changed the same calculation for different reasons, look at the issue or tests that prompted each change. A conflict is a small design review, not a contest to keep the newest-looking text.
For a large conflict, use your editor’s three-way merge view. It shows the common starting version as well as both edits. That context helps you see whether a line was moved, replaced, or added. Talk with the author when the intent is unclear. Ten minutes of conversation is cheaper than silently shipping a behavior neither person intended.
If you need help, send the branch names, the command you ran, the conflicted file, and the behavior you want to preserve. Do not paste an entire repository diff into chat. Share the smallest relevant block and ask the author of a change when intent is unclear.
Check the result before completing the operation
After editing, inspect the file for leftover markers, duplicated code, formatting problems, and missing imports. Run `git diff` and read the whole changed section. Then run the focused test for that behavior. If the conflict touched a shared component, check one nearby use as well. A successful merge command only means Git accepted the files, not that the program behaves correctly.
Stage only the files you resolved, review the staged diff, and finish the merge or continue the rebase using the instructions Git printed. Check `git status` once more. If you are working in a shared repository, push and open review through the normal team process. Explain any meaningful decision in the commit or pull request.
When resolving a rebase, finish one paused commit at a time. Resolve the current files, run a focused check, stage the result, and continue with the command Git suggests. Later commits might touch the same lines and pause again. Do not assume the first conflict was the only one in the branch.
A short checklist for your next conflict
Before editing, identify the operation and save your own work. For each file, read both versions and the surrounding code. Decide what the feature needs, remove the markers, then inspect the complete diff. Run a focused check. Ask the author when the intent is uncertain.
If you resolve conflicts often, reduce avoidable overlap. Keep branches focused, integrate shared changes regularly, and tell teammates when files with overlapping work are changing. This does not prevent every conflict. It makes the conflicts smaller and gives everyone more context when one appears.
A merge commit records the two lines of history coming together. A rebase replays commits and changes their identifiers. Follow the repository’s normal policy. If the branch is shared, ask before rewriting its history. After a rebase, check the branch and remote carefully before pushing, since a normal push may be rejected for good reason.
A short working note
Try this with your work
A safe, repeatable method for understanding Git conflict markers, choosing the right changes, and checking the result before you commit.
Problem note
Capture a useful technical observation.
Video resources
For a visual walkthrough related to this topic, see this YouTube learning resource. It opens a topic search so you can choose a current explanation that fits your level.
Frequently asked questions
Does a merge conflict delete my changes?
Git keeps the conflicting versions in the file and pauses the operation. Save your work and inspect status before editing.
What do the Git conflict markers mean?
They mark the competing text sections and the boundary between them. Review both versions and choose or combine the behavior you need.
Should I always keep the incoming change?
No. The right result depends on what each change was meant to do. Read the code, tests, and issue context.
How do I cancel a merge conflict?
For a merge, `git merge --abort` often returns to the earlier state. For a rebase, use `git rebase --abort`. Check status and save work first.