Pragmatic Guide to Git
Total Page:16
File Type:pdf, Size:1020Kb
Download from Wow! eBook <www.wowebook.com> What Readers Are Saying About Pragmatic Guide to Git I’d heard a lot of the hype surrounding Git. It wasn’t until I read T r a v i s ’ book that I learned why people are so enthusiastic about it. T r a v i s does a great job e x p l a i n i n g the power of Git in a digestible format. Ivo J a n s c h PHP evangelist, author, and founder, Egeniq.com Git can be intimidating and frustrating to new users. Pragmatic Guide to Git alleviates that pain with a straightforward, concise walk-through that arms readers with e x a c t l y what they need to use Git productively. Luigi Montanez Software developer, Sunlight Labs This book is a must-have for anyone using Git or just getting started with Git. It has saved me time in finding the best practices for managing my Git repositories and will sit on my bookshelf as the go-to resource for anything Git. J o h n Mertic Senior software engineer, SugarCRM W i t h two years of e x p e r i e n c e with Git, I thought I would have known most e v e r y t h i n g in Pragmatic Guide to Git. After reading it cover to cover, I learned that’s not the case. It’s a well-organized collection of useful Git techniques for all audiences. Luke Pillow Software engineer, pillowfactory.org Download from Wow! eBook <www.wowebook.com> Pragmatic Guide to Git T r a v i s Swicegood The Pragmatic 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. Where those designations appear in this book, and The Pragmatic Pro- grammers, 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. 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 Peter Production: Janet Furlow Customer support: Ellie Callahan International: Juliet Benda Copyright © 2010 Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, 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-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 Is This Book F o r ? . 9 How to Read This Book . 10 How Git Is Different . 12 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 of an Existing 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 Files 38 T a s k 9. Undoing Uncommitted Changes 40 T a s k 10. Moving Files in Git 42 T a s k 11. Deleting Files in 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 by 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, Part II 74 T a s k 22. Sending Changes to 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. Temporarily 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 Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010) 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 Fixing Things 108 T a s k 35. Fixing Commits 110 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 Subversion 128 T a s k 44. Initializing Bare Repositories 130 A Glossary 132 Index 136 Report erratum Download from Wow! eBook <www.wowebook.com> this copy is (P1.0 printing, October 2010) 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 words. Please give me a few minutes to thank those involved in bringing 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 became this book. Next, Dave, Andy, and the entire crew at Pragmatic Bookshelf have been great to work with a second time. Both books I’ve written for them have been gambles—first as a rookie 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 was al- ways there with advice, tips, the occasional tough love, and an ever-optimistic attitude; e v e r y author should be so lucky. Reviewers of early drafts of this book provided me with a tremendous amount of constructive feedback that helped shaped this book into 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 , Johnathan 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 book on an open source tool would be complete without acknowledging the work of the legion of v o l u n t e e r s who made the project possible. A huge debt is o w e d by all 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 family and friends, this book would not have happened. Download from Wow! eBook <www.wowebook.com> Introduction The world 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) such as Git have b u r s t onto the scene and changed the landscape of open source software development and corporate software workflows.