Conflict case
- Git course
- Intermediate
- Lesson 35/82
- 4 min
Learn to resolve a conflict during cherry-pick.
What is a conflict?
If two commits changed the same line, cherry-pick cannot know which version should win; this is called a conflict. Marks like <<<<<<< put the two versions side by side in the file, and the operation waits.
<<<<<<< HEAD
v3
=======
v2
>>>>>>> surumNow it is your turn to decide.
Resolve and continue
To resolve the conflict you edit the file, keep the content you want and delete the extra marks. Then you mark the file with git add and finish the operation with git cherry-pick --continue.
git add dosya.txt
git cherry-pick --continueThe operation ends this way and the commit is created.
Practice Resolve the conflict and continue
Create dosya.txt with v1 inside, add it and save quietly. Open a branch named surum; set the content to v2 and save quietly, then go back to the previous branch, set it to v3 and save quietly. Try to take the commit from the surum branch with git cherry-pick; when the conflict appears resolve the file as v2, add it again and finish with --continue. Print the contents of dosya.txt at the end.
Starter code
git init -q
# 1) dosya.txt olustur, icine v1 yaz, ekle ve sessizce kaydet
# 2) surum adinda yeni bir dal ac
# 3) dosya.txt icine v2 yaz, ekle ve sessizce kaydet
# 4) onceki dala don
# 5) dosya.txt icine v3 yaz, ekle ve sessizce kaydet
# 6) surum dalindaki commit'i cherry-pick ile dene
# 7) catisma cikinca dosyayi v2 olarak coz ve tekrar ekle
# 8) cherry-pick islemine devam et
# 9) dosya.txt icerigini yazdirSample solution
git init -q
echo v1 > dosya.txt
git add dosya.txt
git commit -q -m "v1"
git checkout -q -b surum
echo v2 > dosya.txt
git add dosya.txt
git commit -q -m "v2"
git checkout -q -
echo v3 > dosya.txt
git add dosya.txt
git commit -q -m "v3"
git cherry-pick surum >/dev/null 2>&1 || true
echo v2 > dosya.txt
git add dosya.txt
GIT_EDITOR=true git cherry-pick --continue >/dev/null 2>&1
cat dosya.txtExpected output
v2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens when a conflict appears during cherry-pick?
- Git always chooses the version from the source commit
- The changes are skipped silently and no commit is made
- The branch is deleted automatically
- Conflict markers stay in the file and the operation waits Correct answer
Explanation: When a conflict appears Git cannot decide; markers stay in the file and you fix it by hand before continuing.
2 Finish the operation
git add dosya.txt
git cherry-pick --continue____
git add dosya.txt
git cherry-pick --continuecontinue
Explanation: The option that resumes a cherry-pick after the fix is --continue; there is no resume, proceed or next option.
3 Conflict steps
| Term | Meaning |
|---|---|
conflict |
Same line changed twice |
resolve |
You fix the conflict by hand |
git add |
Marks the resolved file |
--continue |
Finishes the operation |