Resolve conflicts

Let's demonstrate a merge with conflicts: we will add a variable to myfile.py in both branches, but to different values.

On branch master:

λ echo a = 10 >> myfile.py

λ git commit -m "Added variable a"
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   myfile.py

no changes added to commit (use "git add" and/or "git commit -a")

λ git add myfile.py

λ git commit -m "Added variable a"
[master 349921c] Added variable a
 1 file changed, 1 insertion(+)

On branchnew:

λ git checkout newh
Switched to branch 'new'

λ echo a = 20 >> myfile.py

λ git add myfile.py

λ git commit -m "Added variable a"
[new 9efb6e7] Added variable a
 1 file changed, 1 insertion(+)

Now back to master and start merging

Git tells you that there's a merge conflict in myfile.py. What you need to do is to open the file with conflicts and edit it. Here's what myfile.py looks like now:

Notice Git puts the markers (<<<, ===, >>>) around the conflict area, showing two contradicting sources. You simply need to remove one version, along with all the markers. I.e. change the file to the desired final version:

Then you can treat the remaining steps as a normal file edit: simply add and commit.

Now the merge is complete. The master branch is updated.

Last updated