Overview
Two people edited the same lines of ingest.py. git pull brings their commit in and stops. You edit the markers out, git add, then git commit.
On this page8 sections
The broken result
You run git pull to get your teammate's latest work. Git prints a few lines and stops halfway.
$ git pull
Auto-merging ingest.py
CONFLICT (content): Merge conflict in ingest.py
Automatic merge failed; fix conflicts and then commit the result.Open ingest.py and the file looks broken. Git has written both versions of the clashing lines into it, with marker lines around them.
Four words for this lesson.
| Word | What it means |
|---|---|
| Conflict | Two people changed the same lines in different ways, and Git cannot choose. |
| Merge | Joining the changes of two branches into one. |
| Marker | The special lines Git writes into the file to show the clash. |
| HEAD | The commit you are standing on right now. In a conflict it labels your side. |
- The line with seven < signs starts the clash. Below it is your version.
- The line of seven = signs divides the two versions.
- Below it is the version that came in from the other branch. The line with seven > signs ends the clash.
If you run the file like this, Python stops with a SyntaxError on the first marker.
Why this failure matters
You and your teammate both started from the same version, then each changed line 12 of ingest.py. You wanted the date column to be order_date. They wanted created_at.
Git can see that the two edits collide. It cannot know which one is right for your pipeline.
A is the last commit you both had. B is their change and C is yours. M is the merge Git is trying to make. A conflict only happens at M, and only when B and C touched the same lines.
Git compares both sides with the shared starting point. Only the last row needs a person.
| Line 12 at the start | Their branch | Your branch | What Git does |
|---|---|---|---|
| order_date | order_date | order_date | Nobody changed it. Keep it. |
| order_date | created_at | order_date | Only they changed it. Take theirs. |
| order_date | created_at | order_date_utc | Both changed it differently. Conflict. |
So a conflict is not an error, and nobody did anything wrong. Git is asking a question it cannot answer.
The damage comes from answering badly. If you commit with the markers still in the file, the pipeline ships with <<<<<<< in the middle of the code and fails. If you keep only your side without reading theirs, you silently erase a teammate's work.
They sweep up every file, including ones you have not opened. During a conflict, add only the file you fixed, after you have read it. Never commit while a marker is still in the file.
How to diagnose
Start with git status. During a conflict it lists the files that still need a decision.
$ git status
On branch main
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: ingest.pyboth modified means both branches changed the file. The git add hint matters: adding the file is how you tell Git you have dealt with it.
If you get lost, git merge --abort puts everything back the way it was before the pull. You can start again without damage.
You can also search for leftover markers yourself. On a real project that is the first command below.
$ grep -rn "<<<<<<<" .
./ingest.py:2:<<<<<<< HEADThe Python below does the same on a list of lines. It counts how many conflicts are in a file by counting the lines that start with seven < signs.
conflicted = """def load(path):
<<<<<<< HEAD
date_col = "order_date"
=======
date_col = "created_at"
>>>>>>> origin/main
return read(path, date_col)"""
def count_conflicts(lines):
return sum(1 for line in lines if line.startswith("<<<<<<<"))
print(count_conflicts(conflicted.splitlines()))How many conflicts do you expect? Look at the file above and count the blocks.
1
One block, so one conflict. When you have fixed the file properly, the count must be 0.
The fix
Fixing a conflict takes four moves. Each one sets up the next.
- git pulltheir work comes in, stops
- Edit the fileremove markers, decide
- git add ingest.pysay it is settled
- git commitrecord the merge
The four steps, and why the order matters.
| Step | Why it sits here | What goes wrong if you swap it |
|---|---|---|
| git pull | You cannot merge what you have not fetched. | Pushing first gets rejected, or overwrites their work. |
| Edit the file | Git needs one valid file to record. | Keeping both sides blindly gives a syntax error. |
| git add ingest.py | Marks the conflict as settled. | Committing first fails: Git says unmerged paths remain. |
| git commit | Records the merge. | Committing with a marker left in records a broken file. |
Here is the edit. First, the file with markers.
def load(path):
<<<<<<< HEAD
date_col = "order_date"
=======
date_col = "created_at"
>>>>>>> origin/main
return read(path, date_col)After the edit, one version is kept and every marker line is gone.
def load(path):
date_col = "created_at"
return read(path, date_col)Here the two sides disagree about which date column to read. That is a question about the business, not about typing. If you pick wrong, the daily orders report counts the wrong day. Ask the teammate.
Sometimes you need both sides. If you changed one filter line and your teammate added another next to it, delete only the three marker lines and keep both. Then check that the result still makes sense.
When the file is right, tell Git and record the merge:
$ git add ingest.py
$ git commit -m "merge main: read created_at for order dates"
[main 8d41b07] merge main: read created_at for order datesRun git status, then search for <<<<<<<. If either one still points at the file, you are not done.
Conflicts happen to everyone. They are rarer if you pull often, commit small changes, and do not let two people sit on the same file for a week.
When a reviewer writes "please fix conflicts", it means these four steps. It never means pushing over your teammate's commit with --force.
Going further: merge or rebase?
There are two ways to bring branches together. A merge makes one extra commit that joins both histories. A rebase takes your commits and replays them on top of the latest main, giving one straight line of history.
$ git switch feature/order-metrics
$ git fetch origin
$ git rebase origin/mainIf a rebase hits a conflict, you fix the file, run git add, then git rebase --continue. The safe rule: only rebase your own private branch. Never rebase commits other people have already pulled.
Before you open a pull request, git rebase -i HEAD~4 lets you tidy up messy commits such as wip or fix typo. For each commit you choose what to do:
- pick: keep it as it is.
- squash: merge it into the commit before it, and join the messages.
- fixup: merge it into the commit before it, and drop its message.
- reword: keep it, but change its message.
- drop: throw it away.
Common beginner questions
Why does Git not just pick the newer change?
Newer does not mean right. Both edits might be needed, or one might break the other. Git compares text, not meaning, so it hands the decision to you.
What if I make it worse halfway through?
Before you commit, run git merge --abort. It returns your branch to how it was before the pull. After you have committed, git revert can cancel the merge with a new commit.
Can I avoid conflicts completely?
No, but you can make them rarer. Pull before you start, commit small changes, and split big files so people rarely edit the same lines.
git pull brings in a teammate's work and stops with a CONFLICT in ingest.py.
- git pull
- CONFLICT
Both branches changed line 12 in different ways. Git cannot know which one is right.
- same line
- two answers
Git writes both versions into the file between the marker lines, so the file is no longer valid code.
- markers
- broken file
git status lists the file as both modified. Searching for the seven-sign marker finds any that are left.
- git status
- search markers
Edit the file until it is normal code, then git add, then git commit.
- edit
- add
- commit
Committing too early records the markers as code. If you get lost, git merge --abort starts you over.
- order matters
- merge --abort
What comes next
You can now save your work and join it with someone else's. Next we go from Git to the command line itself: how the shell finds a program, how to chain commands, and what an exit code tells a script.
Practice
You are given a file as a list of lines, with conflict markers in it.
Write count_conflicts(lines) to count how many conflicts you must resolve. Store the number in result and print it.
Going further: reading a conflict, backing out, and choosing a side
A conflict is git saying 'both of you changed the same lines and I will not guess'. It is not an error in your work. You can look at it, back out of the merge completely, or pick one side. Predict what the file looks like in the middle of a conflict.
git init -q
printf 'timeout=10\nretries=3\n' > config.txt && git add config.txt && git commit -qm "base"
git switch -q -c feature
printf 'timeout=60\nretries=3\n' > config.txt && git commit -qam "feature: timeout 60"
git switch -q main
printf 'timeout=30\nretries=3\n' > config.txt && git commit -qam "main: timeout 30"
git merge feature 2>&1 | tail -2
cat config.txt
git merge --abort
git status --short
echo "-- after abort: $(head -1 config.txt)"
git merge -q feature 2>&1 | tail -1
git checkout --theirs config.txt
git add config.txt
git commit -qm "merge feature, keep timeout 60"
cat config.txtCONFLICT (content): Merge conflict in config.txt Automatic merge failed; fix conflicts and then commit the result. <<<<<<< HEAD timeout=30 ======= timeout=60 >>>>>>> feature retries=3 -- after abort: timeout=30 Automatic merge failed; fix conflicts and then commit the result. Updated 1 path from the index timeout=60 retries=3
Between <<<<<<< and ======= is our side (main, timeout=30). Between ======= and >>>>>>> is the incoming side (feature, timeout=60). The line retries=3 was changed by neither, so it is outside the markers. git merge --abort put everything back, and the status is clean.
The second attempt was resolved by taking the incoming side with --theirs, then git add and commit finished the merge.
| Option | Means | Careful |
|---|---|---|
| --ours | The branch you are on | In a rebase the meaning flips, so check which side is which |
| --theirs | The branch you are merging in | Same warning |
| Edit the file by hand | Keep parts of both | Remove every marker line, then run the tests |
| git merge --abort | Forget the merge attempt | Safe: you lose nothing that was committed |
Two practical rules. Always run the tests after resolving, because a file can merge cleanly and still be wrong. And for generated files such as lock files, do not edit the markers by hand: take one side, then regenerate the file with the tool that owns it.
Try a new scenario
You and a teammate both changed the same SQL file. After you resolve the conflict, the file looks fine but the query returns different numbers. What happened and what do you do?
Work it out, then open the answer
A conflict resolution is a code change nobody reviewed. You may have kept your filter and their column rename but lost one dependent change. Re-run the query and compare it with the result on main and on the teammate's branch, and ask the teammate to look at the resolved file. Resolving the markers is not the same as being correct.
Why do conflicts happen?
Git can combine changes automatically when they touch different lines. When two branches change the same lines differently, it cannot tell which is right, so it stops and asks you.
Close the lesson and explain it
Without scrolling up, explain how to read the markers in a conflict and how to back out of a merge.
Practicals · load into the editor
After you read the theory, run these in the pane on the right. They execute in this tab, no cluster.
Finished this lesson?
Mark it as read when you could explain it to someone else.