Gitting it under(version) control
The Plan•   What is Version Control?•   Git Basics•   Workflows•   Other fun stuff
What is Version        Control?• It’s exactly what it sounds like.• Sometimes called SCM (source control  management).• On...
Different Approaches•   Distributed vs. Centralized•   Delta vs. Directed Acyclic Graph (DAG)
Muscle Memory FTW# 0. Get a copy of the repositorygit clone git@github.com:user/repo# 1. Before you do any work...git pull...
Setting Up•     Download Installer    from http://git-scm.com/•    A few commands you    want to run right away:git config...
git commands•   Porcelain (high-level, “nice” for users)     git−add          git−commit   git−log        git−reset     gi...
Getting a repository•   One project, one .git (hidden) directory•   Start it from initial code locally                    ...
Local Repository OperationsWorking Directory   Staging Area   Repository “Working Tree”       “Index”      “Database”
Local Repository OperationsReadme.md* AwesomeProj *AwesomeProjdoes x and y.     Working Directory   Staging Area   Reposit...
Local Repository Operations  git add Readme.md                    Readme.md                    * AwesomeProj *            ...
Local Repository Operations  git add Readme.md                  git commit -m “commit mess”                               ...
Local Repository Operations                     git status     # On branch master     nothing to commit (working directory...
Local Repository Operations                             git status# On branch master# Changes not staged for commit:#   (u...
Local Repository Operations                         git status            git add Readme.md            # On branch master ...
Local Repository Operations                                      git commit -m “new feat”Readme.md                        ...
Local Repository Operations                     git status     # On branch master     nothing to commit (working directory...
Local Repository Operations         git add Readme.md                  git commit -m “commit mess”       Working Directory...
Local Repository Operations         git add Readme.md                  git commit -m “commit mess”                 X      ...
Branching & Merging•       What is a commit?    •    An object that points to trees/blobs in the .git directory    •    ad...
Branching & Merging•       What is a branch?    •    A pointer to a commit    •    logically, a separate concern in develo...
Branching & Merging    git commit -m “new feature”   ad9e3b      master       HEAD
Branching & Merging    git commit -m “new feature”   ad9e3b      master       HEAD
Branching & Merging    git commit -m “new feature”   ad9e3b   c88da7       master      HEAD
Branching & Merging                git branch issue8       ad9e3b                       master       HEAD       c88da7    ...
Branching & Merging                      git checkout issue8             ad9e3b                             master        ...
Branching & Merging# Make changes in editor, add changes to staging...git commit -m “redoing data structure”          ad9e...
Branching & Merging                      git checkout master             ad9e3b                                           ...
Branching & Merging       # Making more commits...   ad9e3b   c88da7        b9711f       issue8   f327e3                  ...
Branching & Merging# Now we need to get that fix for issue8 into master                  git merge issue8         ad9e3b  ...
Branching & Merging# Now we need to get that fix for issue8 into master                  git merge issue8 ad9e3b c88da7   ...
Sharing Code with             Remotes•       How do we share code with others?    •    git has stored URLs with aliases ca...
Remotes  •    By default, you always get one remote      when you clone, it is called “origin”  •     You can add more rem...
Using Remotesf327e3   master   HEAD
Using Remotes                     origin/master HEAD           f327e3                     master      HEADNow what if some...
Using Remotes                             master      HEADf327e3            3602ea           origin/master HEAD  This new ...
Using Remotes                         master      HEADf327e3          3602ea        origin/master HEAD         Now we have...
Using Remotes                            master      HEADf327e3           3602ea           origin/master HEAD How do we co...
Using Remotes              f327e3           master      HEAD             3602ea            origin/master HEAD             ...
Using Remotes               f327e3          master     HEAD              3602ea          origin/master HEAD               ...
Creating a Tracking          Branch•    If theres a new branch available on the    server and you want to participate in  ...
Pushing Changes•     Pushing can only be fast-forward commits,    this prevents you from abandoning    someone else’s chan...
Inspection and History•   git log --pretty=oneline --graph•   git diff [--cached]•   git status•   git remote show
Other Neat Tricks•   git add -u•   git stash•   git push -u origin master•   git push origin :badfeature•   git log featur...
Gotchas•   Binary files? Images? Videos? Databases?•   Detached HEAD•   non fast-forward•   git rm / git mv
Workflows
master is always         deployable•    Good general convention•    Keep actual development on ‘topic’    branches•     Wh...
master is always         deployable•    Good general convention•    Keep actual development on ‘topic’    branches•     Wh...
Local Development•       Common pieces to share:    •    unit tests    •    graphics    •    utility scripts•    Keep your...
.gitignore•     A file with filename patterns that you    don’t want to become part of your index    (nor your commits)•  ...
Mark versions with              Tags•       Tags are like “unmovable” branches•     This makes it a good bookmark to keep ...
Github Pull-Requests•    You don’t always have access to push if    you are contributing to open source, send a    pull-re...
Github Forks•    Just a “clone” that you initialize on a    remote server, instead of on your machine•    Holds a little b...
IDE and GUI Tools      Xcode
IDE and GUI Tools      Eclipse Egit
IDE and GUI Tools•       Platform Specific Tools    •     Tower for Mac    •     TortoiseGit for Windows•       Cross Plat...
Feeling more “at home”•   Bash Completion•   Merge Tool•   Commit Message Editor•   Aliases
Left as an exercise for      the readers•   cherry-picking•   “rewriting” history•   git-svn•   git modules•   environment...
Resources•    Git Cheatsheet (visual)    http://ndpsoftware.com/git-cheatsheet.html•    Github Help    http://help.github....
Erasing anyPreconceived Notions• If you came from svn, you have a separate  understanding of version control• commits happ...
Why distributed over   centralized?• Offline development• Easy to do experimental work• ... code review• ... other workflo...
Upcoming SlideShare
Loading in...5
×

