Table Of ContentWhat readers are saying about Pragmatic Version Control
Using Git
Pragmatic Version Control Using Git is an excellent guide to getting you
started with Git. It will teach you not only how to get yourself started
but also how to cooperate with others and how to keep your history
clean.
Pieter de Bie
Author, GitX
If you are thinking of using Git, I highly recommend this book. If you
are not using a version control system (and code or create content on
a computer), put the book down, slap yourself, pick the book back up,
and buy it.
Jacob Taylor
Entrepreneur and Cofounder, SugarCRM Inc.
Not only has this book convinced me that Git has something to offer
over CVS and Subversion, but it has also showed me how I can benefit
fromusing it myself even if I’m using it alone in a CVS/Subversion
environment. I expect to be a full-time Git user soon after reading
this, which is hard to believe considering this is the first distributed
version control system I’ve ever looked at.
Chuck Burgess
2008 PEAR Group Member
Travis has done an excellent job taking a tricky subject and making it
accessible, useful, and relevant. You’ll find distributed version control
and Git much less mysterious after reading this book.
Mike Mason
Author, Pragmatic VersionControl Using Subversion
Pragmatic Version Control
Using Git
Travis Swicegood
The Pragmatic Bookshelf
Raleigh,NorthCarolina Dallas,Texas
Many of the designations used by manufacturers and sellers to distinguish their prod-
uctsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andThe
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
PragmaticProgrammer,PragmaticProgramming,PragmaticBookshelfandthelinkingg
devicearetrademarksofThePragmaticProgrammers,LLC.
Every precaution was taken in the preparation of this book. However, the publisher
assumesnoresponsibility for errorsor omissions,or for damagesthatmay result from
theuseofinformation(includingprogramlistings)containedherein.
Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatictitles,pleasevisitusat
http://www.pragprog.com
Copyright©2008TravisSwicegood.
Allrightsreserved.
Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmit-
ted, in any form, or by any means, electronic,mechanical,photocopying, recording, or
otherwise,withoutthepriorconsentofthepublisher.
PrintedintheUnitedStatesofAmerica.
ISBN-10:1-934356-15-8
ISBN-13:978-1-934356-15-9
Printedonacid-freepaper.
P2.0printing,March2009
Version:2009-4-20
(cid:13)
Contents
Acknowledgments 9
Preface 10
Who’s This Book For? . . . . . . . . . . . . . . . . . . . . . . . 11
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . 11
Typographic Conventions . . . . . . . . . . . . . . . . . . . . . 13
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . 13
I Welcome to the Distributed World 14
1 Version Control the Git Way 15
1.1 The Repository . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2 What Should You Store? . . . . . . . . . . . . . . . . . . 17
1.3 Working Trees . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4 Manipulating Files and Staying in Sync . . . . . . . . . 18
1.5 Tracking Projects, Directories, and Files . . . . . . . . . 19
1.6 Tracking Milestones with Tags . . . . . . . . . . . . . . 20
1.7 Creating AlternateHistories with Branches . . . . . . . 21
1.8 Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.9 Locking Options . . . . . . . . . . . . . . . . . . . . . . . 24
1.10 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2 Setting Up Git 26
2.1 Installing Git . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2 Configuring Git . . . . . . . . . . . . . . . . . . . . . . . 31
2.3 Using Git’s GUI . . . . . . . . . . . . . . . . . . . . . . . 33
2.4 Accessing Git’s Built-in Help. . . . . . . . . . . . . . . . 34
(cid:13)
CONTENTS 6
3 Creating Your First Project 36
3.1 Creating a Repository . . . . . . . . . . . . . . . . . . . . 37
3.2 Making Changes . . . . . . . . . . . . . . . . . . . . . . . 37
3.3 Starting to Work with a Project . . . . . . . . . . . . . . 40
3.4 Using and Understanding Branches . . . . . . . . . . . 43
3.5 Handling Releases . . . . . . . . . . . . . . . . . . . . . . 44
3.6 Cloning a Remote Repository . . . . . . . . . . . . . . . 48
II Everyday Git 50
4 Adding and Committing: Git Basics 51
4.1 Adding Files . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2 Committing Changes . . . . . . . . . . . . . . . . . . . . 55
4.3 Seeing What Has Changed . . . . . . . . . . . . . . . . . 58
4.4 Managing Files. . . . . . . . . . . . . . . . . . . . . . . . 61
5 Understanding and Using Branches 65
5.1 What Are Branches? . . . . . . . . . . . . . . . . . . . . 66
5.2 Creating a New Branch . . . . . . . . . . . . . . . . . . . 67
5.3 Merging Changes Between Branches . . . . . . . . . . . 69
5.4 Handling Conflicts . . . . . . . . . . . . . . . . . . . . . 74
5.5 Deleting Branches . . . . . . . . . . . . . . . . . . . . . . 77
5.6 Renaming Branches . . . . . . . . . . . . . . . . . . . . 78
6 Working with Git’s History 80
6.1 Inspecting Git’s Log . . . . . . . . . . . . . . . . . . . . . 81
6.2 Specifying Revision Ranges . . . . . . . . . . . . . . . . 82
6.3 Looking at Differences Between Versions . . . . . . . . 85
6.4 Finding Out Who’s to Blame . . . . . . . . . . . . . . . . 86
6.5 Following Content . . . . . . . . . . . . . . . . . . . . . . 88
6.6 Undoing Changes . . . . . . . . . . . . . . . . . . . . . . 91
6.7 Rewriting History . . . . . . . . . . . . . . . . . . . . . . 94
7 Working with Remote Repositories 100
7.1 Network Protocols . . . . . . . . . . . . . . . . . . . . . . 100
7.2 Cloning a Remote Repository . . . . . . . . . . . . . . . 103
7.3 Keeping Up-to-Date . . . . . . . . . . . . . . . . . . . . . 104
7.4 Pushing Changes . . . . . . . . . . . . . . . . . . . . . . 105
7.5 Adding New Remote Repositories . . . . . . . . . . . . . 106
Reporterratum
(cid:13)
thiscopyis(P2.0printing,March2009)
CONTENTS 7
8 Organizing Your Repository 109
8.1 Marking Milestones with Tags . . . . . . . . . . . . . . . 110
8.2 Handling Release Branches . . . . . . . . . . . . . . . . 112
8.3 Using Valid Names for Tags and Branches . . . . . . . 114
8.4 Tracking Multiple Projects . . . . . . . . . . . . . . . . . 115
8.5 Using Git Submodules to Track External Repositories . 116
9 Beyond the Basics 122
9.1 Compacting Repository History . . . . . . . . . . . . . . 123
9.2 Exporting Your Repository . . . . . . . . . . . . . . . . . 124
9.3 Rebasing a Branch . . . . . . . . . . . . . . . . . . . . . 125
9.4 Using the Reflog . . . . . . . . . . . . . . . . . . . . . . . 128
9.5 Bisecting Your Repository . . . . . . . . . . . . . . . . . 131
III Administration 135
10 Migrating to Git 136
10.1 Communicating with SVN . . . . . . . . . . . . . . . . . 136
10.2 Making Sure git-svn Is Available . . . . . . . . . . . . . 140
10.3 Importing a Subversion Repository . . . . . . . . . . . . 141
10.4 Keeping Up-to-Date with a Subversion Repository . . . 143
10.5 Pushing Changes to SVN . . . . . . . . . . . . . . . . . . 145
10.6 Importing from CVS . . . . . . . . . . . . . . . . . . . . . 146
11 Running a Git Server with Gitosis 147
11.1 Making Sure Dependencies Are Met . . . . . . . . . . . 148
11.2 Installing Gitosis . . . . . . . . . . . . . . . . . . . . . . 149
11.3 Creating Administrator Credentials. . . . . . . . . . . . 149
11.4 Configuring the Server for Gitosis. . . . . . . . . . . . . 150
11.5 Initializing Gitosis . . . . . . . . . . . . . . . . . . . . . . 151
11.6 Configuring Gitosis . . . . . . . . . . . . . . . . . . . . . 151
11.7 Adding New Repositories . . . . . . . . . . . . . . . . . . 152
11.8 Setting Up a Public Repository . . . . . . . . . . . . . . 154
11.9 Closing Thoughts . . . . . . . . . . . . . . . . . . . . . . 155
Reporterratum
(cid:13)(cid:13)(cid:13)(cid:13)
thiscopyis(P2.0printing,March2009)
CONTENTS 8
IV Appendixes 157
A Git Command Quick Reference 158
A.1 Setup and Initialization. . . . . . . . . . . . . . . . . . . 158
A.2 Normal Usage . . . . . . . . . . . . . . . . . . . . . . . . 159
A.3 Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
A.4 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
A.5 Remote Repositories . . . . . . . . . . . . . . . . . . . . 164
A.6 Git to SVN Bridge . . . . . . . . . . . . . . . . . . . . . . 165
B Other Resources and Tools 167
B.1 Extras Bundled with Git . . . . . . . . . . . . . . . . . . 167
B.2 Third-Party Tools . . . . . . . . . . . . . . . . . . . . . . 168
B.3 Git Repository Hosting . . . . . . . . . . . . . . . . . . . 171
B.4 Online Resources . . . . . . . . . . . . . . . . . . . . . . 172
C Bibliography 174
Index 175
Reporterratum
(cid:13)
thiscopyis(P2.0printing,March2009)
Acknowledgments
Althoughmynameisonthisbookastheauthor,itisonlyherebecause
ofalonglistofpeople.PleaseindulgemewhileItakeamomenttothank
them.
ThankstoDaveThomasandAndyHuntfortakingthechanceonanew
author. The entire team they’ve put together at Pragmatic Bookshelf is
amazing.Ioweaspecialthankstomyeditor,theeverhelpfulSusannah
Davidson Pfalzer.
I would also like to thank all of those who offered me feedback on the
book as it progressed through beta form. Especially helpful was the
great team of technical reviewers: Chuck Burgess, Pieter de Bie, Stu-
art Halloway, Junio Hamano, Chris Hartjes, Mike Mason, John Mertic,
Gary Sherman, Jacob Taylor, and Tommi “Tv” Virtanen. Thanks also
to the wonderful teams of developers and colleagues at SugarCRM and
Ning who supported me while I wrotethis book.
Finally, I would like to thank my friends and family for their support
and understanding while I wrote this book. My wonderful wife, Meg,
put up with the late nights and “work weekends” with very little com-
plaining.Withouthersupport, andthatfromtherestof myfriendsand
family, this book wouldn’t be here today.
(cid:13)
Preface
Development teams around the globe are changing. They are dropping
their clunky, old, centralized version control systems (VCSs) in favor of
Git, which is a lightweight, distributed version control system (DVCS)
and relativenewcomer to the version control world.
First here’s a quick overview: a version control system is like a bank
vault. You take your valuables—in our case as developers, these valu-
ables are the source code we write—and deposit them in the bank
for safekeeping.1 Each change you mark—or commit—is recorded, and
you can go back over the history just like you can review your bank
statement.
In the Git world, it’s like you walk around with your own vault that
has an automated teller attached right to it. You can fully disconnect
from everyone else, share what you want, and of course keep track of
your project’s history. The brainchild of Linus Torvalds, Git was orig-
inally developed to track changes made to the Linux kernel. Git has
maturedfromtheoriginal rough collection of scripts Linuscreatedin a
few weeks into a rich toolkit. Its following strengths can help you as a
programmer:
• Distributed architecture: Disconnect completely, and work without
the distractions of an always-on Internet connection.
• Easy branching and merging: Creating branches is easy, cheap,
andfast,andunlikesomeversioncontrolsystems,mergingevery-
thing back together—even multiple times—is a snap.
1. AsIwritethis,we’reinthemiddleofa$700billionbailoutoftheAmericanbanking
system, so maybe a bank isn’t the best of analogies. Don’t think about that part; just
thinkofbanksthewaythey’resupposedtowork.
(cid:13)