Tue Oct. 24

Version Control - GIT Workshop

9 – 12h Teaching

Teacher:

  • Simon Eugster
  • Diego Ballesteros Villamizar

Goals

  • I understand the concept of Version Control and how to use it
  • I can use Version Control software for my own projects and other Repos on GitHub (incl. Merge Requests)
  • I know how to find more information about Version Control including git help
  • I know how to visualize differences between versions and how to undo changes

Important Things

  • No slides! You are lost.
  • Or, take notes directly in a Git repository!
  • Ask if something is unclear.
  • We will use Git Bash! Command line does not change. GUIs will be obvious. (Fails the other way round)
  • Servers usually only accessible through command line.

Intro

Why version control, and what is it?

  • Take snapshots of code and upload them to a server
  • Ever broke something and wanted to go back?
  • Or, managed to fix a bug but forgot what you actually changed?
  • Work together on the same code

Links

Getting started: Clone a repository

  1. Clone https://github.com/power-coders/powercoders-002-git-workshop, see commits online
  2. Delete all files except for .git, restore with git checkout -- . (optionally when offline)
  3. Examine gitk
  4. Examine .git/

First repository

init

Can be used completely offline.

  1. Create new repository in empty directory
  2. Examine .git and .git/objects/ again

git status, add, commit

Copy questions file from powercoders repository.

  1. Use git status and read the output after every operation. Command is new best friend.
  2. Commit the file.
  3. Examine .git/objects/ again

Accidentially delete questions file

  1. Check status and READ
  2. Revert

Check the logs

  1. Add something to “I want to remember”
  2. Commit
  3. Show git log in command line with git log and in gitk
  4. Find out how to show log on single line with git help log

Theory

Git Areas

  • Checkout / Workspace
  • Staging Area / Index
  • Local Repository

Commits

  • Snapshot of every single file (go to .git/objects/)
  • Hash uniquely identifies commit.
  • Has parent.

git diff

Diff between two arbitrary snapshots.

  1. Compare to HEAD^
  2. Compare two commits by short hash
  3. Change file and show diff
  4. Stage file and show diff

Undo changes in staging area and workspace

  1. Change file, undo it
  2. Change file, stage it, undo it.

git branch

Create branch with new directory

  1. Create and checkout branch
  2. Examine in gitk and git log --decorate
  3. Add directory food with file favourite.md, commit

Switch Branches

  1. Check out master branch, check files
  2. Add a new file best-about-git.md and commit it
  3. Merge other branch into master

Theory

Branches

  • Branch: Pointer in .git/refs/heads/
  • Why branches? Merge requests, add features. master remains stable.
  • Two commits have same parent. See D3 animation

git merge, rebase

Merge Conflict Scenario: Two people working on the same file

  1. Add favourite beverage on branch feature/beverage to favourite.md
  2. Add favourite swiss food on master
  3. Merge feature branch into master
  4. Resolve merge conflict

Rebasing a branch

  1. Same as before
  2. Go to feature branch and rebase it onto master

Theory

Rebase; show with D3

  • Diff is always between two things. Hard with branches.
  • Rebase avoids divergent branches
  • Merge leaves commits unchanged
  • Later: pull always with rebase

git stash, remote

Stash changes and get them back

  1. Change something and stash it away
  2. Check git status, git log, gitk --all
  3. Recover the stash with git pop --index

Remotes

  1. Show remotes on cloned exercise repo and on local repository
  2. Create repository on GitHub
  3. Add repository as new remote

Theory

  • Stash: New area, also saves snapshots, but only locally.
  • Remotes: Repository on a server; server must accept connections. Examples: GitHub, GitLab, BitBucket, gitolite

12 – 13.30h Lunch

13.30 – 17h Coaching

Coaches:


Afternoon exercises

  • Push new repository to Github. Find out how to create SSH key.
  • Also push the branches.
  • Fork repository of someone else
  • Submit merge request
  • Fill in remaining questions/goals in exercise sheet
  • Time per question. Answer questions if necessary.
  • Work on same repository: pull --rebase
  • Push own web page