Linked handout: 6. Collaboration

Collaboration

Learning how to be accelerated together

rather than being slowed down causing chaos on the way

Collaboration

Important aspects

  • Collaboration is a social contract $\mapsto$
    you need to communicate what you expect!
    And listen to what every one else expects!
  • It's like having roommates
    if no one does the dishes, soon all hell breaks loose

  • Establish a way of doing things $\mapsto$
    and stick to it, if it does not work, find a new way together

    Tools and methods to make collaboration smooth

Collaboration

Important aspects

Tools and methods to make collaboration smooth

Sharing code $\mapsto$version control
Reading code $\mapsto$code style
Writing code $\mapsto$common design / development plan
Running code $\mapsto$build system / dependency management
Verifying code $\mapsto$tests

Collaboration

Important aspects

Tools and methods to make collaboration smooth

Sharing code $\mapsto$git
Reading code $\mapsto$flake8/ruff/...
Writing code $\mapsto$documentation & co-coding/code-review
Running code $\mapsto$make / bash / ... & python packaging
Verifying code $\mapsto$pytest / generic automation system

2,3 we covered - 4,5 is later lectures

Collaboration

Important aspects

Today we focus on git

I recommend everyone to watch the two first CodeRefinery git-intro videos or read the

Introduction to version control with Git - Why we want to track versions and how to go back in time to a working version

Collaboration

Important aspects

Today we focus on git

The command everyone should know: man

                    
                        man git
                        man git-merge
                        man git-rebase
                        man git-pull
                        man git-commit
                        man git-push
                        man git-checkout
                        ...
                    
                

There is SO much good documentation

Collaboration

Some git terminology

  • repository
  • commit
  • branch
  • tag
  • clone
  • remote

I can recommend the Pro Git 2nd Edition open source book

Collaboration

Some git terminology

  • repository

All data and the entire history (commits, branches, tags, ...)

Thats the .git folder

Collaboration

Some git terminology

  • repository
  • commit
  • branch
  • tag
  • clone
  • remote

Collaboration

Some git terminology

  • commit
  • A commit is a so called "snapshot"
    (not file-based differences like most other systems)
  • This snapshot contains meta data and points to a data tree

Commits also have parents (except the first)
A series of commits can look like

    Is that not a lot of duplicated data?

  • Yes, but git also does diffs in a smart way!
  • Every time you push, it optimizes the storage
    Trigger it yourself with git gc
  • do you recognize the output?
    One of the lines Delta compression...

Collaboration

Some git terminology

  • commit

So that is what a commit is

Collaboration

Some git terminology

  • repository
  • commit
  • branch
  • tag
  • clone
  • remote

Collaboration

Some git terminology

  • branch
  • A lightweight movable pointer to one of these commits
  • A git commit also advances this pointer to that commit
  • You are building a directed acyclic graph
  • A branch... is branch (two commits with same parent)
  • A merge is... a merge (one commits with two or more parents)
  • The special HEAD pointers tracks where your are (branch or commit)
    (HEAD on remotes are sort of the "default" branch)

Collaboration

Some git terminology

  • branch
  • A lightweight movable pointer to one of these commits

An example

Figure 13. HEAD pointing to a branch - (CC BY-NC-SA 3.0)
Figure 13. HEAD pointing to a branch - (CC BY-NC-SA 3.0)
                    
                        git checkout testing
                    
                
Figure 14. HEAD points to the current branch - (CC BY-NC-SA 3.0)
                    
                        ...
                        git commit 
                    
                
Figure 15. The HEAD branch moves forward - (CC BY-NC-SA 3.0)
                    
                        git checkout master
                    
                
Figure 16. HEAD moves when you checkout - (CC BY-NC-SA 3.0)
                    
                        ...
                        git commit
                    
                
Figure 17. Divergent history - (CC BY-NC-SA 3.0)

Collaboration

Some git terminology

  • branch

So that is what a branch is

Collaboration

Some git terminology

  • repository
  • commit
  • branch
  • tag
  • clone
  • remote

Collaboration

Some git terminology

  • tag
  • A static pointer to a commit

  • clone
  • Make a complete copy of another repository
    set that repository as a remote

  • remote
  • Another repository somewhere
    (we will use a "bare" repository for testing)

Collaboration

Some git terminology

