Alice: merging with conflict resolution
In this chapter, Alice will show us how to properly manage more sophisticated merge operations.
Lab
- Alice will keep working hard in increasing the tension of the story in the main branch, expanding one of the sentences to provide a more immersive description:
git status
sed -i 's/afloat/afloat, fighting the waves/' chapter-01.md
git diff chapter-01.md
git add chapter-01.md
git commit -m "Enhanced paragraph"
- At the same time, she also keeps pushing for a richer use of language, so she moves HEAD to the vocabulary branch
git checkout vocabulary-chapter-01
- Let’s confirm that there is room for improvement
cat chapter-01.md | grep -e strug -e shore -C 9999
- Definitively, she can make some changes
sed -i 's/struggling/floundering/' chapter-01.md
sed -z -i 's/shore/waterfront/2' chapter-01.md
- Just to ensure that everything is in place, Alice compares the working area content with the one pointed by HEAD
git diff chapter-01.md
- Looks look, so she decides to move the file to the staging area
git add chapter-01.md
- And she commits the change
git commit -m "Replaced struggling and shore."
- Avoiding merges for a long time is considered a bad practice, so Alice will integrate both branches. She starts checking that the current branch is not main:
git status
git branch
- Ok, so she moves to the main branch
git checkout main
- She has a feeling that the merge operation is going to be more complicated this time, and she checks it by comparing the current working area (on main) with the files on the vocabulary-chapter-01 branch:
git diff vocabulary-chapter-01
- In any case, she needs to merge the vocabulary branch into the main one
git merge vocabulary-chapter-01 -m "Merged vocabulary" # Conflict!!
- Something went wrong: she has modified the same lines on both branches and
git
doesn’t know how to solve the conflict, asstatus
confirms:
git status
- The content of the file in the working area has been altered to show both versions of the reality
cat chapter-01.md
- Although it is easy to understand what is happening thanks to
delta
git diff
- Alice decides that she will manually correct the situation to keep both the lighthouse keeper and the floundering portions in the definitive version
L0=$(grep -n "<<<<<<<" chapter-01.md | cut -f1 -d:)
LF=$(grep -n ">>>>>>>" chapter-01.md | cut -f1 -d:)
echo Replacing from $L0 to $LF.
ed chapter-01.md << EOF
${L0},${LF}d
7i
As the waves tossed him around, Tim struggled to stay afloat, fighting the waves. Just when he thought he couldn't hold on any longer, a strong hand grabbed his wrist and pulled him to safety. It was the lighthouse keeper who had noticed him floundering from the shore.
.
w
q
EOF
- Yes, she has it:
cat chapter-01.md | grep -e "lighthouse keeper" -e "floundering" -C 9999
- Time to commit the chapter:
git status
git add chapter-01.md
git commit -m "Solved conflict, merged keeper and floundering."
- The resulting history is pretty cool. Both the content of the chapter and the log ;)
git log --graph --decorate --oneline