an introduction to git
DESCRIPTION
A 30 minutes introduction to GitTRANSCRIPT
introduction basics branches and tags local and remote repos the object store advanced operations end
An introduction to git
Olivier [email protected]
2012-12-06Adapted from "git concepts simplified" by Sitaram Chamarty,[email protected] http://sitaramc.github.com/gcs/
1 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
Why Git
Because
2 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
constructing a graph of commits
Git is all about constructing a nice local graph before sharingit
1 Do your mess locally2 Commit a nice chronology3 Rework the local graph til it’s beautiful4 Share with others in remote repos
3 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
GUI tools : gitk
4 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
GUI tools : Eclipse’s EGit
5 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
the 4 git object types
Git keeps all its data inside a special directory called .git atthe top level of your repository.Somewhere in there is what we will simply call the object store(if you’re not comfortable with that phrase, pretend it’s somesort of database).Git knows about 4 types of objects:
blob – each file that you add to the repo is turned into a blobobject.tree – each directory is turned into a tree object.Obviously, a tree object can contain other tree objects and blobobjects, just like a directory can contain other directories and files.commit – a commit is a snapshot of your working tree at a point intime, although it contains a lot of other information also.tag – we will see this type a bit later.
6 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
what is a SHA
A commit is uniquely identified by a 160-bit hex value (the ‘SHA’). Thisis computed from the tree, plus the following pieces of information:
the SHA of the parent commit(s) – every commit except the very first one in the repohas at least one parent commit that the change is based upon.the commit message – what you type in the editor when you committhe author name/email/timestamp the committer name/email/timestamp
(Actually, all 4 git objects types are identified by SHAs, but of coursethey’re computed differently for each object type. However, the SHAs ofthe other object types are not relevant to this discussion).In the end, as I said, it’s just a large, apparently random looking, number,which is actually a cryptographically-strong checksum. It’s usually writtenout as 40 hex digits.Humans are not expected to remember this number. For the purposes ofthis discussion, think of it as something similar to a memory addressreturned by malloc().It is also GLOBALLY unique! No commit in any repo anywhere in theworld will have the same SHA. (It’s not a mathematical impossibility, butjust so extremely improbable that we take it as fact. If you didn’tunderstand that, just take it on faith).An example SHA: a30236028b7ddd65f01321af42f904479eaff549
7 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
what is a repo
A repository (‘repo’) is a graph ofcommits.
In our figures, we represent SHAs with numbers for convenience.We also represent time going upward (bottom to top).
8 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
why are the arrows backward in your pictures?
So why are the arrows pointing backward?Well. . . every commit knows what its parent commit is (asdescribed in the “what is a SHA” section above).But it can’t know what its child commits are – they haven’tbeen made yet!Therefore a repo is like a single linked list. It cannot be adouble linked list – this is because any change to the contentswould change the SHA!
9 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
working locally and committing to a repo
Commiting to the repository (History) is a 2 step process :1 staging (only what’s necessary)2 committing (when it’s complete)
10 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
atomic commits in ‘git gui’
git gui allows you to stage individual lines or diff hunks, instead of allof a file’s hunks
11 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
branch
Traditionally, the top of a linked listhas a name.That name is a BRANCH name.
12 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
more than one branch
Remember we said a repo is aGRAPH?Specifically, more than one childnode may be pointing at the sameparent node.In this case, each ‘leaf node’ is abranch, and will have a name.
13 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
more than one parent commit (1/2)
Well we can’t keep creatingmore branches withouteventually merging themback.So let’s say feature X isnow tested enough to bemerged into the mainbranch, so you :
git merge feature_X
14 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
more than one parent commit (2/2)
At this point, it’s quite common todelete the feature branch, especiallyif you anticipate no more “large”changes.So you can run
git branch -d feature_X
15 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
current branch/checked out branch
There is a notion of a ‘currentlychecked out’ branch.This is denoted by a special refcalled HEAD.HEAD is a symbolic ref, whichpoints to the ‘current branch’.
16 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
committing
When you make a newcommit, the current branchmoves.Technically, whatever branchHEAD is pointing to willmove.
17 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
naming non-leaf nodes (1/2)
It’s not just ‘leaf’ nodes, but innernodes can also have names.Recall the result of mergingfeature_X earlier (see the morethan one parent commit section):At this point, you could leavefeature_X as it is forever.Or you could delete the branch (aswe showed in that section), inwhich case that label would simplydisappear.
18 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
naming non-leaf nodes (2/2)
You can also continue to developon the feature_X branch, furtherrefining it with a view to onceagain merging it at some laterpoint in time.
19 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
tags
More commonly, inner nodes areTAGS.
20 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
the difference between branches and tags
The main difference between abranch and a tag is branches move,tags don’t.When you make a commit with the“master” branch currently checkedout, master will move to point tothe new commit.
21 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
cloning and sharing
No more central repositories (unless by convention)
22 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
what is a git URL?
Git repos are accessed by providing a URL.There are typically 4 kinds of Git URLs:
ssh: likessh://[user@]host.xz[:port]/path/to/repo.git/http: likehttp[s]://host.xz[:port]/path/to/repo.git/git: likegit://host.xz[:port]/path/to/repo.git/
local dir: likefile:///full/path/to/reponame
(see man git-clone for all the allowed syntaxes for git URLs).
23 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
what is a “remote”?
A remote is a short name (like an alias) used to refer to aspecific git repository.Instead of always saying
git fetch git://sitaramc/gitolite
you can add that as a remote and use that short name insteadof the long URL.For convenience, a ‘remote’ called origin is automaticallycreated when you clone a repo, pointing to the repo youcloned from.
24 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
remote branches
Git is a distributedversion controlsystem.So when you clonesomeone’s repo, youget all the branches inthat one.
25 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
multiple remotes
You can haveseveral remotes.
26 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
fetching and merging from another repo (1/2)
Now let’s say Sita’s repo had acouple of new commits on itsmaster, and you run :
git fetch sitas-repo
27 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
fetching and merging from another repo (2/2)
Now you want to merge Sita’smaster branch into yours.Since your master does not haveany commits that Sita’s masterdoesn’t have (i.e., Sita’s master islike a superset of yours), running
git merge sitas-repo/master
will get you this:
28 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
the object store
Git stores all your data in an “object store”.There are 4 types of objects in this store: files (called “blobs”),trees (which are directories+files), commits, and tags.All objects are referenced by a 160-bit SHA.
29 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
what is a repo (again)
Earlier, we saw that a repo was a graph of commits.At the file system level, however, it is basically a directorycalled .git which looks somewhat like this
$ ls -al .gittotal 40drwxrwxr-x 7 sitaram sitaram 4096 Sep 14 18:54 ./drwx------ 3 sitaram sitaram 4096 Sep 14 18:54 ../drwxrwxr-x 2 sitaram sitaram 4096 Sep 14 18:54 branches/-rw-rw-r-- 1 sitaram sitaram 92 Sep 14 18:54 config-rw-rw-r-- 1 sitaram sitaram 73 Sep 14 18:54 description-rw-rw-r-- 1 sitaram sitaram 23 Sep 14 18:54 HEADdrwxrwxr-x 2 sitaram sitaram 4096 Sep 14 18:54 hooks/drwxrwxr-x 2 sitaram sitaram 4096 Sep 14 18:54 info/drwxrwxr-x 4 sitaram sitaram 4096 Sep 14 18:54 objects/drwxrwxr-x 4 sitaram sitaram 4096 Sep 14 18:54 refs/
30 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
objects and branches/tags (1/4)
The really, really important thing to understand is that theobject store doesn’t care where the commit came from or what“branch” it was part of when it entered the object store.Once it’s there, it’s there!
31 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
objects and branches/tags (2/4)
The starting point is before you did a fetch.
The next two figures are after git fetch sitas-repo and gitmerge sitas-repo/master, respectively.
32 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
objects and branches/tags (3/4)
git fetch sitas-repo
33 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
objects and branches/tags (4/4)
git merge sitas-repo/master
All you did was move a pointer from one node to another.
34 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
advanced operations
We’ll now show some advanced operations with the aid of thissame tree.
35 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
merging (1/4)
First, let’s do merging.
The merge you saw earlier was what is called a “fast-forward”merge, because your local master did not have any commitsthat the remote branch you were merging did not have.In practice, this is rare, especially on an active project withmany developers.So let’s see what that looks like.
36 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
merging (2/4)
The starting point is this:
37 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
merging (3/4)
Now, you made somechanges on your localmaster.Meanwhile, sitas-repohas had some changeswhich you got bydoing a fetch
38 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
merging (4/4)
When you
git merge sitas-repo/master
the end result will usuallylook like this:
39 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
cherry-pick (1/3)
A cherry-pick is not very commonly done – in well designedworkflows it should actually be rare.However, it’s a good way to illustrate an important concept ingit.We said before that a commit represents a certain set of filesand directories, but since most commits have only one parent,you can think of a commit as representing a set of changes too.
40 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
cherry-pick (2/3)
Let’s say one of yourcollaborators (this mythical“Sita” again!) made a wholebunch of changes to his copyof the repo.You don’t like most of thesechanges, except one specificchange which you would liketo bring in to your repo.The starting point is this:
41 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
cherry picking (3/3)
git cherry-pick sitas-repo/master~1
This results in the following commitgraph.
42 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
rebasing (1/5)
Instead of merging, let’s say you wanted to rebase yourcommits on top of Sita’s commits.First of all, what is rebasing?It’s basically transplanting a series of changes from one pointin the graph to another point.So if you guessed that a rebase was (in principle) a series ofcherry-picks, you’d be pretty close, at least from a conceptpoint.
43 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
rebasing (2/5)
So let’s use a similar example as inthe merge example before, butinstead of sitas-repo, the newcommits are in origin (which isthe “main” server for this project).You had your own commits, andyou did a git fetch originwhich brought in the latest commitsfrom “origin”, so it looks like:
44 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
rebasing (3/5)
Now, instead of merging origin/masterinto your local master, you want to rebaseyour commits on top of origin/master.
That is, you want to pretend your localchanges were made after commit 13 onthe origin.
So you run
git rebase origin/master
and this is the result:
45 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
rebasing (4/5)
Unlike cherry-pick, a rebase is quite often done in real life.Rebase also has some other forms.This form is one, but the most common is when a developerwants to re-arrange his own local commits in a more logicalsequence before publishing/pushing them.
46 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
rebasing (5/5)
I often do the eqvt of changing this:
where “22delta” is a minor fixup to “22”, into
using
git rebase -i
47 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
non fast-forward merge (1/2)
Here’s the starting point :
48 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
non fast-forward merge (2/2)
git merge --no-ff feature_X
49 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
“A successful Git branching model”
50 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
Resources
git concepts simplifiedA Visual Git Referencegit cheat sheetAn introduction to git-svn for Subversion/SVK users anddeserterspro gitmémento git à 100%A successful Git branching model
51 / 52
introduction basics branches and tags local and remote repos the object store advanced operations end
copyrights + license
©Copyright Sitaram Chamarty, [email protected] (for partsoriginally in git concepts simplified)©Copyright Mark Lodato, [email protected] (for parts from AVisual Git Reference)©Copyright Vincent Driessen (for parts from A successful Gitbranching model)©Copyright 2012 Olivier Berger + Institut Mines Télécom
This documentation is provided under a Creative CommonsAttribution-NonCommercial-ShareAlike 3.0.
Made with org-mode (under emacs, to generate beamer slides with inlinedot graphs)
52 / 52