Remotes have their own versions of branches

                    
                         ~/g/phd-course-software-development (main)> git branch -a
                        * main
                          remotes/origin/HEAD -> origin/main
                          remotes/origin/main
                    
                

These can of course diverge like we saw before

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • merge
  • pull
  • push
  • rebase
  • blame
  • log
  • remote
  • reset

(no this is not a daft punk song)

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • merge
  • pull

Collaboration

Useful git commands

  • diff
  •                             
                                    git diff HEAD..HEAD~1
                                
                            
    git specifying revisions

    ^ and ~ and @{} are different (and there are more!)

  • branch
  • checkout
  • fetch
  • merge
  • pull

Collaboration

Useful git commands

  • diff
  • branch
  •                             
                                    git branch new_branch
                                
                            
  • checkout
  •                             
                                    git checkout new_branch
                                    git checkout -b another_new
                                    git checkout v0.1  # a tag name
                                    git checkout f5b4bf3  # a commit
                                
                            
  • fetch
  • merge
  • pull

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • Download objects and refs from another repository

                                
                                    git fetch  # default remote
                                    git fetch origin
                                    git fetch --all
                                
                            
  • merge
  • pull

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • merge
  • Merge two or more development histories together

                                
                                    git merge target_branch  # or commit-id
                                    git merge (--continue | --abort | --quit)
                                
                            

    Can enter conflict if incompatible histories

  • pull

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • merge
  • pull
  •                             
                                    git pull
                                    git pull origin
                                
                            

    fetch and merge remote branch into local branch

    Can enter conflict if incompatible histories

Collaboration

Useful git commands

  • diff
  • branch
  • checkout
  • fetch
  • merge
  • pull
  • push
  • rebase
  • blame
  • log
  • remote
  • reset

Collaboration

Useful git commands

  • push
  •                             
                                    git push
                                    git push origin
                                
                            

    Updates remote refs using local refs
    (default is)branch $\mapsto$ remote/name/branch

    Can fail if incompatible histories
    (then you need to pull and fix)

  • rebase
  • blame
  • log
  • remote
  • reset

Collaboration

Useful git commands

  • push
  • rebase
  •                             
                                    git rebase target_branch
                                    git pull --rebase
                                    git rebase (--continue|--skip|--abort|--quit)
                                
                            

    Reapply commits on top of another base tip

    Can enter conflict if incompatible histories

  • blame
  • log
  • remote
  • reset

Collaboration

Useful git commands

  • push
  • rebase
  • blame
  • Show what revision and author last modified each line of a file

                                
                                    git blame path/to/file
                                
                            
  • log
  • remote
  • reset

Collaboration

Useful git commands

  • push
  • rebase
  • blame
  • log
  • Shows commit logs

                                
                                    git log
                                    git log --all --graph --decorate --oneline
                                    git log HEAD~3..HEAD
                                
                            
  • remote
  • reset

Collaboration

Useful git commands

  • push
  • rebase
  • blame
  • log
  • remote
  • Work with remotes

                                
                                    git remote -v
                                    git remote remove upstream
                                    git remote add upstream LOCATION
                                
                            
  • reset

Collaboration

Useful git commands

  • push
  • rebase
  • blame
  • log
  • remote
  • reset
  • Reset HEAD to a specified state

                                
                                    git reset HEAD~1
                                    git reset [--soft | --mixed | --hard ...]
                                
                            

    BE CAREFUL - you can lose data with reset

Collaboration

A quick rebase overview

Figure 35. Simple divergent history - (CC BY-NC-SA 3.0)
Figure 35. Simple divergent history - (CC BY-NC-SA 3.0)
                    
                        git checkout master
                        git merge experiment
                    
                
Figure 36. Merging to integrate diverged work history - (CC BY-NC-SA 3.0)
                    
                        # RESET THAT
                        git checkout experiment
                        git rebase master
                    
                
Figure 37. Rebasing the change introduced in C4 onto C3 - (CC BY-NC-SA 3.0)
                    
                        # C4 does not exist any more, it is now C4'
                        # So now we can 'fast forward' master as they are in line
                        git checkout master
                        git merge experiment
                    
                
Figure 38. Fast-forwarding the master branch - (CC BY-NC-SA 3.0)

Collaboration

Tools integration: git blame in the editor

Collaboration

Some practical experience

First we will setup a two person situation, but locally

                    
                        # go to the folder where you want to do this exercise
                        git init --bare git-clash.git
                        git clone ./git-clash.git git-clash-person1
                    
                

