Download from Wow! eBook <www.wowebook.com> What Readers Are Saying About Pragmatic Guide to Git I’ d heard a l ot of the hype surrounding Git. It wa s n ’t until I read T r a v i s ’ book that I learned why people are so e nthusiastic a bout i t. T r a v i s does a great j ob e x p l a i n i n g the p o wer of Git in a digestible format. Ivo Ja n s c h PHP e v a n g e l i s t , author, and founder, Egeniq.com Git can be i ntimidating and frustrating to n ew users. P ragmatic Guide t o Git alleviates that pain with a s traightforward, concise w a l k - t h r o u g h that a rms readers with ex a c t l y what they need to u se Git productively. Luigi Montanez Software d ev eloper, Sunlight Labs This book i s a must-have for anyone u sing Git or just g etting started with Git. I t has s aved me time in finding the best practices f or managing my Git repositories and will sit on my bookshelf as the go-to r esource for anything Git. J o h n Mertic Senior software e ngineer, SugarCRM W i t h two y ears of e x p e r i e n c e with Git, I thought I wo u l d have known most e v e r y t h i n g in Pragmatic Guide to Git. After reading it cover t o cover, I learned that’s not t he case. It’ s a well-organized collection o f useful Git techniques for all audiences. Luke Pillow Software e ngineer, pillowfactory.org Download from Wow! eBook <www.wowebook.com> Pragmatic Guide to Git T r a v i s Swicegood The P ragmatic Bookshelf Raleigh, North Carolina Dallas, T e x a s Download from Wow! eBook <www.wowebook.com> Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. W here those d esignations appear in this b ook, and The P ragmatic Pro- grammers, LLC was aw a re of a trademark claim, the designations have been printed i n initial capital letters or in all c apitals. T he Pragmatic Starter Kit, T he Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks o f T he P ragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, t he publisher assumes n o responsibility for errors or omissions, or for damages that may result f rom the use of information (including p rogram listings) contained herein. Our Pragmatic courses, wor k s ho ps , and other products can help you and your team create better software and have more fun. Fo r more information, as well as the latest Pragmatic titles, please visit u s at http://www.pragprog.com. The team that produced this book includes: Editor: Susannah Davidson Pfalzer Indexing: Potomac Indexing, LLC Copy edit: Kim W i m p s e t t Layout: Steve P eter Production: Janet Furlow Customer support: Ellie Callahan International: Juliet Benda Copyright © 2010 Pragmatic P rogrammers, LLC. All r ights reserved. No part of this publication m ay be reproduced, s tored in a r etrieval system, or transmitted, in any form, or by any means, electronic, mechanical, p hotocopying, recording, or otherwise, without the prior consent of the publisher. Printed i n the United States of America. ISBN-10: 1-934356-72-7 ISBN-13: 978-1-934356-72-2 Printed on acid-free paper. P1.0 printing, October 2010 V e r s i o n : 2010-10-29 Download from Wow! eBook <www.wowebook.com> Contents Acknowledgments 8 Introduction 9 Who I s This Book Fo r ? . . . . . . . . . . . . . . . . . . . . 9 How to Read This Book . . . . . . . . . . . . . . . . . . . . 10 How Git Is Different . . . . . . . . . . . . . . . . . . . . . . 1 2 The Git W o r k fl o w . . . . . . . . . . . . . . . . . . . . . . . 13 Online Resources . . . . . . . . . . . . . . . . . . . . . . . 16 I Getting Started 17 T a s k 1. Installing Git 20 T a s k 2. Configuring Git 22 T a s k 3. Creating a New Repository 24 T a s k 4. Creating a Local Copy o f an E xisting Repository 26 II W o r k i n g with Git 28 T a s k 5. Seeing What Has Changed 32 T a s k 6. Staging Changes to Commit 34 T a s k 7. Committing Changes 36 T a s k 8. Ignoring F iles 38 T a s k 9. Undoing Uncommitted Changes 40 T a s k 10. Moving F iles in Git 42 T a s k 11. Deleting F iles i n Git 44 Download from Wow! eBook <www.wowebook.com> CONTENTS 6 T a s k 12. Sharing Changes 46 III Organizing Y o u r Repository with Branches and T a g s 48 T a s k 13. Creating and Switching Branches 54 T a s k 14. V i e w i n g Branches 56 T a s k 15. Merging Commits Between Branches 58 T a s k 16. Rewriting History b y Rebasing 60 T a s k 17. Deleting Branches 62 T a s k 18. T a g g i n g Milestones 64 IV W o r k i n g with a T e a m 66 T a s k 19. Adding and Removing Remotes 70 T a s k 20. Retrieving Remote Changes 72 T a s k 21. Retrieving Remote Changes, Pa r t II 74 T a s k 22. Sending Changes t o Remotes 76 T a s k 23. Handling Remote T a g s and Branches 78 V Branches and Merging Revisited 80 T a s k 24. Handling Conflicts 82 T a s k 25. Handling Conflicts with a GUI 84 T a s k 26. T e m p o r a r i l y Hiding Changes 86 T a s k 27. Cherry-Picking Commits 88 T a s k 28. Controlling How Y o u Replay Commits 90 T a s k 29. Moving Branches 92 Report erratum this copy is (P1.0 printing, October 2010) Download from Wow! eBook <www.wowebook.com> CONTENTS 7 VI W o r k i n g with the Repository’s History 94 T a s k 30. V i e w i n g the Log 98 T a s k 31. Filtering the Log Output 100 T a s k 32. Comparing Differences 102 T a s k 33. Generating Statistics About Changes 104 T a s k 34. Assigning Blame 106 VII F ixing Things 108 T a s k 35. Fixing Commits 1 10 T a s k 36. Reverting Commits 112 T a s k 37. Resetting Staged Changes and Commits 114 T a s k 38. Erasing Commits 116 T a s k 39. Finding Bugs with bisect 118 T a s k 40. Retrieving “Lost” Commits 120 VIII Moving Beyond the Basics 122 T a s k 41. Exporting Y o u r Repository 124 T a s k 42. Doing Some Git Housekeeping 126 T a s k 43. Syncing with S ubversion 128 T a s k 44. Initializing Bare Repositories 130 A Glossary 132 Index 136 Report erratum this copy is (P1.0 printing, October 2010) Download from Wow! eBook <www.wowebook.com> Acknowledgments Like any book, this is the result of much more than an author such as me sitting in front of their computer typing a b u n c h of wor d s. Please give me a few minutes to thank those in v olved in b ringing this book to you. First, I’ d like to thank a reader of my first book, who shot me an email that planted the seed that b ecame this book. Next, Dave, Andy , and the entire crew at Pragmatic Bookshelf have been great to wo r k with a second time. Both books I’v e written for them have been gambles—first as a r ookie author and then with this book as an author charting the territory of a new format—and they haven’t blinked an e y e . My editor, Susannah Davidson Pfalzer, has been indispensable. She wa s al- wa y s there with advice, tips, the occasional tough love, and an e v e r - o p t i m i s t i c attitude; e v e r y author should b e so lucky. Reviewers of early drafts of this book provided me with a tremendous amount of constructive f eedback that helped s haped this book i nto what you’ re hold- ing in your hands (or looking at on your computer’s screen). Thanks to Joel Clermont, Javier Collado, Geoff Drake, Chad Dumler-Montplaisir, W a y n e Huang, Michael Hunger, Ivo Jansch, Jerry K u c h , J ohnathan Meehan, John Mertic, Luigi Montanez, Karl Pfalzer, Luke Pillow, Christophe Portneuve, T o m Sartain, Stefan T u r a l s k i , T o m V a n Herreweghe, Matt W a r r e n , and Nick W a t t s . No acknowledgments for a b ook on an open source tool wo u l d be complete without acknowledging the wo r k of the legion of v o l u n t e e r s who made the project possible. A h uge debt is o w e d by a ll of us who use Git to the nearly 700 people who have contributed to the project. My family and friends, in particular my wife (whom I’m lucky enough to count as both), have been amazing—as always. W i t h o u t their support, and that of the rest of my fa m i l y and friends, t his book w o u l d not have happened. Download from Wow! eBook <www.wowebook.com> Intr oduction The wo r ld of v e r s i o n control systems (VCSs) has undergone a major shift o v e r the past few years. F a s t , reliable, and approachable distributed v e r s i o n control systems ( DVCSs) s uch as Git ha ve b u r s t onto the scene and c hanged the landscape of open source software development and corporate software wo r kfl ow s . This book is your guide t o this new p aradigm. It’s n ot a complete reference; instead, it focuses on getting you up and running quickly . P ragmatic Guide to Git covers the 95 p ercent of Git that you’ll use at least once a week, as well as a few tasks that will come in h andy b u t aren’t used as often. Git started when the license of VCS software that the Linux ke r n e l used to track changes w a s revoked. After investigating the other alternatives, Linus T o r v a l d s decided he could write a better ve r s i o n control system i n a few weeks than what c urrently ex i s t e d , so he set off to do t hat. Git, then in a v e r y rough form, wa s the result of that two weeks of hacking together some s hell scripts b ack in the spring of 2005. Linus had to calculate pieces of the commits by hand on the first few commits (commits are the changes Git tracks for you). Since those original hand-rolled commits, Git has become the l eader in the field o f D V C S . Who Is This Book For? This book is geared for someone new to Git who is looking to get up to speed quickly. This book is for you if you’ re already familia r with another VCS such a s Subversion and are l ooking for a quick g uide to the Git landscape or if you’re a quick study and w a n t a concise guide. It’ s organized by task to make it easy to translate from the task you need to accomplish to how the process w o r k s in Git. If y ou’ ve never used a ve r s i o n control system before a nd thought Subversion wa s something you d id t o o v e r t h r o w governments, t his b ook will get you up and r unning with Git. Fo r much more detail on ve r s i o n control concepts, you should read Pragmatic V e r s i o n Control Using Git, 1 my other book, a s well. 1. http://pragprog.com/titles/tsgit/ Download from Wow! eBook <www.wowebook.com> HOW TOREADTHISBOOK 1 0 How to Read This Book This book is organized in parts to guide you from starting out t hrough more complex situations, with each part broken down into tasks. T a s k s follow a specific f ormula: t he left page ex p l a i n s the t ask and t he commands r elated to it, and the right page gives you the raw commands with a little bit of informa- tion about them and a cross-reference t o related t asks. Y o u can read this book in paper form as an open book to see the tasks side by side, b u t it’s also an e x c e l l e n t reference in digital form, especially when searching for a particular Git task. If you’ re reading a digital v e r s i o n of this book on a computer with a large enough display, I recommend setting your reader to display two pages side by side instead of a single page. That gives you the same visual that’s intended in the book. On y our first pass, I suggest that you read the introductions to each part. They give you a broad o v e r v i e w of how to approach e ach part o f the Git wo r kfl ow, as well as a synopsis o f the t asks contained in that p art. Armed with high-level information, y ou can determine where t o dive in. Y o u can read this book from start to finish or cherry-pick the tasks relevant to what you’ re trying to accomplish. The parts of this book are organized to wa lk you through the v a r i o u s phases of u se in Git. • P a r t I, Getting Started, starts with the absolute basics—installing and configuring Git and creating your fi rst r epository. • P a r t II, W o r k i n g with Git, covers the basic commands you need as part o f your d ay-to-day i nteraction with Git by y ourself. These are the b u i l d i n g blocks, and they’re a must-read if this i s your first time u sing Git. • P a r t III, Organizing Y o u r Repository with Branches and T a g s , intro- duces branches, a powerful a nd central part of Git t hat’ s necessary f or understanding how e v e r y t h i n g wo r ks together. • P a r t IV, W o r k i n g with a T e a m , c o vers the most powerful aspect o f any VCS: collaborating with other developers. This part gets you up to speed on how to share your w o rk with other developers and retrieve changes from them. • P a r t V , Branches and Merging Revisited, b u i l d s on the information in P a r t III and teaches you how to handle it when things go wrong, as Report erratum this copy is (P1.0 printing, October 2010) Download from Wow! eBook <www.wowebook.com> [...]... some-repository prompt> git init For example, to create a repository called widgets in the /work directory, use this: prompt> mkdir /work/widgets prompt> cd /work/widgets prompt> git init Initialized empty Git repository in /work/widgets/ .git/ Create a repository in an existing directory, and add all files from that directory prompt> prompt> prompt> prompt> cd /path /to/ some/directory git init git add git. .. prompt> git config global user.email "user@domain.com" prompt> Set the Git user for a specific repository prompt> cd /path /to/ repository prompt> git config user.name "Your Name" prompt> git config user.email "user@domain.com" prompt> Turn colors on wherever possible in the Git UI prompt> git config global color.ui auto prompt> Configure Git s editor prompt> git config global core.editor /path /to/ editor... (P1.0 printing, October 2010) 23 C REATING A N EW R EPOSITORY 3 Creating a New Repository Repositories in Git are stored on your local file system right alongside the code they track You create a repository by typing git init in the directory that you want to start tracking files in You use two repositories in Git to collaborate with others: a private one and a public one Your private repository—the one we’re... auto setting tells Git to use color whenever it is generating output to be displayed but to render plain text whenever the output is being piped to another process This makes it easy to output a raw diff—the changes between two versions of the file to a file but still allows you to see the colorized diff when you view the output directly Finally, Git uses core.editor to specify a particular editor Git. .. You use git clone to fetch changes when a repository already exists, but you don’t have to clone a repository to work with a remote repository Remote repositories are repositories that you can talk to, generally over a network, to push and pull changes from You can initialize a new repository, like we talked about in Task 3, Creating a New Repository, on page 24, and then add a remote repository later... git- core To install the user manual, do this: prompt> sudo apt-get install git- doc To install the Git to Subversion functionality, do this: prompt> sudo apt-get install git- svn You can use apt-get to handle all of the dependencies and then compile Git from source using the previous steps prompt> sudo apt-get build-dep git- core git- doc git- svn Install Git on OS X You can use MacPorts and install Git with... a git directory in your current directory and initializes the Git repository inside that Once you’ve initialized a repository, you still need to add and commit the files using git add (see Task 6, Staging Changes to Commit, on page 34) and git commit (see Task 7, Committing Changes, on page 36), respectively, but both of these require an initialized repository first You have to initialize the repository... printing, October 2010) 32 S EEING W HAT H AS C HANGED What the status of a new repository looks like If you just created a repository using git init, this is what your repository looks like: prompt> git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track) What git status looks like in a repository with changes git status requires a repository with... directory prompt> git add prompt> Add all files in the current repository prompt> git add -A| all prompt> Add all tracked files that have been changed prompt> git add -u| update prompt> Choose which changes to commit prompt> git add -p| patch or a specific file prompt> git add -p path /to/ file prompt> Open the current diff in the editor prompt> git add -e or a specific file prompt> git add -e path /to/ file... particular editor Git launches an editor whenever you need to create a commit message, edit patches, and do a few other tasks Git doesn’t require you to set the core.editor value, though It tries to figure out what editor to use by checking the following values, in order: GIT_ EDITOR environment variable; core.editor configuration value; VISUAL environment variable; EDITOR environment variable; and, finally, . Creating a New Repository, on page 24. Y o u learn how to create a completely new repository. • Another w a y to start a Git repository is to create a clone of someone else’s repository, covered in. an use t he msysGit 7 installer to get up and running, and OS X users can install Git using t he Git OS X Installer 8 to install Git v ia a DMG. The original wa y to install Git, which is still. access to push changes to the same public repository, or each developer may have their o w n public repositories. Y o u can p ush to and fetch from multiple r epositories. This allows you to pull in