45
Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters

Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Getting’ the hang of git

Created by Sinthujan ThanabalasingamUpdated by Simon Birkenheuer

Version control and why git matters

Page 2: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

• 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?

Page 3: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

?!

Easy!USB drives! Just share source code between colleagues via flash drives!

TU Dresden – RoboLab 2019

Page 4: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 5: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 6: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 7: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 8: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 9: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

git status - result

TU Dresden – RoboLab 2019

Page 10: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 11: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Adding changes and status check

TU Dresden – RoboLab 2019

Page 12: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 13: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 14: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 15: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

It’s alive! - The commit history

TU Dresden – RoboLab 2019

???

Page 16: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

It’s alive! - The commit history

master head

TU Dresden – RoboLab 2019

init first commit last commit

Page 17: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 18: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

It’s alive! - Branches growing

master

init first commit last commit

feature head

TU Dresden – RoboLab 2019

Page 19: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

It’s alive! - Branches growing

master

feature head

TU Dresden – RoboLab 2019

init first commit last commit

Page 20: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 21: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 22: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 23: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 24: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Staying up-to-dateHow to integrate the new bugs my colleague just programmed?

TU Dresden – RoboLab 2019

Page 25: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Staying up-to-date

TU Dresden – RoboLab 2019

origin/master

head

new bug from teammate

remote:

master headlocal:

Page 26: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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:

Page 27: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 28: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Staying up-to-date: Done wrong

TU Dresden – RoboLab 2019

origin/master

head

new bug from teammate

remote:

your new bug

local: master head

Page 29: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 30: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

git merge

TU Dresden – RoboLab 2019

Page 31: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 32: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

git merge – The integration step

master

feature

head

TU Dresden – RoboLab 2019

Page 33: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

git merge – The integration step

master

feature

head

TU Dresden – RoboLab 2019

merge commit

Page 34: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 35: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 36: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 37: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 38: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Almost done

Any general questions before we tackle details to git during RoboLab?

?TU Dresden – RoboLab 2019

Page 39: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 40: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 41: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 42: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 43: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

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

Page 44: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

git tree in action

TU Dresden – RoboLab 2019

Page 45: Getting’ the hang of git - TU Dresden · Getting’ the hang of git Created by Sinthujan Thanabalasingam Updated by Simon Birkenheuer Version control and why git matters ... git

Done. It’s time to GIT GUDAny questions remaining?

Please ask!

?TU Dresden – RoboLab 2019