SlideShare a Scribd company logo
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
Overview

                     •   Key concepts of git
                     •   Using git by yourself
                     •   Using git with others




Saturday, August 4, 12
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 fit



Saturday, August 4, 12
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 UIs



Saturday, August 4, 12
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 commit




Saturday, August 4, 12
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 file

Saturday, August 4, 12
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 SHA1

Saturday, August 4, 12
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 others




Saturday, August 4, 12
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
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
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
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
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 lines



Saturday, August 4, 12
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 topic2


Saturday, August 4, 12
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 rebased

Saturday, August 4, 12
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 root




Saturday, August 4, 12
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 branch



Saturday, August 4, 12
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 empty


Saturday, August 4, 12
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
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 subdirs

Saturday, August 4, 12
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 preferred




Saturday, August 4, 12
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 name




Saturday, August 4, 12
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/master



Saturday, August 4, 12
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

More Related Content

PDF
Git: a brief introduction
ZIP
Introduction to Git
PDF
Git Is A State Of Mind - The path to becoming a Master of the mystic art of Git
PPTX
Git Presentation
PDF
Github - Git Training Slides: Foundations
PDF
Git training v10
PDF
Intro To Git
PDF
Git Internals
Git: a brief introduction
Introduction to Git
Git Is A State Of Mind - The path to becoming a Master of the mystic art of Git
Git Presentation
Github - Git Training Slides: Foundations
Git training v10
Intro To Git
Git Internals

What's hot (20)

PDF
An introduction to git
PPTX
Basic Git Intro
PPTX
Introduction to Git and Github
PDF
Git: A Getting Started Presentation
PDF
Smau Milano 2016 - Fabio Alessandro Locati
PDF
Advanced Git Tutorial
PDF
Getting Git Right
PDF
Ruby in office time reboot
PDF
Into The Box 2020 Keynote Day 1
PDF
Open Source Tools for Leveling Up Operations FOSSET 2014
PPTX
Git and Github
PPT
Talk to git
PPTX
Introduction to github using Egit
PPTX
git-and-bitbucket
PDF
VCS for Teamwork - GIT Workshop
PPTX
Git 101
PPT
Domino testing presentation
PPT
Node.js what's next (Index 2018)
PDF
Rupher = Ruby + Gopther
PDF
Rupher
An introduction to git
Basic Git Intro
Introduction to Git and Github
Git: A Getting Started Presentation
Smau Milano 2016 - Fabio Alessandro Locati
Advanced Git Tutorial
Getting Git Right
Ruby in office time reboot
Into The Box 2020 Keynote Day 1
Open Source Tools for Leveling Up Operations FOSSET 2014
Git and Github
Talk to git
Introduction to github using Egit
git-and-bitbucket
VCS for Teamwork - GIT Workshop
Git 101
Domino testing presentation
Node.js what's next (Index 2018)
Rupher = Ruby + Gopther
Rupher
Ad

Similar to Intro to git (one hour version) (20)

PDF
Introduction to git
PDF
Gitting It Under (Version) Control
PDF
GIT: Content-addressable filesystem and Version Control System
PPTX
PPTX
Git One Day Training Notes
PDF
Git Intermediate Workshop slides v1.3
PPTX
Git workshop
PDF
Git intermediate workshop slides v1.4
PPT
Git basic
PPTX
Git like a pro EDD18 - Full edition
KEY
Git Tech Talk
PPTX
Git training (basic)
PPTX
PPTX
Git_new.pptx
PPTX
git internals
PDF
Git for the Android Developer
PDF
Git for the Android Developer
PDF
SCM for Android Developers Using Git
PPTX
Git and GitHub
PDF
Introduction to git, an efficient distributed version control system
Introduction to git
Gitting It Under (Version) Control
GIT: Content-addressable filesystem and Version Control System
Git One Day Training Notes
Git Intermediate Workshop slides v1.3
Git workshop
Git intermediate workshop slides v1.4
Git basic
Git like a pro EDD18 - Full edition
Git Tech Talk
Git training (basic)
Git_new.pptx
git internals
Git for the Android Developer
Git for the Android Developer
SCM for Android Developers Using Git
Git and GitHub
Introduction to git, an efficient distributed version control system
Ad

More from Randal Schwartz (7)

PDF
Why Flutter.pdf
PDF
Native mobile application development with Flutter (Dart)
PDF
Perl best practices v4
PDF
A brief introduction to dart
PDF
My half life with perl
PDF
Testing scripts
ZIP
Forget The ORM!
Why Flutter.pdf
Native mobile application development with Flutter (Dart)
Perl best practices v4
A brief introduction to dart
My half life with perl
Testing scripts
Forget The ORM!

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Machine Learning_overview_presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Machine Learning_overview_presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”

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 others Saturday, 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 fit Saturday, 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 UIs Saturday, 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 commit Saturday, 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 file Saturday, 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 SHA1 Saturday, 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 others Saturday, 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 lines Saturday, 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 topic2 Saturday, 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 rebased Saturday, 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 root Saturday, 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 branch Saturday, 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 empty Saturday, 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 subdirs Saturday, 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 preferred Saturday, 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 name Saturday, 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/master Saturday, 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