Git: a brief                                  introduction                     Randal L. Schwartz, merlyn@stonehenge.com  ...
Overview                     •   Key concepts of git                     •   Using git by yourself                     •  ...
Key concepts                     •   Manages trees, not files                     •   Stores entire history for a commit   ...
The moving parts                     •   Stored in “.git”:                         • Object repository                    ...
Commits                     •   Unit of work                     •   Usually has a parent                         • Two or...
Trees                     • SHA1 of each file in a directory                     • SHA1 of each directory                  ...
Making commits                     • Changes made to work dir                     • Added to index with “git add”         ...
The SHA1 is king                     •   SHA1 of commit defines:                         • Metadata of commit              ...
Naming commits                     •   SHA1 (can often be abbreviated)                     •   HEAD, branch-name, tag     ...
Branches                     • Initial branch is “master” (name not special)                     • Fork the current branch...
Multiple branches                     •   Start a second topic based on original master:                         git check...
Combining the work                     •   Merge the work back in to master:                         git checkout master  ...
Really merging                     •   Now to bring topic2 back into the mix:                         git checkout master ...
Linear history                     •   We can “rebase” commits:                         A B C D E F (master)              ...
Rebase vs merge                     • Both can have conflicts                     • Merge makes a bushy tree               ...
Using git yourself                     •   Create the repo at your top-level dir:                         git init        ...
Work a bit                     •   Edit some files                     •   Check the status: git status                    ...
What’s changed?                     •   git diff                       •    Diff between index and working tree           ...
Useful commands                     •   git archive: export a tree as a tar/zip                     •   git bisect: find th...
Read the history                     • git log                       • print the changes                     • git log -p ...
Using git with others                     •   Commits can be transmitted by many protocols                         •  On d...
Getting commits                     •   Most likely: git clone over SSH:                         git clone user@host:/some...
Working with upstream                     •   Work on local master:                         edit edit; git commit -a      ...
For further info                     •   See “Git (software)” in Wikipedia                     •   And the git homepage ht...
Upcoming SlideShare
Loading in …5
×

Intro to git (one hour version)

3,304 views

Published on

The new shortened slide-deck first used at FISL 13 in Porto Alegre.

Published in: Technology
2 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total views
3,304
On SlideShare
0
From Embeds
0
Number of Embeds
37
Actions
Shares
0
Downloads
53
Comments
2
Likes
5
Embeds 0
No embeds

No notes for slide

