Git All hail the octo-cat Sean Dague http://dague.net
The Bad (not so) Old Days
Selective SCM History SCCS Original source code control system Local only, single file at a time RCS GNU implementation of local SCM (same basic limitations as SCCS) CVS Wrapped RCS with concept “changesets” Provided a network server
Selective SCM History SVN “CVS done right” global repository versioning support for file renames Bitkeeper Not open source Designed around Linux Kernel workflow license revoked “reverse engineering” spat Then the anti market kicked in... git, hg, monotone, bzr
The Concurency Issue Master Tree Sean Joe
The Locking Solution Master Tree Sean Joe
The Opportunistic Locking Solution Master Tree Sean Joe
The Keep Everything Solution Master Tree Sean Joe
The Keep Everything Solution Master Tree Sean Keep this in mind, we'll come back to it later Joe
Content Addressable Storage /home/sdague/foo.txt Inode: 569 Some file
Content Addressable Storage /home/sdague/foo.txt SHA1: f4ed8388... Some file Inode: 569
Content Addressable Storage cp foo.txt foo2.txt /home/sdague/foo.txt /home/sdague/foo2.txt SHA1: f4ed8388... Some file Inode: 569
Content Addressable Storage It doesn't matter where the file is, if it's the same contents /usr/local/file.txt /home/sdague/foo.txt SHA1: f4ed8388... SHA1: f4ed8388... Inode: 569 Some file
CAS + Directed Graph Courtesy http://learn.github.com
Git Truisms Changes in local content addressable storage Uses cryptographically strong SHA1 hash to identify changes Each change includes a reference to the parent change(s) that it is based Every repository is an equal clone Git makes it really hard to loose data
Git Basic Commands Network/Repo init – create a new repository clone – make a local copy of a tree pull – update tree from remote tree push – push changes to remote tree Making Changes add – add a file to be versioned commit – add changes rm – remove a file mv – move a file (often not needed)
Git Basic Commands Inspecting diff - show what's changed log - show a time linear log of changes status - show what would be commited on a commit Oh Crap! Undo! Undo! Undo! reset --hard - get back to the last commit checkout -- $file - reset a single file from commit revert $version - revert a previous commit
Simple git workflow for 1 git clone $repo make changes git add $file1 $file2 … git commit -a rinse, wash, repeat git push Demo time!
Branching Quick in tree branching is one of the git selling points Branching Commands branch - displays current branches branch $name - creates a new branch checkout $name - switches to a branch You'll get yelled at if you try to switch branches with outstanding changes.
Merging Most of the time, it just happens and you don't realize it Commands that merge pull - automatically generates a merge commit if needed merge - for manual operations, you rarely need this
A note on diagrams to come Arrows point in flow direction instead of towards parents Different colors are meant to represent different hash values Courtesy http://learn.github.com
Branching & Merging Master Tree Ok, we're keeping everything but now we have a new problem. Which change represents head? Bug 1 Bug 2
Rules of Trees A branch should only have 1 head. If you do something that would make a second head, git will try to stop you A changeset can have more than 1 parent A changeset doesn't need to have content in the traditional sense
Merging Master Tree Can't push from bug 1 branch because it would make 2 headed master Bug 1 Bug 2
Merging Master Tree git pull pulls down changes Bug 1
Merging Master Tree git pull pulls down changes automatically builds a merge commit Bug 1
Merging Master Tree now a git push works correctly Bug 1
Merging Master Tree And once we are all done we have identical trees Bug 1
Merging with conflicts Git will handle most merges automatically new files file renames multiple changes in files (as long as they aren't the same lines) combinations of the above If git can't automatically create the merge commit it will stop and dump out to an interactive mode. You manually create the fix up patch that lets things move forward
Fixing Screwups reset --hard revert checkout -- uncommitted files
Rebase... there be dragons... There is another merge like operations that deserves attention - rebase rewrites your changesets to be based on a different parent common usage for side patches private fixes you can't release (yet) working changes for upstream Important: do not publicly expose rebased trees, it makes life difficult for everyone
Rebase Master Tree Bug 1
Rebase Master Tree rebase first pulls in all the new changes from the target tree Bug 1
Rebase Master Tree rewrites your changes to be against a new parent Bug 1
Rebase Master Tree Destroys old changes Bug 1
Rebase Master Tree Destroys old changes Bug 1
Rebase Master Tree Rebase does not push changes. At this point you could push cleanly, or hold onto your changes for future rebasing Bug 1
Why you don't rebase public trees Master Tree Bug 1 Bob
Why you don't rebase public trees Master Tree Bug 1 Bob now can't make a sane merge tree with Bug 1, as it got rid of changes his merge was working with Bob
Why you don't rebase public trees Master Tree The fix sucks. First you have to purge out nodes in your tree (through creating a branch and rewinding). Then 1 rebase against the new base. Bug 1 Bob
Why you don't rebase public trees Master Tree Bug 1 Then another rebase against the newly pulled changes. Bob
Git with teams When using git with multiple people the typical pattern is that everyone has 1 or more public trees and pull is used to integrate other people's changes. push is really only used within a set of trees/branches that you own.
Git with teams Sean Master Tree Sean Working Joe Working
Git with teams Sean Master Tree changes are done, pull into master Sean Working Joe Working
Git with teams Sean Master Tree Joe is ready to get his changes in, so he syncs with master, and puts his tree up somewhere public Sean Working Joe Working
Git with teams Sean Master Tree Sean pulls in Joe's changes including the merge into public master Sean Working Joe Working
Git with teams Sean Master Tree Rinse, Wash Sean Working Joe Working
Git with teams Sean Master Tree Rinse, Wash Repeat Sean Working Joe Working
Setting up your own public repo If it's open source, consider using github.com if on ubuntu/debian use git-daemon-run uses a git custom protocol to get changes it is also possible to set up git server with vanilla http it's over 100x slower for large trees
Git Advantages O(1) runtime for access to changes Cryptographicly secure Branching/Merging make it really simple to try out new things Very space efficient Versions metadata as well as data (+x bits) No bloody line ending convert games Plays mostly well with others
Git Gui (Linux)
More Bits of Git Windows Support via TortoiseGit (like TortoiseSVN) complete enough now that git on windows is a real otpion git-svn bridge extendable via plugins git format-patch / send-email - for email workflows
Learning more on Git http://learn.github.com/ consult your man pages
Backup