Baby don't hurt me / don't hurt me / no more
What is GitBaby don’t hurt me / don’t hurt me / no more
View Slide
PageSo, what is Git?› Git is an application that provides Distributed Source Control.› It provides features similar to TFS in that it allows us to versioncontrol our code, tag these versions with metadata, stash,branch, merge, and a bunch of other bits and bobs.› The windows version is called msysgit(https://msysgit.github.io/)› Git can be used from either a command line interface (CLI), orfrom a few GUI-driven applications, such as GitExtensions(https://code.google.com/p/gitextensions/)/ Copyright ©2014 by Readify Pty Ltd2
PageDistributed Source Control› TFS is centralized, Git is distributed.› With TFS, when you create a workspace mapping from a repository toyour PC and retrieve the code, you are retrieving a snapshot of the latestversion of the code from the TFS server.› With Git, when you Clone a repository, you retrieve the entire repositoryfrom the nominated Origin, including all history.› This means with git that once you Clone a repository, you have a completeworking backup of the Origin.› Once you have Clonedyou can then work completely offline with therepository until you decide you want to sync with Origin./ Copyright ©2014 by Readify Pty Ltd3
PageLocal vs Remote› Work locally, synchronize remotely.› Local commands: add, commit, branch.› These commands let you modify your local gitrepository while you work.› Remote commands: pull, fetch, push.› These commands let you interact with your remoterepository and synchronize your work./ Copyright ©2014 by Readify Pty Ltd4
PageWorking locally/ Copyright ©2014 by Readify Pty Ltd5When working locally, files are inone of three states: committed,modified, or staged.Committed files are stored safelyin your local .git database.Modified files are files in yourworking directory you havechanged but not yet staged orcommitted.Staged files are modified filesthat have been marked to beadded to your next commit.
PageStaging visualised/ Copyright ©2014 by Readify Pty Ltd6
PageHow do I use it?› Lets assume we have a local repository cloned and we are ready to write some code.› First, create a branch with git checkout –b “branchname”. This will keep our changesisolated from Master until they have been reviewed in a PR.› Now code away!› When you have satisfied your need to code, you can see what changes you have madewith git statusor git log. Or just jump into GitExt› When you are ready to ‘check in’ your code, you can use git add –Ato “stage” all of yourchanges.› Once you are happy with your staged changes, commit them locally with git commit –m“Commit message”/ Copyright ©2014 by Readify Pty Ltd7
PagePush, pull, fetch!› Once you have been working for a little while you may want to integrateany changes in the master branch with your work.› You have a couple of options! Pull, or fetch.› When you pull, changes from the remote branch you pull from (Master inthis case) will be merged directly into your local workspace, and you mayhave to resolve changes.› When you fetch, you pull the changes down into your git repository, butthe working area is not updated. You can then then optionally mergethose changes into your branch if you wish.› Once you have finished merging, Push your changes up!/ Copyright ©2014 by Readify Pty Ltd8
PageUnder the hood› Git works in such a way that you only ever have a single copy of the code in your workingdirectory.› Commits store a snapshot (not a diff) of the file changes you have made in the gitdatabase, along with a reference to the previous commit.› Commits therefore end up forming a “directed acyclic graph”› When you check out a commit your working directory is rebuiltbased on the tree ofcommits from the one you have checked out.› This means when we branch, we still only have a single copy of the source locally, wedon’t need separate folders for separate branches.› When we check out a different branch, git will rebuild our working directory based on thetree of commits cascading down from that branch./ Copyright ©2014 by Readify Pty Ltd9
PageBranching› Branching in Git is a lightweight operation, as branches are just pointers toa commit.› A branch therefore is just a reference to a commit.› Branches come in a couple of flavours: local and remote. Local branchesare those on our machine that point to commits on our local repository.› Remote branches are of the form origin/{branch}, and they let us track thestate of our remote repo. Git automatically updates them every time wecommunicate with our remote for some reason./ Copyright ©2014 by Readify Pty Ltd10
PageBranching workflow› With Git you are always working in a branch.They are non-optional.› The central branch or ‘trunk’ is referred to as master. Masteris the defaultbranch created when the repository is initialized. When working youbranchfrom master, and when done you mergeback into master./ Copyright ©2014 by Readify Pty Ltd11
PageGlossary› Clone: akin to establishing a workspace mapping and pulling down the code in TFS, cloneretrieves an entirerepository for us.› Pull: akin to “Get Latest” in TFS, pull retrieves the latest changesetsfrom our upstream Remote repository andattempts to merge them in with our current work.› Fetch: similar to pull, this retrieves the latest changesetsfrom our upstream Remote repository, but does notattempt to merge them in with our current work.› Add: add marks files that you have changed as ready to commit. Only the files marked by ‘add’ will then becommitted on your next commit. Other modified files will not be committed.› Commit: saves the work you have staged into the git repository.› Push: akin to ‘check in’ in TFS, this pushes any changes you have made locally up to the remote repository. If ithas changes that you haven’t yet pulled down and integrated, this will fail./ Copyright ©2014 by Readify Pty Ltd12
PageFurther learnings› http://think-like-a-git.net/ (How git uses graphs totrack changes, and how we can use them to make ourlives easier)› http://rogerdudler.github.io/git-guide/ (A nice briefguide to working with git)› http://jake.ginnivan.net/explain-git-with-d3/ (Asandbox for playing with git concepts)/ Copyright ©2014 by Readify Pty Ltd13