git for cowards
TRANSCRIPT
Tim Duckett 2011
Git for cowards
• I’m a coward - originally an electronics engineer - now freelance: iOS, Ruby, Rails etc - associate lecturer at Sheffield Hallam University - [email protected] / @timd / adoptioncurve.net
• This is Git - Source code management - Collaboration on shared code - Backup of virtually anything binary
Tim Duckett 2011
What is source control?
• “the management of changes to documents, programs, and other information stored as computer files”
• Also a means of securely storing documents, source code etc
• Also a means of managing changes made by a team of people
Tim Duckett 2011
Why would you use Git?
• Source code management • Collaboration on shared code • Backup of virtually anything binary • It’s social!
Tim Duckett 2011
Git
• A distributed version control system • Originally developed by Linus Torvalds for Linux
kernel development • Key features
- Fast! - Each working directory is a fully-fledged repository - Not dependent on a central server or network access - Open source, and free
• Extensively used in the web and open source community
• Making inroads into corporate life
Tim Duckett 2011
Available for...
• Windows - http://msysgit.googlecode.com/files/Git-1.7.4-preview20110204.exe
• Mac - http://git-osx-installer.googlecode.com/files/git-1.7.4.1-x86_64-leopard.dmg
• Linux: - apt-get install git-core
Tim Duckett 2011
Basic Git concepts
• Places a file can be: - the working directory
the location where you actually work with the files
- the index a “staging area” for files that will be committed to the repository
- the local repository the local database containing all the files in the project that have been committed from the index
- the remote repository a copy of the local repository on a remote system (e.g. Github.com)
Tim Duckett 2011
Basic Git concepts
Tim Duckett 2011
Statuses a file can have
• Untracked - Only in the working directory
• Tracked - Has been added to the index
• Staged - Has been committed to the
repository
• Ignored - Git won’t do anything with it - Has to be listed in
the .gitignore file
Tim Duckett 2011
The working directory
• The place where the work takes place - this is the “normal” working directory,
and is not under Git control - files can be created, modified, and
deleted at will by you - files created and amended in here will
not be touched by Git until they are staged - Git can work out which files haven’t
been staged, but it won’t do it for you
Tim Duckett 2011
The index
• Files which are awaiting commitment to the repository - Only files which are staged in the index
will be committed - Files have to be explicitly copied to the
index by the git add command - Once a file is in the index, it’s
independent from the copy in the working directory
- Changes to the file in the working directory won’t affect the copy in the index
Tim Duckett 2011
The repository
• The database of files which comprise the Git project - Files are added to the repository by
committing the index - Files in the index are copied into the
respository
Tim Duckett 2011
The remote repository
• A copy of the local repository which is stored elsewhere - Can be on another system, or a hosted
service like Github - Commits are pushed from local to
remote - You need to explicitly configure a remote
repository - by default, everything is local
• Git can work completely offline - you don’t necessarily need a remote
repository - you don’t need a network connection
Tim Duckett 2011
(Very) basic Git workflow
Tim Duckett 2011
Git objects - the blob
• Binary Large Object • The contents of a file • Git doesn’t care what is in the file • Each blob has a SHA-1 hash calculated on it
- the SHA1 hash is a 40-digit checksum of the file - you can refer to the hash by the first 4 or 5 digitis
Tim Duckett 2011
Git objects - the tree
• a directory of blobs and commits - also known by a SHA1 hash
Tim Duckett 2011
Git objects - the commit
• A pointer to a single tree • Acts as a “snapshot” of the state of
the project. • Contains information about
- the tree it points to - any parent commits it has - the author - the committer - a commit message
Tim Duckett 2011
Git objects - the tag
• A fixed and human-readablelabel for a commit
• Contains - a tag name - the name of the tagger - a message (often the PGP signature
of the tagger)
Tim Duckett 2011
Commits are snapshots over time
Tim Duckett 2011
Commits are snapshots over time- Git “fills-in” the blanks by referring to previous files if they’re
unchanged...
Tim Duckett 2011
Commits are snapshots over time- So each commit is a complete picture of the state of the project
Tim Duckett 2011
Generic Git workflow
Tim Duckett 2011
Initialising the repository
git init
• Creates the repository • “Adopts” the directory as a Git project
Initialized empty repository in /dir/.git/
Tim Duckett 2011
Setting up the config
• Git needs to know your name and email address to properly credit your commits
• Set globally using the git config command
git config --global user.name “TimD” git config --global user.email “[email protected]”
• You can override on a per-repo basis by dropping the --global switch
Tim Duckett 2011
Checking files out of the remote repository
• Cloning an existing repo: git clone [repo]
• Adding a remote repo: git remote add [shortname] [url]
• Fetching from a remote: git fetch [remote name]
• Cloning a remote repo: git clone [repo] [branchname]
Tim Duckett 2011
Adding files to the index
git add [filename]
or git add [filename] [filename] ...
or git add .
Tim Duckett 2011
Check the status of files in the index
•git status
Tim Duckett 2011
Adding files to the index
•git add .
Tim Duckett 2011
Committing files to the repository
•git commit -am “[message]”
Tim Duckett 2011
Pushing files to the remote repository
•git push [repo name] [branch name]
Tim Duckett 2011
• Each commit is a snapshot of the project
Branching
Tim Duckett 2011
Branching
• A branch is a lightweight movable pointer to a particular commit
• The default branch is called master
• Branching is very cheap... • Branch early, and branch often!
Tim Duckett 2011
Branching
• As you make commits, the branch pointer moves forward automatically
Tim Duckett 2011
Branching
• Simple scenario: - You’re working on a project which is going to have a beta
release.
- You make 3 commits, tag the 3rd commit as “beta”, and then deploy:
Tim Duckett 2011
Branching
• Simple scenario, continued: - Then after releasing, you create a new branch called “v1.0”:
git branch v1.0
Tim Duckett 2011
Branching
• Simple scenario, continued: - Switch to it, start working and make a commit:
- git checkout v1.0
Tim Duckett 2011
• Simple scenario, continued: - The master branch stays where it is:
- and the v1.0 branch moves ahead independently
Branching
Tim Duckett 2011
Branching
• Simple scenario, continued: - Git keeps track of your current branch and commit with a label
called “HEAD”
Tim Duckett 2011
Branching
• Simple scenario, continued: - You now need to make a tweak to the master branch that’s
been deployed:
Tim Duckett 2011
Branching
• Simple scenario, continued: - You switch back to the master branch:
- git checkout master
Tim Duckett 2011
• Simple scenario, continued: - Make the fix - commit it - then deploy:
- git add . - git commit -am “Fixed problem”
Branching
Tim Duckett 2011
• Simple scenario, continued: - Then switch back to the v1.0 branch and carry on working
with it:
- git checkout v1.0
Branching
Tim Duckett 2011
• Simple scenario, continued: - Then switch back to the v1.0 branch and carry on working
with it:
- git add . - git commit -am “More development”
Branching
Tim Duckett 2011
• Key points: - Changes in the branches are independent - Switching from v1.0 to master ‘resets’ the
working directory
- new files you’ve added to v1.0will ‘disappear’
- files that are in master but not in v1.0 will ‘reappear’ - you can switch between branches at will - you can create a new branch from any existing branch
Branching
Tim Duckett 2011
OMFG something went wrong!!!
• Don’t panic! • You can rollback changes with git reset
Tim Duckett 2011
OMFG something went wrong!!!
• git reset --hard C3
Tim Duckett 2011
• Eventually, you’ll want to bring changes in your working branch back into the the master
Merging
Tim Duckett 2011
Merging
• You merge v1.0 into master
- git checkout master - git merge v1.0
Tim Duckett 2011
A much better workflow
Time
release branches masterdevelop hotfixesfeature
branches
Feature for future
release
Tag 1.0
Major feature for
next release
From this point on, “next release”
means the release after 1.0
Severe bug fixed for
production: hotfix 0.2
Bugfixes from rel. branch
may be continuously merged back into develop
Tag 0.1
Tag 0.2
Incorporate bugfix in develop
Only bugfixes!
Start of release
branch for 1.0
Author: Vincent Driessen Original blog post: http://nvie.com/archives/323 License: Creative Commons
- extends core git with custom workflows - http://nvie.com/posts/a-successful-git-
branching-model/
- https://github.com/nvie/gitflow
Tim Duckett 2011
Merging
• Git will show conflicts between files
everything abovethe line is from HEAD
everything belowthe line is from branch2
this doesn’t clash
Tim Duckett 2011
Merging
• You need to manually select the right modifications, then commit the result
everything abovethe line is from HEAD
everything belowthe line is from branch2
this doesn’t clash
Tim Duckett 2011
• github.com - basic accounts are free - Micro accounts are $7pm
(gives you 5 privaterepositories)
- excellent showcase foryour code!
Git hosting at Github.com
Tim Duckett 2011
Git references
• Git Crash Course - http://gitref.org/index.html
• The Git Community Book - http://book.git-scm.com/
• Zack Rusin’s Cheat Sheet - http://zrusin.blogspot.com/2007/09/git-cheat-sheet.html
• Git for Web Designers - http://www.webdesignerdepot.com/2009/03/intro-to-git-for-web-designers/