Solution to Step 0 - START
Solution to Step 1 - Fast-forward on pull
In the simplest case, we have done nothing ourselves, and just want to take over Anja’s changes.
Perform a pull.
Show the status and the commit graph.
fast-forward $ git pull
Updating 47053b2..640cceb
Fast-forward
average.kts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From ../origin-for-merge-samples
47053b2..640cceb main -> origin/main
Git signals that a fast-forward was performed.
The graph shows that no branch was created and no merge was necessary.
fast-forward $ git log --graph --oneline --decorate
* 640cceb (HEAD -> main, origin/main, origin/HEAD) Use double values instead of int
* 47053b2 Created file average.kts on branch main by anja .
* 75dfc35 Created file README.md on branch main by anja .
Solution to Step 2 - Force merge on pull
Again, we have done nothing, and just want to take over Anja’s changes.
Perform a pull with --no-ff
.
Show the status and the commit graph.
no-ff $ git pull --no-ff
Merge made by the 'ort' strategy.
average.kts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From ../origin-for-merge-samples
47053b2..640cceb main -> origin/main
Git signals that no fast-forward was performed.
The graph shows that a merge has occurred.
no-ff $ git log --graph --oneline --decorate
* f71f246 (HEAD -> main) Merge branch 'main' of ../origin-for-merge-samples
|\
| * 640cceb (origin/main, origin/HEAD) Use double values instead of int
|/
* 47053b2 Created file average.kts on branch main by anja .
* 75dfc35 Created file README.md on branch main by anja .
Solution to Step 3 - Integration with changes in different files
- Edit the file
README.md
.- Create a commit for it.
- Check with
git show
if the commit is OK.
- Try a push
- This will fail because your colleague Bea has edited and pushed the file
average.kts
in the meantime.
- This will fail because your colleague Bea has edited and pushed the file
- Integrate with pull
- Examine the result, e.g.
- the commit graph
- the changes Anja made
- the commits Anja made
changes-in-different-files $ # Edit file README.md
changes-in-different-files $ git commit -am "Commited file README.md on branch main by bjoern "
[main cc16e28] Commited file README.md on branch main by bjoern
1 file changed, 1 insertion(+), 1 deletion(-)
changes-in-different-files $ git show
commit cc16e28115d01b0553942e880ce01ec0be06b066
Author: bjoern <kapitel26blog@gmail.com>
Date: Thu Jul 29 00:00:00 2021 +0000
Commited file README.md on branch main by bjoern
diff --git a/README.md b/README.md
index 8b6805c..28cf676 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-Hallo Wolt!
+Hallo Welt!
changes-in-different-files $ git push
To ../origin-for-merge-samples.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '../origin-for-merge-samples.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This message shows that there are changes in origin
that we have not yet integrated.
changes-in-different-files $ git pull
Merge made by the 'ort' strategy.
average.kts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From ../origin-for-merge-samples
47053b2..640cceb main -> origin/main
Git has fetched the changes and created a merge commit.
changes-in-different-files $ git log --oneline --graph
* 7136fb0 Merge branch 'main' of ../origin-for-merge-samples
|\
| * 640cceb Use double values instead of int
* | cc16e28 Commited file README.md on branch main by bjoern
|/
* 47053b2 Created file average.kts on branch main by anja .
* 75dfc35 Created file README.md on branch main by anja .
changes-in-different-files $ git diff HEAD^1...HEAD^2
diff --git a/average.kts b/average.kts
index 4cd02bf..5ff5b2b 100644
--- a/average.kts
+++ b/average.kts
@@ -1,6 +1,6 @@
if(args.isEmpty())
throw RuntimeException("No arguments given!")
-val s = args.map{ it.toInt() }.sum()
+val s = args.map{ it.toDouble() }.sum()
println("The average is ${s/args.size}")
changes-in-different-files $ git log HEAD^2..HEAD^1
commit cc16e28115d01b0553942e880ce01ec0be06b066
Author: bjoern <kapitel26blog@gmail.com>
Date: Thu Jul 29 00:00:00 2021 +0000
Commited file README.md on branch main by bjoern
And now we can try a push again.
changes-in-different-files $ git push
To ../origin-for-merge-samples.git
640cceb..7136fb0 main -> main
Solution to Step 4 - Integration with changes in the same file
In this case, we are editing the same file that Anja also edited. A conflict will occur, which we have to resolve.
- We have already prepared and committed a change that leads to a conflict. Examine it with
git show
. - Perform a pull.
- Show the status and resolve the conflict.
changes-in-same-files $ git show
commit 1fac0522fadc13dbac62d35e6bdeadb3fddc9762
Author: bjoern <kapitel26blog@gmail.com>
Date: Thu Jul 29 00:00:00 2021 +0000
Refactoring: s in summe umbenennen
diff --git a/average.kts b/average.kts
index 4cd02bf..7eb87f2 100644
--- a/average.kts
+++ b/average.kts
@@ -1,6 +1,6 @@
if(args.isEmpty())
throw RuntimeException("No arguments given!")
-val s = args.map{ it.toInt() }.sum()
+val summe = args.map{ it.toInt() }.sum()
-println("The average is ${s/args.size}")
+println("The average is ${summe/args.size}")
changes-in-same-files $ git pull
Auto-merging average.kts
CONFLICT (content): Merge conflict in average.kts
Automatic merge failed; fix conflicts and then commit the result.
From ../origin-for-merge-samples
47053b2..7136fb0 main -> origin/main
As expected, a conflict has occurred.
changes-in-same-files $ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" if you want to integrate the remote branch with yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
modified: README.md
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: average.kts
The file contains conflict markers.
changes-in-same-files $ cat average.kts
if(args.isEmpty())
throw RuntimeException("No arguments given!")
<<<<<<< HEAD
val summe = args.map{ it.toInt() }.sum()
||||||| 47053b2
val s = args.map{ it.toInt() }.sum()
=======
val s = args.map{ it.toDouble() }.sum()
>>>>>>> 7136fb096caf572c0e9bdc2d2ff004dc4032193d
println("The average is ${summe/args.size}")
changes-in-same-files $ # Edit average.kts replacing pattern with val summe = args.map{ it.toDouble() }.sum()
Don’t forget: call git add
after cleaning up.
changes-in-same-files $ git add average.kts
changes-in-same-files $ git commit -m 'Änderungen von Anja integriert'
[main 0f407b2] ?nderungen von Anja integriert
And here again the resulting graph:
changes-in-same-files $ git log --graph --oneline
* 0f407b2 ?nderungen von Anja integriert
|\
| * 7136fb0 Merge branch 'main' of ../origin-for-merge-samples
| |\
| | * 640cceb Use double values instead of int
| * | cc16e28 Commited file README.md on branch main by bjoern
| |/
* / 1fac052 Refactoring: s in summe umbenennen
|/
* 47053b2 Created file average.kts on branch main by anja .
* 75dfc35 Created file README.md on branch main by anja .