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 and origin/main

Tips

  • git config --global merge.conflictStyle diff3 improves the display of conflicts.
  • git pull fetches and integrates changes equivalent to git fetch + git merge)
  • git log --graph shows the commit graph
  • git diff main origin/main shows the changes of the others
  • git log main..origin/main shows the commits of the others
  • HEAD^1 and HEAD^2 denote the first and second predecessor, of the current HEAD commit.
  • git diff HEAD^1...HEAD^2 shows the “foreign” changes
  • git log HEAD^1..HEAD^2 shows the “foreign” commits
  • After a merge conflict:
    1. Edit conflict files
    2. then don’t forget git add
    3. 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>.

  1. Edit the file README.md.
    • Create a commit for it.
    • Check with git show if the commit is OK.
  2. Try a push
    • This will fail because your colleague Bea has edited and pushed the file average.kts in the meantime.
  3. Integrate with pull
  4. 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.

  1. We have already prepared and committed a change that leads to a conflict. Examine it with git show.
  2. Perform a pull.
  3. Show the status and resolve the conflict.
changes-in-same-files $ cd ..


To the solution

To the overview