Scaling API-first – The story of a global engineering organization
Git Presentation - Handout
1. Internals Usage Distribution Merging and Rebasing Extras
Git for mere mortals
https://github.com/Kelsin/git-presentation
Christopher Giroir
July 26th, 2011
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
1 Internals
Objects
Branches and Tags
2 Usage
Creating and Commiting
Inspection
3 Distribution
Advantages
Architecture
Command Examples
4 Merging and Rebasing
Merging
Rebase
5 Extras
Remotes
Misc
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Objects
Object Store
The git object store is a big black box and we like it that way.
It was written for speed and correctness
Comparisons
More information than CVS
More sane than SVN
More efficient than Mercurial
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Objects
Blobs
_^]XYZ[Blob
Blobs store file data (text or binary).
Labeled by SHA1. If the content changes, so does the SHA.
Christopher Giroir Git for mere mortals
2. Internals Usage Distribution Merging and Rebasing Extras
Objects
Trees
_^]XYZ[Blob
_^]XYZ[Tree //
??
_^]XYZ[Blob
Trees point to blobs
Represent the entire state of the working directory
Also labeled by SHA’s.
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Objects
Commits
onmlhijkCommit _^]XYZ[Blob
onmlhijkCommit
OO
//_^]XYZ[Tree
CC§§§§§§§§§§§§§
//_^]XYZ[Blob
Points to a tree object and parent commit(s)
Stores author and date information
Labeled by SHA’s.
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Objects
Important Distinction!
Commits link to entires TREES not DIFFS.
Diffs are not stored in git, they are computed.
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Branches and Tags
Pointers
Most other things in git are pointers to commits.
?>=<89:;a //
v3.0.1
?>=<89:;b //
bbbbbbbbb
Master
?=89:;c //?=89:;d //?=89:;e
SAVE-465
?=89:;g //
origin/SAVE-576
?=89:;h //?=89:;i
SAVE-576
Branches are just labels to commit SHA’s
Tags are just labels with meta information
Christopher Giroir Git for mere mortals
3. Internals Usage Distribution Merging and Rebasing Extras
Branches and Tags
Rewriting History
Rewriting a commit changes it’s SHA
Any commit after it changes as well
Most branch operations are quick and painless
Can shoot yourself in the foot and heal it back up again
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Creating and Commiting
Starting Out
Setting User Info
git config --global user.name Christopher Giroir
git config --global user.email kelsin@valefor.com
Completely New Repo
cd dir-with-code
git init
Cloning any accessible repo
git clone git@github.com:Kelsin/configs.git
cd configs
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Creating and Commiting
Commits
Initial Commit
git add .
git commit -m Initial Commit
Editing some files
git add edited-file.rb
git commit -m Improved everything
Adding to previous commit
git add edited-file.rb
git commit --amend
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Inspection
Status and Diffs
Normal Status
git status
What’s current changed?
git diff
Changes from only one file
git diff example-file.rb
Changes between branches
git diff master
Christopher Giroir Git for mere mortals
4. Internals Usage Distribution Merging and Rebasing Extras
Inspection
Show and Log
Show current commit
git show
Show some other commit
git show 234ab32
Show log of recent commits
git log
Show pretty log
git log --graph --decorate --oneline
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Inspection
Refs and Objects
By commit id
git show 234ab32
By branch
git show master
By tag
git show v3.0.2
Relative
git show HEAD^
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Advantages
Distributed Source Control
Only difference from remote and local (normally): working
directories
Can function offline
Can “push” and “pull” from each other, as well as servers
Each of our computers serves as a backup of the server
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Architecture
Location Descriptions
Git has 4 locations where commits are stored
Remote Repo Any remote object store
Local Repo You local object store
Index A single spot to “stage” commits
Working Directory These are the files you are editing
Christopher Giroir Git for mere mortals
5. Internals Usage Distribution Merging and Rebasing Extras
Command Examples
Locations and Commands
Working
git add
Index
git commit
Local Repo
git push
##
git checkout
gg
Remote Repo
git fetch
cc
BC@A
git pull
OO
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Merging
Fast Forward Merging
git checkout master
git merge SAVE-234
?=89:;a //?=89:;b //?=89:;c //?=89:;d //?=89:;e
Master
SAVE-234
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Merging
Merging
git checkout master
git merge SAVE-234
?=89:;a //?=89:;b //
aaaaaaaaa
?=89:;c //?=89:;d //?=89:;e //?=89:;76540123j
Master
?=89:;g //?=89:;h //?=89:;i
@@¢¢¢¢¢¢¢¢¢
SAVE-234
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Merging
No Fast Forward Merging
git checkout master
git merge --no-ff SAVE-234
?=89:;a //?=89:;b
bbbbbbbbb
//?=89:;76540123f
Master
?=89:;c //?=89:;d //?=89:;e
@@
SAVE-234
Christopher Giroir Git for mere mortals
6. Internals Usage Distribution Merging and Rebasing Extras
Rebase
Rebase
git checkout SAVE-234
git rebase master
?=89:;a //?=89:;b //?=89:;c //?=89:;d //?=89:;e
wwooooooooooooooooo
Master
GFED@ABC?=89:;g //GFED@ABC?=89:;h //ONMLHIJKGFED@ABCi
SAVE-234
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Remotes
Remote Commands
Adding remote
git remote add origin
git@github.com:Kelsin/configs.git
Fetch all remote objects
git fetch
Show all branches
git branch -a
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Remotes
Pushing
Push objects from source to destination
git push origin source:destination
Removing remote branch
git push origin :destination
Setting upstream
git push -u origin SAVE-453
Force pushes
git push -f origin SAVE-453
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Remotes
Pulling
Pull in remote changes
git pull origin master
Special case
Executes:
git fetch
git merge
Merges in to your current branch
Christopher Giroir Git for mere mortals
7. Internals Usage Distribution Merging and Rebasing Extras
Misc
Git Config File
My git config is available at
https://github.com/Kelsin/configs/blob/master/.gitconfig
Push defaults
Commiter name and email
Color settings
Aliases
Christopher Giroir Git for mere mortals
Internals Usage Distribution Merging and Rebasing Extras
Misc
Some Tips
Always work on branches
When in doubt use GitX or gitk to see what you are doing
When in doubt save a new branch so you can always get back
Add the current git branch into your prompt
Explore with rebase and cleaning up code before final push
Christopher Giroir Git for mere mortals