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 checkout master
Switched to branch 'master'
λ git merge new
Auto-merging myfile.py
CONFLICT (content): Merge conflict in myfile.py
Automatic merge failed; fix conflicts and then commit the result.
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:
import numpy as np
arr = np.arange(10)
print(arr.mean())
print(arr.std())
<<<<<<< HEAD
a = 10
=======
a = 20
>>>>>>> new
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:
import numpy as np
arr = np.arange(10)
print(arr.mean())
print(arr.std())
a = 10
Then you can treat the remaining steps as a normal file edit: simply add and commit.
λ git add myfile.py
λ git commit -m "Merging with a conflict"
[master df51349] Merging with a conflict
Now the merge is complete. The master
branch is updated.
λ git log --oneline
df51349 (HEAD -> master) Merging with a conflict
9efb6e7 (new) Added variable a
349921c Added variable a
e84601c Added new file
a37231a Revert "Added another file"
35ac407 Added another file
ee7ebbd Print out the std dev
b1e55d3 Print out the mean
c96c526 First commit
Last updated