Git training for sysadmins
(or, how to tolerate and even benefit from git)
Panu Kalliokoski, 16.1.2015
Preliminaries
- accepted languages for training?
- level of prior knowledge?
- what do you want to do with git?
- apologies for (lacking) preparations
- acknowledge your presence
git - the stupid content tracker
( man git )
Well, not stupid, actually quite smart (in an artificial
intelligence-ish way), but…
- very complicated (150 subcommands and counting)
- not very consistent user interface
- internals leak every now and then
- doesn't always follow the principle of least surprise
→ no way would this come so far if it wasn't for Linus' personal
support!
Git's good sides
That said…
- git's evolving really fast
- git can do some stuff that doesn't really exist in other version
control tools (like rebase over content moves, and git-rerere)
- git's got good cloud support (i.e. Github)
- git makes most easy stuff hard and hard stuff easy!
Centralised and distributed version control
- In centralised vc, history is only in one repository used by everyone
- In distributed vc, every working directory has its own history.
- Histories are copied (and changes merged) between repositories
- Some repos might be accessible for many people
push
& pull
models
but, in practice, usually one repository works as a
sync repo
in DVCS
too… so what's the difference?
- you can commit without publishing
- you can form independent sub-teams
How to get going with git
Pretty simple! Either:
- mkdir project; cd project; git init
- this initialises whichever directory as a git repository
- you can work, commit etc., but no push/pull because there's no
origin
repository
- Set up a public repository in Github or source02.csc.fi
- clone some existing project or create a new project
- make a local git repository by git clone url
Basic git workflow
- git pull --ff-only — bring your local working directory up-to-date
- edit files etc. in an ordinary way
- git add file.txt — tell git about a new file to be tracked
- make a commit out of a logical set of changes
- git commit -am
description
— commit all new work
- git add -p; git commit — commit only some part
- repeat from (2) if you don't want to publish your changes yet
- git pull --rebase — merge your work with what others have been doing
- git push — publish your work to your remote repository
- go home.
First hands-on
- create a Github account
- create a repository
- clone your repository
- test basic git workflow
Working with others
- The git pull --rebase is where the magic is
- It takes others' new work and reapplies your changes to this new
context
- Sometimes (usually, if you don't touch the same lines) it goes
smoothly…
- When it doesn't… there's a conflict and you have to specify the
result of merging
In case of conflicts, short version
- ensure you don't have anything else going on (more on this later)
- git pull — get the remote changes (you can explicitly tell to git
pull origin master)
- git diff — should show you the conflicts
- edit them away by hand (gedit some-file-that-conflicted)
- git commit -am — commit your resolution of the changes
- git push — test if the push is now clean
- If it isn't, retry pulling…
Making sure you don't have anything going on
- git status
- git rebase --abort — ensure you aren't in the middle of a rebase
- git diff --cached — ensure you don't have uncommitted changes:
- … if they aren't your changes: git reset HEAD
- … if they are your changes: git commit -m
explanation of the changes
- git checkout master (or whatever branch you were working on) —
ensure that you are where you want to be:
Second hands-on
- git clone https://(tunnus)@github.com/atehwa/git-koulutus.git
- cd git-koulutus
- oman tiedoston perustaminen, koko workflow
- muiden tiedostojen muokkaaminen + konfliktit
Git's internal objects
git pitää kirjaa myös ainakin:
- työhakemistosta (working directory)
- vientivarastosta (staged snapshot)
- välivarastoista (stashes)
- viittauksista (refs)
- muutamista metaviittauksista (esim. HEAD)
- vertaissäilöistä (remotes)
- vertaissäilöjen haaroista (remote branches)
- viittausvastaavuuksista (refspecs) säilöjen välillä
- haarojen oletus-mergekohteista (tracked branches)
- viittaushistorioista (reflogs)
- tallennetuista ratkaisuista (recorded resolutions)
- tylsistä tiedostoista (excludes)
- tapahtumaskripteistä (hooks)
- ja varmaan vielä jostain muustakin, mitä minä en tiedä.
And now we revert to URL-clicking…
How to use version control in sysadmin work?
at least three ways:
- prod-system local checkouts
- dev-system checkout and publish-automation script
- dev-system checkout, packaging and install-automation script
Should I use branches for differentiation between machines?
no, parameterization is almost always better
- it's cumbersome to track which changes are not to be merged back
- parameterization often requires some coding
- for config files, there are ok tools like cpp and m4
- for publishing scripts, you need some automation anyway (like
sh/make/ssh)