Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Getting’ the hang of git
Created by Sinthujan ThanabalasingamUpdated by Simon Birkenheuer
Version control and why git matters
• Complex projects require teams of developers• Division of labour as central aspect• Integration of sub parts to one whole• How do you manage the code base?
TU Dresden – RoboLab 2019
Why Git?
?!
Easy!USB drives! Just share source code between colleagues via flash drives!
TU Dresden – RoboLab 2019
Version Control System (VCS)• Tracking of distributed documents and changes
to them• Automatic integration (merging) of changes and
revisions• Recover previous states• Managing different versions of the project• Essential for organization of multi-developer-
projects
TU Dresden – RoboLab 2019
git to the rescue• Distributed VCS• Managing projects in form of repositories• Support by web-based hosting services like
GitHub or Bitbucket• Available for Linux, Windows and macOS
TU Dresden – RoboLab 2019
How to get git
Debian/Ubuntu/WSL
Windows• git bash: https://gitforwindows.org• WSL (Windows Subsystem for Linux)
macOS• https://git-scm.com/download/mac
sudo apt-get install git-all
TU Dresden – RoboLab 2019
git – basic workflow
git init git clone <url>or
Every directory can be managed by git.
• Initializes/copies a repository in the current working directory
• Every developer works on a local copy of the project (local repository)
TU Dresden – RoboLab 2019
status of the repository
• Summary of changes since the last commit• Lists new, deleted and changed files• Tool to decide which changes should be included
in the next commit
git status
TU Dresden – RoboLab 2019
git status - result
TU Dresden – RoboLab 2019
Adding changesAdding files to the git index is called staging.
adds file <filename> to the index
adds all files to the index
git add <filename>
git add .
TU Dresden – RoboLab 2019
Adding changes and status check
TU Dresden – RoboLab 2019
git commit
• Bundles changes to a commit • commits are saved locally• commit message
• Should be a meaningful description of changes made by the dev
• Is visible information for other developers
git commit –m “commit message”
TU Dresden – RoboLab 2019
The emphasis lays on meaningful…
git commit –m “it works”
git commit –m “Fix all errors - Robi completed his task”
git commit –m “bugs”
Bad commit messages encountered @robolab:
TU Dresden – RoboLab 2019
The emphasis lays on meaningful…
git commit –m “using a less bug prone break condition for path backtrace”
git commit –m “implemented getter for communication points”
Good commit messages:
TU Dresden – RoboLab 2019
It’s alive! - The commit history
TU Dresden – RoboLab 2019
???
It’s alive! - The commit history
master head
TU Dresden – RoboLab 2019
init first commit last commit
git branch
• Creates a new branch called <name>
• Changes the current branch to <branch>• Future changes now only apply to new branch
git branch <name>
git checkout <branch>
TU Dresden – RoboLab 2019
It’s alive! - Branches growing
master
init first commit last commit
feature head
TU Dresden – RoboLab 2019
It’s alive! - Branches growing
master
feature head
TU Dresden – RoboLab 2019
init first commit last commit
Now, you might ask yourself…How is this process collaborative?How do you publish your changes? And most important: where do you publish your changes to?
TU Dresden – RoboLab 2019
git remote got you covered
• Connects local repository with a remote repository• <alias>: alias, under which the remote is known
locally• <url>: location of the remote repository
• Lists all available remote repositories
git remote set-url <alias> <url>
git remote –v
TU Dresden – RoboLab 2019
Share your bugs with the world!™
• Pushes all unpublished commits on the branch <branch> of the remote repository called <alias>
• If both <alias> and <branch> are left empty the current branch gets pushed to the default alias origin
• Authentication is required every time
git push <alias> <branch>
TU Dresden – RoboLab 2019
git pull
• Fetches the most recent state of the remote repository
• git tries to auto-merge both versions of the codebase
What could possibly go wrong?
git pull
TU Dresden – RoboLab 2019
Staying up-to-dateHow to integrate the new bugs my colleague just programmed?
TU Dresden – RoboLab 2019
Staying up-to-date
TU Dresden – RoboLab 2019
origin/master
head
new bug from teammate
remote:
master headlocal:
Staying up-to-date: Done right
TU Dresden – RoboLab 2019
origin/master
head
new bug from teammate
remote:
master head
new bug from teammate
local:
Staying up-to-date: Done right
TU Dresden – RoboLab 2019
origin/master
head
new bug from teammate
remote:
new bug from teammate
local: master head
Your fancy new bug
Staying up-to-date: Done wrong
TU Dresden – RoboLab 2019
origin/master
head
new bug from teammate
remote:
your new bug
local: master head
A saviour presents itself!
origin/master
head
new bug from teammate
remote:
new bug from teammate
local: master head
your new bug
TU Dresden – RoboLab 2019
git pull --rebase
git merge
TU Dresden – RoboLab 2019
git merge – The integration step
• Integrates state of <other-branch> into current branch
• Is always triggered on a pull• If merge fails (and boy they do), you are
presented with merge-conflicts
That sounds terrifying, because at first, it is!
git merge <other-branch>
TU Dresden – RoboLab 2019
git merge – The integration step
master
feature
head
TU Dresden – RoboLab 2019
git merge – The integration step
master
feature
head
TU Dresden – RoboLab 2019
merge commit
A simple file in conflict<<<<<<< HEAD:hello_world.py print(“Hello World!")======= print(“Hello!")print(“Good Bye, World!")>>>>>>>24b9b78:hello_world.py
local version
remote version
conflict resolution markers
hello_world.py
TU Dresden – RoboLab 2019
Resolve the conflict by hand
• Remove the conflict resolution markers• Craft a solution of the code that fits your needs• Solution can be a mix of the local and remote state• Add the resolved files, commit and push them
TU Dresden – RoboLab 2019
Merge conflicts
TU Dresden – RoboLab 2019
• Conflicts occur if…• Remote changes were not integrated properly• Remote and local state of files or branches differ “too much”
• So please… • Always pull remote changes before you start working• Communicate in your team to avoid merge conflicts• Use branches to divide work• Avoid multiple people working on the same file
Undoing your screwing
• Unstages a commit in progress but your local changes stay
• Unstages all files and deletes all changes since last commit
• Do not do this if there is work you • want to keep!
git reset
git reset --hard
TU Dresden – RoboLab 2019
Almost done
Any general questions before we tackle details to git during RoboLab?
?TU Dresden – RoboLab 2019
Hints regarding the course• The use of git is mandatory• Commit regularly: avoid 1000 line monolithic commits• Commit quality and frequency can and will influence
your grade• Commits should be evenly distributed between all
members• On exam day, the last state of the master branch
counts
TU Dresden – RoboLab 2019
Getting’ started1. Create a Bitbucket account & tell your tutor your
username, also install git ( ͡° ͜ʖ ͡° )2. You will be granted access to the robolab-template-lab
repository3. Only one of the team members clones the repository using
the following instruction (recursively!!)
git clone --recursive https://bitbucket.com/tudrobolab/robolab-template-lab.git
TU Dresden – RoboLab 2019
Gettin’ started4. Change the remote url to point to your own team
repository on Bitbucket that we prepared for you, using
Replace <id> with your group id with leading zeros!5. Verify that the remote now points to your team repo with
git remote set-url origin https://se-gitlab.inf.tu-dresden.de/robolab-autumn/ws2019/group-id
git remote -v
TU Dresden – RoboLab 2019
Getting’ started6. Perform an initial commit and push it using
7. All remaining team members clone the freshly populated team repo using:
git push origin master
git clone --recursivehttps://se-gitlab.inf.tu-dresden.de/robolab-autumn/ws2019/group-id
TU Dresden – RoboLab 2019
One last Tip
TU Dresden – RoboLab 2019
git log --graph --decorate --oneline --all
git config --global alias.<name> <command>
git tree
• You can define your own git aliases
• And then do fancy stuff like this in one line
git tree in action
TU Dresden – RoboLab 2019
Done. It’s time to GIT GUDAny questions remaining?
Please ask!
?TU Dresden – RoboLab 2019