Intro to git (one hour version)

  1. Git: a brief introduction Randal L. Schwartz, merlyn@stonehenge.com Version 5.0.2 on 28 Jul 2012 This document is copyright 2011, 2012 by Randal L. Schwartz, Stonehenge Consulting Services, Inc. This work is licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License http://creativecommons.org/licenses/by-nc-sa/3.0/Saturday, August 4, 12
  2. Overview • Key concepts of git • Using git by yourself • Using git with othersSaturday, August 4, 12
  3. Key concepts • Manages trees, not files • Stores entire history for a commit • Serverless (mostly) • Source-code manager • No meta-data • Handles binaries badly • Optimized for branch/merge • If not branching or merging, bad fitSaturday, August 4, 12
  4. The moving parts • Stored in “.git”: • Object repository • files (“blob”), directories, commits • Operational files (config, etc.) • Working directory • Low-level CLI (“plumbing”) • High-level CLI (“porcelain”) • Various GUIs • Various web UIsSaturday, August 4, 12
  5. Commits • Unit of work • Usually has a parent • Two or more on a merge • Zero on a “root” commit • Metadata (committer, message, timestamps) • Tree of files at time of commitSaturday, August 4, 12
  6. Trees • SHA1 of each file in a directory • SHA1 of each directory • ... recursively • These are kept efficiently in the object store • Identical files map to same SHA1, stored once • And so do identical trees! • No penalty to “move” a subdirectory or fileSaturday, August 4, 12
  7. Making commits • Changes made to work dir • Added to index with “git add” • Committed with “git commit” • Commit updates HEAD • Prior value of HEAD is the parent • HEAD almost always points at a branch name • Thus, branch name is also moved forward • Series of commits loosely also called “branch” • Commit also gets a SHA1Saturday, August 4, 12
  8. The SHA1 is king • SHA1 of commit defines: • Metadata of commit • Tree of files • Parentage • Parentage defines previous commits • Thus, SHA1 uniquely defines complete history • Useful when working with othersSaturday, August 4, 12
  9. Naming commits • SHA1 (can often be abbreviated) • HEAD, branch-name, tag • Optionally followed by @{historical} • “historical” can be: • yesterday, 2011-11-22, etc (date ref) • 1, 2, 3, etc (prior version of this ref) • “upstream” (upstream version of local) • Optionally followed by ~n for “n’th ancestor”Saturday, August 4, 12
  10. Branches • Initial branch is “master” (name not special) • Fork the current branch: git checkout -b topic1 • topic1 is set to same SHA1 as previous branch • HEAD now points at topic1 • New commits now apply to topic1 • Switch back with “git checkout master”Saturday, August 4, 12
  11. Multiple branches • Start a second topic based on original master: git checkout -b topic2 master • Work, work, commit, commit • Switch back and forth at will: git checkout topic1 • Topics record independent deltas to master • A B C (master) D E F (topic1) • A B C (master) G H I (topic2)Saturday, August 4, 12
  12. Combining the work • Merge the work back in to master: git checkout master git merge topic1 git branch -d topic1 • This was a fast-forward merge: A B C D E F (new “master”) A B C G H I (topic2)Saturday, August 4, 12
  13. Really merging • Now to bring topic2 back into the mix: git checkout master # already there git merge topic2 • Three-way merge between master, topic2, relative to common ancestor (“C”) • Might cause conflicts • New commit will have both F and I as parents • History is no longer linear • DEF and GHI will appear as parallel linesSaturday, August 4, 12
  14. Linear history • We can “rebase” commits: A B C D E F (master) A B C G H I (topic2) git checkout topic2; git rebase master • Git replays new commits on top of ancestor: A B C D E F (master) G’ H’ I’ (topic2) • Might cause conflicts at each replay • Need to clean up when done: git checkout master; git merge topic2 git branch -d topic2Saturday, August 4, 12
  15. Rebase vs merge • Both can have conflicts • Merge makes a bushy tree • Hard to peel out “linear” history • Rebase makes a linear tree • But gets rid of commits • Rebase is nicer, but you must take care • Shared commits should not be rebasedSaturday, August 4, 12
  16. Using git yourself • Create the repo at your top-level dir: git init git add -A . # or git add * git commit # invokes a text editor • You now have a single “.git” dir • The current working dir snapshotted as rootSaturday, August 4, 12
  17. Work a bit • Edit some files • Check the status: git status • Add all the changes and commit them: git add -A . git commit • Shortcut if you haven’t added new files: git commit -a • You’re making commits on the master branchSaturday, August 4, 12
  18. What’s changed? • git diff • Diff between index and working tree • These are things you should “git add” • “git commit -a” will also make this list empty • git diff HEAD • Difference between HEAD and working tree • “git commit -a” will make this empty • git diff --cached • between HEAD and index • “git commit” (without -a) makes this emptySaturday, August 4, 12
  19. Useful commands • git archive: export a tree as a tar/zip • git bisect: find the offensive commit • git cherry-pick: selective merging • git mv: rename a file/dir • git rm: ditto for delete • git revert: add commit to undo previous commit • git blame: who wrote this?Saturday, August 4, 12
  20. Read the history • git log • print the changes • git log -p • print the changes, including a diff between revisions • git log --stat • Summarize the changes with a diffstat • git log -- file1 file2 dir3 • Show changes only for listed files or subdirsSaturday, August 4, 12
  21. Using git with others • Commits can be transmitted by many protocols • On disk • HTTP[S] • git protocol over TCP • git protocol over SSH • git over SSH is preferredSaturday, August 4, 12
  22. Getting commits • Most likely: git clone over SSH: git clone user@host:/some/path/to/repo.git • Makes local repo.git dir • Checks out remote repo’s “HEAD” as master • Adds remote repo as “origin” • Nothing special about that nameSaturday, August 4, 12
  23. Working with upstream • Work on local master: edit edit; git commit -a • Refresh from upstream • By merge: git pull origin master • By rebase: git pull --rebase origin master • Push to upstream: git push origin master • Check what’s different before pull: git fetch origin; git diff master origin/masterSaturday, August 4, 12
  24. For further info • See “Git (software)” in Wikipedia • And the git homepage http://git-scm.com/ • Git wiki at https://git.wiki.kernel.org/ • Wonderful Pro Git book: http://progit.org/book/ • Get on the mailing list • Helpful people there • You can submit bugs, patches, ideas • And the #git IRC channel (on Freenode) • Now “git” to it!Saturday, August 4, 12

×