Pragmatic Version Control Using Git.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Prepared exclusively for Trieu Nguyen Download at Boykma.Com What 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 from using 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 Version Control Using Subversion Prepared exclusively for Trieu Nguyen Download at Boykma.Com Pragmatic Version Control Using Git Travis Swicegood The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Prepared exclusively for Trieu Nguyen Download at Boykma.Com Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The 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 Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. 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 Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Travis Swicegood. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-15-8 ISBN-13: 978-1-934356-15-9 Printed on acid-free paper. P2.0 printing, March 2009 Version: 2009-4-20 Prepared exclusively for Trieu Nguyen Download at Boykma.Com 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 Alternate Histories 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 Prepared exclusively for Trieu Nguyen Download at Boykma.Com 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 Report erratum Prepared exclusively for Trieu Nguyen this copy is (P2.0 printing, March 2009) Download at Boykma.Com 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 Report erratum Prepared exclusively for Trieu Nguyen this copy is (P2.0 printing, March 2009) Download at Boykma.Com 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 toSVN 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 Report erratum Prepared exclusively for Trieu Nguyen this copy is (P2.0 printing, March 2009) Download at Boykma.Com Acknowledgments Although my name is on this book as the author, it is only here because of a long list of people. Please indulge me while I take a moment to thank them. Thanks to Dave Thomas and Andy Hunt for taking the chance on a new author. The entire team they’ve put together at Pragmatic Bookshelf is amazing. I owe a special thanks to my editor, the ever helpful Susannah 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 wrote this 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. Without her support, and that from the rest of my friends and family, this book wouldn’t be here today. Prepared exclusively for Trieu Nguyen Download at Boykma.Com 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 relative