One of the most important reasons for using git is it's ability to merge files - it's the number one collaboration tool in our industry.
I makes sense to study and understand exactly what a git merge is.
Three-way-merge
Git uses (default) a standard three-way-merge when it merges files.
It's a simple algorithm.
Two (2) files must be merged
They have a (1) common ancestor - 2+1=3 - hence the name - three-way--merge
Each contributing file is line-by-line compared against the common ancestor
Change rules if one of the contributors has either deleted, added or altered a line in the file, then that change also goes into the result.
If both contributors have different opinions about the same line - then it's a conflict
Let's stir the pot and potentially create a conflict - which we can then solve.
[ ] Copy this code into a terminal and run it - read the code first, can you predict what it does?
git co master
cd $(git root)
mkdir merge-lab && cd "$_"
cat <<EOF > poem.txt
Eeny, meeny, miny, moe,
Catch a tiger by the toe.
If he hollers, let him go,
Eeny, meeny, miny, moe.
My mother told me to pick
the very best one and you - are - it!"
EOF
git add poem.txt && git commit -m "I wrote a poem"
git tag ancestor
Open the file in an editor and see what it contains.
Now we want to mimic a team of four developers hacking in the same file:
@DenverCoder: First one branches out and changes tiger to lion
@WWhite: Second one branches out and changes mother to father
@JulesW: Third one branches out and changes tiger to turtle
YOU: Change the exclamation mark in the end to a punctuation mark directly on the master.
NOTE: The code snippet below makes use of the aliases you created earlier: co and root.
# We'll be working in the same file - all the time
POEM=$(git root)/merge-lab/poem.txt
# Here comes dev1
git co -b lion master
sed -i 's/tiger/lion/g' $POEM
git add $POEM && git commit --author="DenverCoder <denvercoder@dark.net>" -m "Tiger is lion"
# Here comes dev2
git co -b father master
sed -i 's/mother/father/g' $POEM
git add $POEM && git commit --author="WWhite <ww@bb.io>" -m "Mother is father"
# Here comes dev3
git co -b turtle master
sed -i 's/tiger/turtle/g' $POEM
git add $POEM && git commit --author="JulesW <jw@pulp.fiction>" -m "Tiger is turtle"
# Here comes you
git co master
sed -i 's/\!/\./g' $POEM
git add $POEM && git commit -m "! is ."
What on earth is going on in that snippet?
Usually when you've made changes to files and your workspace, the git icon will show a number of changes to be added og committed.
But this time it's clean as a whistle - why is that?
I'll give you a hint - Try to run this command - what are you looking at?
One of the most important reasons for using git is it's ability to merge files - it's the number one collaboration tool in our industry.
I makes sense to study and understand exactly what a git merge is.
Three-way-merge
Git uses (default) a standard three-way-merge when it merges files.
It's a simple algorithm.
Two (2) files must be merged
They have a (1) common ancestor - 2+1=3 - hence the name - three-way--merge
Each contributing file is line-by-line compared against the common ancestor
Change rules if one of the contributors has either deleted, added or altered a line in the file, then that change also goes into the result.
If both contributors have different opinions about the same line - then it's a conflict
Let's stir the pot and potentially create a conflict - which we can then solve.
[ ] Copy this code into a terminal and run it - read the code first, can you predict what it does?
Open the file in an editor and see what it contains.
Now we want to mimic a team of four developers hacking in the same file:
@DenverCoder: First one branches out and changes tiger to lion @WWhite: Second one branches out and changes mother to father @JulesW: Third one branches out and changes tiger to turtle YOU: Change the exclamation mark in the end to a punctuation mark directly on the master.
NOTE: The code snippet below makes use of the aliases you created earlier:
co
androot
.What on earth is going on in that snippet?
Usually when you've made changes to files and your workspace, the git icon will show a number of changes to be added og committed.
But this time it's clean as a whistle - why is that?
I'll give you a hint - Try to run this command - what are you looking at?