CYAN MAGENTA
YELLOW BLACK PANTONE 123 C
w
BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
THE EXPERT’S VOICE® IN SOFTWARE DEVELOPMENT Companion eBook Available
Pro Git
Git
Pro Git is written to help the professional developer learn the Git distributed source control tool from front to back. In this book, you’ll learn why Git is different and powerful, how to use it from beginning usage to advanced features, how to transition to it from an existing system, and how it works under the covers. The book will also help you master a variety of common workflows, both as a contributor to a Git-based project and as a maintainer of such a project. This book assumes no prior knowledge of Git but will make you an expert, ready to dazzle your friends and impress your coworkers. I wrote Pro Git because I teach Git and talk to people all the time who want to learn to use it. Many of them have heard from their peers how great Git is, but I get only an hour or two to try to convince them why the tool is so amazing. This is the in-depth guide I want people to be able to learn from after they have caught the bug. Git is such a huge change from other tools that I feel I became a better programmer after learning it. I want to help you learn it too, perhaps a bit more easily than I did. Git is an incredible tool that will not only make you think about source control differently but also make you a better developer. Using the lightweight branching system to do fast and cheap context switching when you develop will change how you approach programming problems by removing obstacles to development that you shouldn’t have to deal with. I hope this book makes it easier for you to learn Git and feel comfortable using it. I also hope Git changes the way you approach your source-code development tasks, as it did for me.
Pro
Dear Reader,
Pro Git
Sincerely,
Everything you need to know about the Git distributed source control tool
Scott Chacon
See last page for details on $10 eBook version
RELATED TITLES
Companion eBook
+++
)3".
US $34.99
Chacon
Available online at http://progit.org
+++
Scott Chacon Foreword by Junio C Hamano, Git project leader
Shelve in Software Development User level: Beginner–Advanced
this print for content only—size & color not accurate
spine = 0.6563" 288 page count
Pro Git
Scott Chacon
Pro Git Copyright © 2009 by Scott Chacon Permission is granted to copy, distribute and/or modify this document under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. Apress (dppl6++sss*]lnaoo* _ki+) and the author(s) ask for your support by buying the print or eBook edition through any online or retail outlet. A copy of the license is included in the section entitled “Creative Commons Legal Code.” All rights reserved subject to the Creative Commons license. ISBN-13 (pbk): 978-1-4302-1833-3 ISBN-13 (electronic): 978-1-4302-1834-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 The Git logo on this book’s cover comes from dppl6++aj*segela`e]*knc+sege+Beha6Cep)hkck*orc, where it is available under the GNU General Public License. It is available from dppl6++]lnaoo*_ki+^kkg+reas+ -0/,.-4//5. You may redistribute it and/or modify it under the terms of the GPL (dppl6++sss*cjq*knc+ _kluhabp+clh*dpih), as published by the Free Software Foundation; either version 2 of the License, or any later version. Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Duncan Parkes Technical Reviewer: Shawn Pearce Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Managers: Beth Christmas, Candace English Copy Editor: Tiffany Taylor Associate Production Director: Kari Brooks-Copony Production Editor: Liz Berry Compositor: Diana Van Winkle Proofreader: Dan Shaw Indexer: Julie Grady Cover Designer: Anna Ishchenko Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)ju
I would like to dedicate this, my first print book, to my little girl, Josephine, whose release date was nearly the same as this book’s.
Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Git Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CHAPTER 3
Git Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
CHAPTER 4
Git on the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
CHAPTER 5
Distributed Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
CHAPTER 6
Git Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CHAPTER 7
Customizing Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
CHAPTER 8
Git and Other Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
CHAPTER 9
Git Internals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
APPENDIX
Creative Commons Legal Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
v
Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About Version Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Local Version Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Centralized Version Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Distributed Version Control Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 A Short History of Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Git Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Snapshots, Not Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Nearly Every Operation Is Local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Git Has Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Git Generally Only Adds Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The Three States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Installing Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing from Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing on Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 First-Time Git Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Your Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Your Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Your Diff Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Checking Your Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
vii
viii
NCO NTENT S
CHAPTER 2
Git Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Getting a Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Initializing a Repository in an Existing Directory . . . . . . . . . . . . . . . . . 15 Cloning an Existing Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Recording Changes to the Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Checking the Status of Your Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Tracking New Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Staging Modified Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Ignoring Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Viewing Your Staged and Unstaged Changes . . . . . . . . . . . . . . . . . . . 20 Committing Your Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Skipping the Staging Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Removing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Moving Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Viewing the Commit History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Limiting Log Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Using a GUI to Visualize History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Undoing Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Changing Your Last Commit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Unstaging a Staged File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Unmodifying a Modified File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Working with Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Showing Your Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Adding Remote Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Fetching and Pulling from Your Remotes. . . . . . . . . . . . . . . . . . . . . . . 36 Pushing to Your Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Inspecting a Remote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Removing and Renaming Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Tagging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Listing Your Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Creating Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Verifying Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Tagging Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Sharing Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Auto-Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Git Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
NC O N T E N T S
CHAPTER 3
Git Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 What a Branch Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Basic Branching and Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Basic Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Basic Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Basic Merge Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Branch Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Branching Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Long-Running Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Topic Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Remote Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Pushing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Tracking Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Deleting Remote Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Rebasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 The Basic Rebase. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 More Interesting Rebases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Perils of Rebasing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
CHAPTER 4
Git on the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Local Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 The SSH Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Git Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The HTTP/S Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Getting Git on a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Putting the Bare Repository on a Server . . . . . . . . . . . . . . . . . . . . . . . 84 Small Setups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 SSH Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Generating Your SSH Public Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Setting Up the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Public Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 GitWeb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Gitosis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Git Daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
ix
x
NCO NTENT S
Hosted Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Setting Up a User Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Creating a New Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Importing from Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Adding Collaborators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Forking Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 GitHub Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
CHAPTER 5
Distributed Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Distributed Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Centralized Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Integration-Manager Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Dictator and Lieutenants Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Contributing to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Commit Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Private Small Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Private Managed Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Public Small Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Public Large Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Maintaining a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Working in Topic Branches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Applying Patches from E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Checking Out Remote Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Determining What Is Introduced . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Integrating Contributed Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Tagging Your Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Generating a Build Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Preparing a Release. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 The Shortlog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CHAPTER 6
Git Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Revision Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Single Revisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Commit Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
NC O N T E N T S
Interactive Staging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Stashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Stashing Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Creating a Branch from a Stash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Rewriting History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Changing the Last Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Changing Multiple Commit Messages . . . . . . . . . . . . . . . . . . . . . . . . 157 Reordering Commits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Squashing a Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Splitting a Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 The Nuclear Option: filter-branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Debugging with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 File Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Starting with Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Cloning a Project with Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Superprojects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Issues with Submodules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Subtree Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
CHAPTER 7
Customizing Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Git Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Basic Client Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Colors in Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 External Merge and Diff Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Formatting and Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Server Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Git Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Keyword Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Exporting Your Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Merge Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Git Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Installing a Hook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Client-Side Hooks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Server-Side Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
xi
xii
NCO NTENT S
An Example Git-Enforced Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Server-Side Hook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Client-Side Hooks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
CHAPTER 8
Git and Other Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Git and Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 git svn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Setting Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Committing Back to Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Pulling in New Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Git Branching Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Subversion Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Subversion Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Git-Svn Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Migrating to Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Perforce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 A Custom Importer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
CHAPTER 9
Git Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Plumbing and Porcelain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Git Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Tree Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Commit Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Object Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Git References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 The HEAD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Remotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Packfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 The Refspec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Pushing Refspecs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Deleting References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
NC O N T E N T S
Transfer Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 The Dumb Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 The Smart Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Downloading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Maintenance and Data Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Data Recovery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Removing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
APPENDIX
Creative Commons Legal Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
xiii
Foreword I
n April 2005, Linus Torvalds published the very first version of Git, the distributed version control system that is the topic of this book, and started managing the Linux kernel project with it. Countless online pages have been written about Git by third parties since then, but many of them are unfortunately obsolete—not in the sense that the procedures they teach no longer work, but in the sense that there are better ways to do the same things more effectively with more modern versions of Git. The rate at which Git has undergone vast improvements both in capability and usability has been simply too rapid for those pages to keep up. For a long time, the user manual and the documentation that came with Git were the only up-to-date and accurate sources of information, but they were primarily written by the people who built Git. The Git community sorely lacked good introductory material written from the perspective of the end user. Enter Scott Chacon, the author of this book, who is also behind git-scm.com, the popular online resource that has become the default home site for Git documentation. In this latest book, Scott makes effective use of graphics to explain the key concepts and writes in plain and clear language to give a readable overview of how to work with Git. After you gain a solid understanding from this book, I hope you will find yourself being more productive and using Git more effectively. And, more important, I hope you will enjoy using Git. Junio C Hamano Git Project Leader
xv
About the Author NSCOTT CHACON is a Git evangelist and Ruby developer employed at Logical Awesome, working on GitHub.com, the largest Git hosting web site. He is the author of the Git Internals Peepcode PDF in addition to maintaining the Git home page (git-scm.com) and the Git Community Book (book.git-scm.com). Scott has presented at conferences such as RailsConf, RubyConf, RubyKaigi, Scotland on Rails, and OSCon, as well as for companies such as Google, Yahoo, and Digg. Scott occasionally talks about Git for a number of local groups and has done corporate training on Git across the country.
xvii
About the Technical Reviewer NSHAWN PEARCE is the second in command of the Git project. He has been actively involved in the project since early 2006, contributing more than 1,300 changes in three years. Shawn is the author of git-gui, a Tk based graphical interface shipped with Git; and git-fast-import, a stream-based import system often used for converting projects to Git. In addition, Shawn’s opinion, backed by his code, has influenced many key design decisions that form the modern Git implementation. In early 2006, Shawn founded the JGit project, creating a 100% pure Java reimplementation of the Git version control system. The JGit library can often be found in Java-based products that interact with Git, including Git plug-ins for the popular Eclipse and NetBeans IDEs; the Hudson CI server; Apache Maven; and Gerrit Code Review, a peer code-review system specially designed for Git. Today he continues to develop and maintain JGit, EGit (the Eclipse plug-in based on top of it), and Gerrit Code Review.
xviii
Acknowledgments F
irst, I would like to thank the entire Git development community for giving us such a great tool. I dislike working in C and so have contributed little actual code to the Git project, opting instead to try to teach, document, and evangelize it, which I tend to be a lot better at. However, without the guys that wrote and maintain Git, I would still be living in the sad and hopeless version-controlled world that is Subversion. I would also very much like to thank Shawn Pearce, who, aside from being one of the more prolific developers on the Git project, is also the technical editor for this book. Shawn has saved me from making countless horrible technical mistakes in this book and has done so on numerous other occasions as well. I always enjoy working with him; I tend to give my best guess at something, and then he teaches me what the right answer is. I have learned a ton from him, which I often get to teach to hundreds of others (who then give me credit). I want to thank my good friend Nick Hengeveld, who has contributed a lot of work to Git and, more important for me, introduced me to the tool and taught me how to use it back in the pre-1.0 days. There is probably no way I would be doing all this if he had not brought Git to my attention and introduced it to the environment in which we both worked. Further thanks go to Chris Wanstrath, Tom Preston-Werner, and PJ Hyett for inviting me to join them in working on GitHub very early on, allowing me to spend basically all my time thinking about and working on Git-related projects. It’s difficult to imagine a better group of people to work with or a better job to have. Last, I want to thank my wife, Jessica, who kept me continually working at this. Writing a book of this scope is a heck of a process, and when I stalled out from time to time, she made sure I got back on track. Thanks, Magoo.
xix
Introduction Y
ou’re about to spend several hours of your life reading about Git. Let’s take a minute to explain what we have in store for you. Here is a quick summary of this book’s nine chapters: In Chapter 1, you’ll cover Version Control Systems (VCSs) and Git basics—no technical stuff, just what Git is, why it came about in a land full of VCSs, what sets it apart, and why so many people are using it. Then, you’ll learn how to download Git and set it up for the first time if you don’t already have it on your system. In Chapter 2, you’ll go over basic Git usage—how to use Git in the 80% of cases you’ll encounter most often. After reading this chapter, you should be able to clone a repository, see what has happened in the history of the project, modify files, and contribute changes. If the book spontaneously combusts at this point, you should already be pretty useful wielding Git in the time it takes you to go pick up another copy. Chapter 3 is about the branching model in Git, often described as Git’s killer feature. Here, you’ll learn what truly sets Git apart from the pack. When you’re done, you may feel the need to spend a quiet moment pondering how you lived before Git branching was part of your life. Chapter 4 will cover Git on the server. This chapter is for those of you who want to set up Git inside your organization or on your personal server for collaboration. You’ll also explore various hosted options if you prefer to let someone else handle that for you. Chapter 5 will go over in full detail various distributed workflows and how to accomplish them with Git. When you’re done with this chapter, you should be able to work expertly with multiple remote repositories, use Git over e-mail, and deftly juggle numerous remote branches and contributed patches. Chapter 6 is about advanced Git commands. You’ll learn about topics like binary searching to identify bugs, editing history, revision selection in detail, and a lot more. This chapter will round out your knowledge of Git so that you’re truly a master. Chapter 7 is about configuring your custom Git environment. This includes setting up hook scripts to enforce or encourage customized policies and using environment configuration settings so you can work the way you want to. You’ll also cover building your own set of scripts to enforce a custom committing policy. Chapter 8 deals with Git and other VCSs. This includes using Git in a Subversion (SVN) world and converting projects from other VCSs to Git. A lot of organizations still use SVN and aren’t about to change, but by this point you’ll have learned the incredible power of Git—and this chapter will show you how to cope if you still have to use a SVN server. You’ll also cover how to import projects from several different systems in case you do convince everyone to make the plunge. Now that you know all about Git and can wield it with power and grace, you can move on to Chapter 9, which delves into the murky yet beautiful depths of Git internals. Here you’ll learn how Git stores its objects, what the object model is, details of packfiles and server protocols, and more. Throughout, the book refers to sections of this chapter in case you feel like looking deeper at that point; but if you’re like me and want to dive right into the technical details, you may want to read Chapter 9 first. I leave that up to you. Let’s get started.
xxi
CHAP TER
1
Getting Started T
his chapter is about getting started with Git. We’ll begin at the beginning by presenting some background about n version-control tools, then move on to how you get Git running on your system, and finally explain how to set it up so you can start working with Git. At the end of this chapter, you should understand why Git exists and why you should use it, and you should be ready to do so.
About Version Control What is version control, and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. For the examples in this book, you will use software source code as the files being version controlled, though in reality you can do this with nearly any type of file on a computer. If you are a graphic or web designer and want to keep every version of an image or layout (which you would most certainly want to), a Version Control System (VCS) is a very wise thing to use. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.
1
2
CH APT ER 1 N G ETTING S TA R TED
Local Version Control Systems Many people’s version-control method of choice is to copy files into another directory (perhaps a time-stamped directory, if they’re clever). This approach is very common because it’s so simple, but it’s also incredibly error prone. It’s easy to forget which directory you’re in and accidentally write to the wrong file or copy over files when you don’t mean to. To deal with this issue, programmers long ago developed local VCSs that had a simple database that kept all the changes to files under revision control (see Figure 1-1). One of the more popular VCS tools was a system called rcs, which is still distributed with many computers today. Even the popular Mac OS X operating system includes the n_o command when you install the Developer Tools. This tool basically works by keeping patch sets (that is, the differences between files) from one change to another in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches.
Figure 1-1. Local version control diagram
C H A P T E R 1 N G E T T I N G S T A R T E D
Centralized Version Control Systems The next major issue that people encounter is that they need to collaborate with developers on other systems. To deal with this problem, Centralized Version Control Systems (CVCSs) were developed. These systems, such as CVS, Subversion, and Perforce, have a single server that contains all the versioned files, and a number of clients that check out files from that central place. For many years, this has been the standard for version control (see Figure 1-2). This setup offers many advantages, especially over local VCSs. For example, everyone knows to a certain degree what everyone else on the project is doing. Administrators have fine-grained control over who can do what; and it’s far easier to administer a CVCS than it is to deal with local databases on every client. However, this setup also has some serious downsides. The most obvious is the single point of failure that the centralized server represents. If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on. If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything—the entire history of the project except whatever single snapshots people happen to have on their local machines. Local VCS systems suffer from this same problem—whenever you have the entire history of the project in a single place, you risk losing everything.
Figure 1-2. Centralized version control diagram
3
4
CH APT ER 1 N G ETTING S TA R TED
Distributed Version Control Systems This is where Distributed Version Control Systems (DVCSs) step in. In a DVCS (such as Git, Mercurial, Bazaar, or Darcs), clients don’t just check out the latest snapshot of the files: they fully mirror the repository. Thus if any server dies, and these systems were collaborating via it, any of the client repositories can be copied back up to the server to restore it. Every checkout is really a full backup of all the data (see Figure 1-3).
Figure 1-3. Distributed version control diagram Furthermore, many of these systems deal pretty well with having several remote repositories they can work with, so you can collaborate with different groups of people in different ways simultaneously within the same project. This allows you to set up several types of workflows that aren’t possible in centralized systems, such as hierarchical models.
C H A P T E R 1 N G E T T I N G S T A R T E D
A Short History of Git As with many great things in life, Git began with a bit of creative destruction and fiery controversy. The Linux kernel is an open source software project of fairly large scope. For most of the lifetime of the Linux kernel maintenance (1991–2002), changes to the software were passed around as patches and archived files. In 2002, the Linux kernel project began using a proprietary DVCS system called BitKeeper. In 2005, the relationship between the community that developed the Linux kernel and the commercial company that developed BitKeeper broke down, and the tool’s free-of-charge status was revoked. This prompted the Linux development community (and in particular Linus Torvalds, the creator of Linux) to develop their own tool based on some of the lessons they learned while using BitKeeper. Some of the goals of the new system were as follows:
s 3PEED
s 3IMPLE DESIGN
s 3TRONG SUPPORT FOR NON LINEAR DEVELOPMENT THOUSANDS OF PARALLEL BRANCHES
s &ULLY