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 pull
integrates the local branch with its “upstream” counterpart, here:main
andorigin/main
Tips
git config --global merge.conflictStyle diff3
improves the display of conflicts.git pull
fetches and integrates changes equivalent togit fetch
+git merge
)git log --graph
shows the commit graphgit diff main origin/main
shows the changes of the othersgit log main..origin/main
shows the commits of the othersHEAD^1
andHEAD^2
denote the first and second predecessor, of the currentHEAD
commit.git diff HEAD^1...HEAD^2
shows the “foreign” changesgit log HEAD^1..HEAD^2
shows 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/<unknown>
.
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/<unknown>
.
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/<unknown>
.
- 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 $ cd ..
$ cd changes-in-same-files
Step 4 - Integration with changes in the same file
Start in directory git-uebungen/aufgaben/<unknown>
.
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 ..