Gitting It Under (Version) Control

597

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
597
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Gitting It Under (Version) Control

  1. 1. Gitting it under(version) control
  2. 2. The Plan• What is Version Control?• Git Basics• Workflows• Other fun stuff
  3. 3. What is Version Control?• It’s exactly what it sounds like.• Sometimes called SCM (source control management).• One codebase, one project.• Lots of people changing it to get things done at the same time. • How do we track those changes? • How do we make sure I don’t step over your toes?
  4. 4. Different Approaches• Distributed vs. Centralized• Delta vs. Directed Acyclic Graph (DAG)
  5. 5. Muscle Memory FTW# 0. Get a copy of the repositorygit clone git@github.com:user/repo# 1. Before you do any work...git pull# 2. Do work, edit files, but when you want to share it...git commit -am "commit message"# 3. Now send it to everyone elsegit push# GOTO Step 1.
  6. 6. Setting Up• Download Installer from http://git-scm.com/• A few commands you want to run right away:git config --global user.name "Ankur Oberoi"git config --global user.email "aoberoi@gmail.com"git config --global color.ui true
  7. 7. git commands• Porcelain (high-level, “nice” for users) git−add git−commit git−log git−reset git−branch git−diff git−mv git−status git−checkout git−fetch git−merge git−push git−clone git-init git−pull git−tag git-archive git-bisect git-bundle git-cherry-pick git-citool git-clean git-describe git-format-patch ... ... ... ...• Plumbing (low-level, deals with database & filesystem) git−apply git−checkout−index git−commit−tree git−hash −object git−index−pack git−init−db git−merge−index git−mktag git−mktree git−pack−objects git−prune−packed git−read−tree git−repo−config git−unpack−objects git−update−index git−write−tree git−cat−file git−describe git−diff−index git−diff−files git-diff-index git-diff-tree git-for-each-ref git-ls-files ... ... ... ...
  8. 8. Getting a repository• One project, one .git (hidden) directory• Start it from initial code locally • git init• Clone it from somewhere else • git clone URL
  9. 9. Local Repository OperationsWorking Directory Staging Area Repository “Working Tree” “Index” “Database”
  10. 10. Local Repository OperationsReadme.md* AwesomeProj *AwesomeProjdoes x and y. Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  11. 11. Local Repository Operations git add Readme.md Readme.md * AwesomeProj * AwesomeProj does x and y.Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  12. 12. Local Repository Operations git add Readme.md git commit -m “commit mess” Readme.md * AwesomeProj * AwesomeProj does x and y.Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  13. 13. Local Repository Operations git status # On branch master nothing to commit (working directory clean)Readme.md Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  14. 14. Local Repository Operations git status# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# Readme.md (use "git checkout -- <file>..." to discard changes in working Readme.mddirectory)# * AwesomeProj *# modified: Readme.md#no changes added to commit (use "git add" and/or "git commit -a") AwesomeProj does x, y and z. Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  15. 15. Local Repository Operations git status git add Readme.md # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) Readme.mdReadme.md # # modified: Readme.md # * AwesomeProj * AwesomeProj does x, y and z. Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  16. 16. Local Repository Operations git commit -m “new feat”Readme.md Readme.md [master ad9e3bf] new feat1 files changed, 1 insertions(+), 1 deletions(-) * AwesomeProj * AwesomeProj does x, y and z. Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  17. 17. Local Repository Operations git status # On branch master nothing to commit (working directory clean)Readme.md Working Directory Staging Area Repository “Working Tree” “Index” “Database”
  18. 18. Local Repository Operations git add Readme.md git commit -m “commit mess” Working Directory Staging Area Repository “Working Tree” “Index” “Database”git reset HEAD Readme.md
  19. 19. Local Repository Operations git add Readme.md git commit -m “commit mess” X git checkout -- Readme.md X git checkout HEAD -- Readme.md git reset --hard [HEAD] Working Directory Staging Area Repository “Working Tree” “Index” “Database”git reset HEAD Readme.md
  20. 20. Branching & Merging• What is a commit? • An object that points to trees/blobs in the .git directory • addressed by a SHA1 hash (ad9e3b) • contains info about author/commiter • also contains pointers to “parent commits” ad9e3b
  21. 21. Branching & Merging• What is a branch? • A pointer to a commit • logically, a separate concern in development• What is HEAD? • A pointer to a branch ad9e3b master HEAD • Usually represents whats in the WT *now*
  22. 22. Branching & Merging git commit -m “new feature” ad9e3b master HEAD
  23. 23. Branching & Merging git commit -m “new feature” ad9e3b master HEAD
  24. 24. Branching & Merging git commit -m “new feature” ad9e3b c88da7 master HEAD
  25. 25. Branching & Merging git branch issue8 ad9e3b master HEAD c88da7 issue8New branch for a separate logical concern
  26. 26. Branching & Merging git checkout issue8 ad9e3b master c88da7 issue8 HEADCheckout actually changes the files in your Working Tree
  27. 27. Branching & Merging# Make changes in editor, add changes to staging...git commit -m “redoing data structure” ad9e3b master c88da7 b9711f issue8 HEADNew commits have pointers to parent(s)
  28. 28. Branching & Merging git checkout master ad9e3b master HEAD c88da7 b9711f issue8Checkout actually changes the files in your Working Tree
  29. 29. Branching & Merging # Making more commits... ad9e3b c88da7 b9711f issue8 f327e3 master HEAD
  30. 30. Branching & Merging# Now we need to get that fix for issue8 into master git merge issue8 ad9e3b c88da7 b9711f issue8 f327e3 master HEAD
  31. 31. Branching & Merging# Now we need to get that fix for issue8 into master git merge issue8 ad9e3b c88da7 b9711f issue8 f327e3 This is known as a “merge commit” a77106 master HEAD
  32. 32. Sharing Code with Remotes• How do we share code with others? • git has stored URLs with aliases called “remotes” • a few operations to move code from your local repository to and from a remote • you get local copies of remote branches in your repo • *branch related operations happen locally
  33. 33. Remotes • By default, you always get one remote when you clone, it is called “origin” • You can add more remotes using:git remote add github git@github.com:aoberoi/testproj.git This URL can be ssh:// (most common), http://, https://, git://, or even file:///
  34. 34. Using Remotesf327e3 master HEAD
  35. 35. Using Remotes origin/master HEAD f327e3 master HEADNow what if someone else adds commits to origin?
  36. 36. Using Remotes master HEADf327e3 3602ea origin/master HEAD This new commit only exists on the server... git fetch origin
  37. 37. Using Remotes master HEADf327e3 3602ea origin/master HEAD Now we have a local copy.How do we continue working where the latest from the server left off?
  38. 38. Using Remotes master HEADf327e3 3602ea origin/master HEAD How do we continue working where the latest from the server left off? git merge origin/master
  39. 39. Using Remotes f327e3 master HEAD 3602ea origin/master HEAD git merge origin/masterFast-forward merge: my commit is in the history of what is to be merged in
  40. 40. Using Remotes f327e3 master HEAD 3602ea origin/master HEAD Pull is a shortcut: git pull = git fetch + git mergeNo need to specify the argument to merge if you are on a “tracking branch”
  41. 41. Creating a Tracking Branch• If theres a new branch available on the server and you want to participate in developing on that “logical concern” git checkout -t origin/next-big-thing You are not checking out a remote branch, you are creating a new one
  42. 42. Pushing Changes• Pushing can only be fast-forward commits, this prevents you from abandoning someone else’s changes git push origin master
  43. 43. Inspection and History• git log --pretty=oneline --graph• git diff [--cached]• git status• git remote show
  44. 44. Other Neat Tricks• git add -u• git stash• git push -u origin master• git push origin :badfeature• git log feature ^master• git add -i
  45. 45. Gotchas• Binary files? Images? Videos? Databases?• Detached HEAD• non fast-forward• git rm / git mv
  46. 46. Workflows
  47. 47. master is always deployable• Good general convention• Keep actual development on ‘topic’ branches• When topic is complete and code is tested → merge (or pull request) master issue-8
  48. 48. master is always deployable• Good general convention• Keep actual development on ‘topic’ branches• When topic is complete and code is tested → merge (or pull request) master issue-8
  49. 49. Local Development• Common pieces to share: • unit tests • graphics • utility scripts• Keep your own preferences on your local machine and don’t commit them
  50. 50. .gitignore• A file with filename patterns that you don’t want to become part of your index (nor your commits)• See samples for all types of platforms: https://github.com/github/gitignore/
  51. 51. Mark versions with Tags• Tags are like “unmovable” branches• This makes it a good bookmark to keep track of code that was released • “I need to patch a bug that my customer sees, but what code did I deploy?”
  52. 52. Github Pull-Requests• You don’t always have access to push if you are contributing to open source, send a pull-request• Pull-requests work from branch to branch • use this for collaborative development • code review• http://www.confreaks.com/videos/706-
  53. 53. Github Forks• Just a “clone” that you initialize on a remote server, instead of on your machine• Holds a little bit of history for the project so you can track relationships in a “network”
  54. 54. IDE and GUI Tools Xcode
  55. 55. IDE and GUI Tools Eclipse Egit
  56. 56. IDE and GUI Tools• Platform Specific Tools • Tower for Mac • TortoiseGit for Windows• Cross Platform • gitk
  57. 57. Feeling more “at home”• Bash Completion• Merge Tool• Commit Message Editor• Aliases
  58. 58. Left as an exercise for the readers• cherry-picking• “rewriting” history• git-svn• git modules• environment variables and $GIT_DIR• Treeishes by ‘x’spec• Writing your own git hooks
  59. 59. Resources• Git Cheatsheet (visual) http://ndpsoftware.com/git-cheatsheet.html• Github Help http://help.github.com/git-cheat-sheets/• Pro Git http://progit.org/book/• Git Community Book http://book.git-scm.com/• Google Tech Talk with Linus Torvalds
  60. 60. Erasing anyPreconceived Notions• If you came from svn, you have a separate understanding of version control• commits happen locally (actually almost everything happens locally)• version numbers are not monotonically increasing• branches are not global (pollution of namespace?)• merging is (relatively) cheap, can be done
  61. 61. Why distributed over centralized?• Offline development• Easy to do experimental work• ... code review• ... other workflowy specific purposes (flexibility)• “commit access” politics are avoided• backups
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×