Exercise - Integration of changes
When multiple developers work independently on the same project, their changes must be integrated from time to time. This is called merging.
Integration can be done in Git with the commands pull, merge and rebase.
This often leads to merge conflicts.
In this exercise, we show integration via pull, because this is very typical for working with Git.
However, the merging and handling of conflicts works very similarly with merge and rebase.
Info
git pullintegrates the local branch with its “upstream” counterpart, here:mainandorigin/main
Tips
git config --global merge.conflictStyle diff3improves the display of conflicts.git pullfetches and integrates changes equivalent togit fetch+git merge)git log --graphshows the commit graphgit diff main origin/mainshows the changes of the othersgit log main..origin/mainshows the commits of the othersHEAD^1andHEAD^2denote the first and second predecessor, of the currentHEADcommit.git diff HEAD^1...HEAD^2shows the “foreign” changesgit log HEAD^1..HEAD^2shows the “foreign” commits- After a merge conflict:
- Edit conflict files
- then don’t forget
git add - Complete the merge with
git commit
Initial situation
Your colleague Anja has started working on a project. Now you join and take over tasks. But Anja has also continued to work in parallel. Integrate the new changes from Anja.
Step 0 - START
$ cd fast-forward
Step 1 - Fast-forward on pull
Start in directory git-uebungen/aufgaben/zusammenarbeit-integration-von-aenderungen/fast-forward.
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 $ cd ..
$ cd no-ff
Step 2 - Force merge on pull
Start in directory git-uebungen/aufgaben/zusammenarbeit-integration-von-aenderungen/no-ff.
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 $ cd ..
$ cd changes-in-different-files
Step 3 - Integration with changes in different files
Start in directory git-uebungen/aufgaben/zusammenarbeit-integration-von-aenderungen/changes-in-different-files.
- Edit the file
README.md.- Create a commit for it.
- Check with
git showif the commit is OK.
- Try a push
- This will fail because your colleague Bea has edited and pushed the file
average.ktsin 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 $ cd ..
$ cd changes-in-same-files
Step 4 - Integration with changes in the same file
Start in directory git-uebungen/aufgaben/zusammenarbeit-integration-von-aenderungen/changes-in-same-files.
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 $ cd ..