Git

version control, for beginners

$ git init

Who has used Git before?

Who has used it in a team?

Who has ever lost work they wanted back?

Who has a folder called
final-v2?

The fear of Git

$ ls ~/projects

project/
project-revised/
project-final/
project-final-FINAL/
project-final-real/
project-final-real-2/

Version control

  • Snapshots of your code over time
  • Parallel work — many people, different parts, at once
  • Rewind — go back to any snapshot

Git is not the only one. It is just the winner:
CVS · Subversion · Mercurial · TFS · Perforce

Centralised vs distributed

Centralised repository offline update commit working copy workstation #1 update commit working copy workstation #2 update commit working copy workstation #3 everyone is stuck Distributed repository offline pull push repository update commit working copy workstation #1 pull push repository update commit working copy workstation #2 pull push repository update commit working copy workstation #3 everyone keeps committing

Git vs. GitHub vs. GitLab

Branches and commits

main develop feature/a feature/b v0.1 v0.2 v1.0

A commit is a point. A branch is just a name pointing at one.

How Git works

workspace .git __init__.py src stage local repository remote repository GitHub GitLab Bitbucket git add git reset git commit git push git fetch git pull

git pull = git fetch + git merge

Demo

Getting started

Create a new repositorygit init
Clone an existing onegit clone <url>
Check statusgit status
Check the loggit log
Stage one filegit add <file>
Stage everythinggit add .
Commit what is stagedgit commit -m 'message'
Create a branchgit branch <branch-name>
Switch to a branchgit checkout <branch-name>
Both at oncegit checkout -b <branch-name>

Atomic commits

Use meaningful commit messages

$ git log --oneline

a1b2c3d  created main loop & timing control
4d5e6f7  enabled config file parsing
8a9b0c1  misc bugfixes
2d3e4f5  code additions/edits
6a7b8c9  more code
0d1e2f3  here have code
4a5b6c7  aaaaaaaa
8d9e0f1  adkfjslkdfjsdklfj
2a3b4c5  my hands are typing words

Write for the person reading this in six months.
It will probably be you, and you will not remember.

Conflict resolution

  • Merging two branches may cause a conflict
  • A conflict means Git could not choose for you — it is not an error, it is a question
  • You resolve it by editing, not by commands alone
  • Then add the file to mark it resolved, and commit to finish the merge

What a conflict looks like

<<<<<<< HEAD (current change)
    readFileAndGenerating(file, split);
=======
    readFileAndGenerate(input, split);
>>>>>>> main (incoming change)

    i18nFileGenerate(output, options || null);

Delete the markers. Keep the code you want — maybe both, maybe neither. VS Code puts buttons on top of exactly this.

Demo

Merging and conflicts

Merge a branch into this onegit merge <branch>
Back out of a failed mergegit merge --abort
Mark a file resolvedgit add <file>
Finish the mergegit commit

$ git merge --abort

Nothing is lost — you are back exactly where you were before the merge.

Undoing changes

workspace stage local repo remote add commit push git restore <file> throw the edits away git restore --staged unstage, keep the edits git reset HEAD~1 uncommit, keep the edits git revert <commit> a new commit that undoes it

First question: where is the change right now?

Reset vs revert

git reset git revert A B C main main history rewritten · local only A B C undo C main history kept · safe after push

Reset what only you have. Revert what everyone has.

Demo

Undo commands

Throw away edits to a filegit restore <file>
Unstage a file, keep the editsgit restore --staged <file>
Fix the last commitgit commit --amend
Undo the last commit, keep the changesgit reset --soft HEAD~1
Undo a commit that is already pushedgit revert <commit>
Park your changes, get them back latergit stashgit stash pop

$ git reset --hard

The one undo you cannot undo: uncommitted changes are gone for good.

Thank you

Workflow — solo dev

Switch to main and get latestgit checkout maingit pull
Create a branch for your featuregit checkout -b feature/add-login
Make changesgit add .git commit -m "Add login form"
Merge when donegit checkout maingit merge feature/add-login
Clean upgit branch -d feature/add-login

Workflow — team, starting out

Switch to main and get latestgit checkout maingit pull
Create a branch for your featuregit checkout -b feature/add-login
Make changesgit add .git commit -m "Add login form"
Stay up to dategit fetch origingit rebase origin/main
Merge when donegit checkout maingit merge --ff-only feature/add-logingit push
Clean upgit branch -d feature/add-login

Workflow — team, in practice

Switch to main and get latestgit checkout maingit pull
Branch for your featuregit checkout -b feature/add-login
Make changesgit add .git commit -m "Add login form"
Sync with the remotegit push origin feature/add-login
Open a pull request
  • base branchmaincompare branchfeature/add-login
  • add title, description, context
Review & discuss
  • comment or suggest changes
  • testing
  • push fixes — they join the PR automatically
If main moved since you branchedgit fetch origingit merge origin/maingit push
Merge
  • Squash → one clean commit
  • Rebase → linear history
  • Normal merge → not recommended
Clean upgit branch -d feature/add-logingit push origin --delete feature/add-login

Resources

notes.txt today's handout — everything we covered, in one text file
learngitbranching.js.org branches and merges, drawn as you type them
ohmygit.org an actual game about learning Git
ohshitgit.com how to undo the thing you just did
w3schools.com/git a click-through tutorial from zero
git-scm.com/book Pro Git — the reference, free online

And the one already on your machine: git help <command>.

Questions?

$ open

presentations.hackenshaw.com/downloads/git-intro-fontys.html

These slides, with a link to notes.txt.

$ thanks for coming

Feedback

TERRIBLE BAD OKAY GOOD GREAT