Collaboration

Some practical experience

Now we add some content

                    
                        cd git-clash-person1
                        echo "Hello!" > README.md
                        git add README.md
                        git commit -m "added readme"
                        git push
                    
                

Open up a new terminal and put them side-by-side

Collaboration

Some practical experience

Open up a new terminal and put them side-by-side

Navigate to the folder where you put your git folders

                    
                        git clone ./git-clash.git git-clash-person2
                        cd git-clash-person2
                    
                

Now you should have two terminals

  • one in git-clash-person1
  • one in git-clash-person2
  • git remote -v on both point to git-clash.git

Collaboration

Some practical experience

Now you should have two terminals

  • one in git-clash-person1
  • one in git-clash-person2
  • git remote -v on both point to git-clash.git

This is exactly like a 2-person collaboration trough gitlab

Collaboration

Some practical experience

Your task: make this happen and then fix it

Collaboration

Some practical experience

Person 1 terminal

                    
                        echo "World!" >> README.md
                        cat README.md
                        git add README.md
                        git commit -m "forgot world"
                        git push
                    
                

Person 2 terminal

                    
                        echo "Friend!" >> README.md
                        cat README.md
                        git add README.md
                        git commit -m "Someone forgot to add the end!"
                        git push
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        git push
                        # This fails with "! [rejected]"
                    
                
                    
                        git pull
                        # also fails!
                        # "You have divergent branches and need to specify how to reconcile them."
                        git status
                        git log --oneline --graph origin/main main
                    
                

You see how you have a divergence? This will need to be fixed

Collaboration

Some practical experience

Person 2 terminal

                    
                        git pull --rebase
                        # This causes a merge conflict
                        cat README.md
                    
                
                    
                        <<<<<<< HEAD
                            stuff...
                        =======
                            stuff...
                        >>>>>>> 952ba69
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        <<<<<<< HEAD
                            stuff...
                        =======
                            stuff...
                        >>>>>>> 952ba69
                    
                

Special marker inserted by git

HEAD is remote changes

hash is your changes

Resolve the conflict and remove the markers

Collaboration

Some practical experience

Person 2 terminal

                    
                        git add README.md
                        git rebase --continue
                        # No need to commit, you are modifying 
                        # your old commit to not be in conflict!

                        git log --oneline --graph origin/main main

                        git push
                    
                

git log shows one line of commits

Collaboration

Some practical experience

Person 2 terminal

                    
                        nvim README.md 
                        # Modify a word to upper case, e.g. World -> WORLD
                        git add README.md
                        git commit -m "thats better"
                    
                

Person 1 terminal

                    
                        git pull
                        nvim README.md
                        # Modify the same word to another word, e.g. World -> Earth
                        git add README.md
                        git commit -m "naming convention"
                        git push
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        git push
                        # ! [rejected] again!
                        git pull --no-rebase
                        git status
                        cat README.md
                        # fix conflict
                        git add README.md
                        git merge --continue

                        git log --oneline --graph origin/main main
                    
                

git log shows a branch and a merge!

Collaboration

Some practical experience

Person 2 terminal

                    
                        echo "I just want to add a line" > README.md
                        git add README.md
                        git commit -m "added a line"
                        cat README.md

                        # OPS a misstake!
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        git reset --soft HEAD~1
                        git status
                        cat README.md

                        # We still have the changes staged, just not committed
                        # Lets see what --hard/mixed does
                        
                        git commit -m "added a line"
                        # Now we are back to how it was before the reset
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        git reset --mixed HEAD~1
                        git status
                        cat README.md

                        # Changes are still there but not staged and not committed
                        git add README.md
                        git commit -m "added a line"

                        # Now we are back to how it was before the reset
                        # now lets try --hard
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        git reset --hard HEAD~1
                        git status
                        cat README.md

                        # Now the changes are gone too

                        # lets do a different commit
                        echo "I just want to add a line" >> README.md
                        echo "new file" > test.txt
                        git add README.md test.txt
                        git commit -m "added a line"
                    
                

Collaboration

Some practical experience

Person 2 terminal

                    
                        # Lets say i want to revert just README.md 

                        # There are many ways to do that, but could e.g.
                        git reset --mixed HEAD~1
                        git status
                        git checkout README.md
                        git add test.txt
                        git commit -m "added a test file"
                    
                

Collaboration

Let us look at the handout