Learning how to be accelerated together
rather than being slowed down causing chaos on the way
Important aspects
It's like having roommates
if no one does the dishes, soon
all hell breaks loose
Tools and methods to make collaboration smooth
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 |
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
Important aspects
Today we focus on git
I recommend everyone to watch the two first CodeRefinery git-intro videos or read the
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
Some git terminology
I can recommend the Pro Git 2nd Edition open source book
Some git terminology
All data and the entire history (commits, branches, tags, ...)
Thats the .git folder
Some git terminology
Some git terminology
Commits also have parents (except the first)
A series of commits can look like
Is that not a lot of duplicated data?
git gc
Delta compression...
Some git terminology
So that is what a commit is
Some git terminology
Some git terminology
git commit also advances this pointer to that commitHEAD pointers tracks where your
are (branch or commit)
HEAD on remotes are sort of the "default" branch)
Some git terminology
An example
git checkout testing
...
git commit
git checkout master
...
git commit
Some git terminology
So that is what a branch is
Some git terminology
Some git terminology
A static pointer to a commit
Make a complete copy of another repository
set that repository as a remote
Another repository somewhere
(we will use a "bare" repository for testing)
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
Useful git commands
diffbranchcheckoutfetchmergepullpushrebaseblamelogremotereset(no this is not a daft punk song)
Useful git commands
diffbranchcheckoutfetchmergepullUseful git commands
diff
git diff HEAD..HEAD~1
git specifying revisions
^ and ~ and @{} are different (and
there are more!)
branchcheckoutfetchmergepullUseful git commands
diffbranch
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
fetchmergepullUseful git commands
diffbranchcheckoutfetchDownload objects and refs from another repository
git fetch # default remote
git fetch origin
git fetch --all
mergepullUseful git commands
diffbranchcheckoutfetchmergeMerge two or more development histories together
git merge target_branch # or commit-id
git merge (--continue | --abort | --quit)
Can enter conflict if incompatible histories
pullUseful git commands
diffbranchcheckoutfetchmergepull
git pull
git pull origin
fetch and merge remote branch into local branch
Can enter conflict if incompatible histories
Useful git commands
diffbranchcheckoutfetchmergepullpushrebaseblamelogremoteresetUseful 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)
rebaseblamelogremoteresetUseful git commands
pushrebase
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
blamelogremoteresetUseful git commands
pushrebaseblameShow what revision and author last modified each line of a file
git blame path/to/file
logremoteresetUseful git commands
pushrebaseblamelogShows commit logs
git log
git log --all --graph --decorate --oneline
git log HEAD~3..HEAD
remoteresetUseful git commands
pushrebaseblamelogremoteWork with remotes
git remote -v
git remote remove upstream
git remote add upstream LOCATION
resetUseful git commands
pushrebaseblamelogremoteresetReset HEAD to a specified state
git reset HEAD~1
git reset [--soft | --mixed | --hard ...]
BE CAREFUL - you can lose data with reset
A quick rebase overview
git checkout master
git merge experiment
# RESET THAT
git checkout experiment
git rebase master
# 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
Tools integration: git blame in the editor
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
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
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
git-clash-person1git-clash-person2git remote -v on both point to git-clash.gitSome practical experience
Now you should have two terminals
git-clash-person1git-clash-person2git remote -v on both point to git-clash.gitThis is exactly like a 2-person collaboration trough gitlab
Some practical experience
Your task: make this happen and then fix it
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
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
Some practical experience
Person 2 terminal
git pull --rebase
# This causes a merge conflict
cat README.md
<<<<<<< HEAD
stuff...
=======
stuff...
>>>>>>> 952ba69
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
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
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
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!
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!
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
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
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"
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"
Let us look at the handout