Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Git in Practice
Git in Practice
Git in Practice
Ebook445 pages5 hours

Git in Practice

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

Summary

Git in Practice is a collection of 66 tested techniques that will optimize the way you and your team manage your development projects. The book begins with a brief reminder of the core version control concepts you need when using Git and moves on to the high-value features you may not have explored yet. Then, you'll dig into cookbook-style techniques like history visualization, advanced branching and rewriting history each presented in a problem-solution-discussion format. Finally you'll work out how to use Git to its full potential through configuration, team workflows, submodules and using GitHub pull requests effectively.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

Git is a source control system, but it's a lot more than just that. For teams working in today's agile, continuous delivery environments, Git is a strategic advantage. Built with a decentralized structure that's perfect for a distributed team, Git manages branching, committing, complex merges, and task switching with minimal ceremony so you can concentrate on your code.

About the Book

Git in Practice is a collection of battle-tested techniques designed to optimize the way you and your team manage development projects. After a brief overview of Git's core features, this practical guide moves quickly to high-value topics like history visualization, advanced branching and rewriting, optimized configuration, team workflows, submodules, and how to use GitHub pull requests. Written in an easy-to-follow Problem/Solution/Discussion format with numerous diagrams and examples, it skips the theory and gets right to the nitty-gritty tasks that will transform the way you work.

Written for developers familiar with version control and ready for the good stuff in Git.

What's Inside
  • Team interaction strategies and techniques
  • Replacing bad habits with good practices
  • Juggling complex configurations
  • Rewriting history and disaster recovery

About the Author

Mike McQuaid is a software engineer at GitHub. He's contributed to Qt and the Linux kernel, and he maintains the Git-based Homebrew project.

Table of Contents
    PART 1 INTRODUCTION TO GIT
  1. Local Git
  2. Remote Git
  3. PART 2 GIT ESSENTIALS
  4. Filesystem interactions
  5. History visualization
  6. Advanced branching
  7. Rewriting history and disaster recovery
  8. PART 3 ADVANCED GIT
  9. Personalizing Git
  10. Vendoring dependencies as submodules
  11. Working with Subversion
  12. GitHub pull requests
  13. Hosting a repository
  14. PART 4 GIT BEST PRACTICES
  15. Creating a clean history
  16. Merging vs. rebasing
  17. Recommended team workflows
LanguageEnglish
PublisherManning
Release dateSep 29, 2014
ISBN9781638352174
Git in Practice
Author

Mike McQuaid

Mike McQuaid is a software engineer at GitHub. He's contributed to Qt and the Linux kernel, and he maintains the Git-based Homebrew project.

Related to Git in Practice

Related ebooks

Software Development & Engineering For You

View More

Related articles

Reviews for Git in Practice

Rating: 4.166666666666667 out of 5 stars
4/5

3 ratings1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    Git in Practice is a very good "learn by practice" experience aboutworking with Git repositories. The books presents various real-world problems equipped with solution and discussion.Basic and advanced concepts are clearly presented and quickly verifiableby running few commands that highlight the power and expressiveness of Git.Particular attention is dedicated to rewrite history and disaster recovery, which are options unavailable to previous generations of source code management tools. A significant emphasis is addressed tohow creating a clean history and how valuable is having a good history.The author brings his working experience both describing variousworkflows, that show the Git flexibility in adapting to different software development life-cycle, and giving extremely useful hintsabout which branching strategies suits best to our project needs.

Book preview

Git in Practice - Mike McQuaid

Copyright

For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact

     Special Sales Department

     Manning Publications Co.

     20 Baldwin Road

     PO Box 761

     Shelter Island, NY 11964

     Email: 

orders@manning.com

©2015 by Manning Publications Co. All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.

Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.

ISBN 9781617291975

Printed in the United States of America

1 2 3 4 5 6 7 8 9 10 – EBM – 19 18 17 16 15 14

Brief Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Foreword

Preface

Acknowledgments

About this Book

About the Cover Illustration

1. Introduction to Git

Chapter 1. Local Git

Chapter 2. Remote Git

2. Git essentials

Chapter 3. Filesystem interactions

Chapter 4. History visualization

Chapter 5. Advanced branching

Chapter 6. Rewriting history and disaster recovery

3. Advanced Git

Chapter 7. Personalizing Git

Chapter 8. Vendoring dependencies as submodules

Chapter 9. Working with Subversion

Chapter 10. GitHub pull requests

Chapter 11. Hosting a repository

4. Git best practices

Chapter 12. Creating a clean history

Chapter 13. Merging vs. rebasing

Chapter 14. Recommended team workflows

Appendix A. Git installation

Appendix B. Creating a GitHub account and repository

Appendix C. Commented Git configuration

Appendix D. Why use version control?

Index

List of Figures

List of Tables

List of Listings

Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Foreword

Preface

Acknowledgments

About this Book

About the Cover Illustration

1. Introduction to Git

Chapter 1. Local Git

1.1. Why do programmers use Git?

1.2. Initial setup

Technique 1 Creating a repository: git init

1.3. .git subdirectory

1.4. Creating a new commit: git add, git commit

Technique 2 Building a new commit in the index staging area: git add

Technique 3 Committing changes to files: git commit

1.4.1. Object store

1.4.2. Parent commits

1.5. Rewriting history

Technique 5 Viewing the differences between commits: git diff

1.6. Diff formats

1.7. Refs

1.8. Summary

Chapter 2. Remote Git

Technique 6 Adding a remote repository: git remote add

2.1. Authoritative version storage

Technique 7 Pushing changes to a remote repository: git push

Technique 8 Cloning a remote/GitHub repository onto your local machine: git clone

Technique 9 Pulling changes from another repository: git pull

Technique 10 Fetching changes from a remote without modifying local branches: git fetch

Technique 11 Creating a new local branch from the current branch: git branch

Technique 12 Checking out a local branch: git checkout

Technique 13 Pushing a local branch remotely

Technique 14 Merging an existing branch into the current branch: git merge

2.2. Merge conflicts

2.3. Rebasing

Technique 15 Deleting a remote branch

Technique 16 Deleting the current local branch after merging

2.4. Summary

2. Git essentials

Chapter 3. Filesystem interactions

Technique 17 Renaming or moving a file: git mv

Technique 18 Removing a file: git rm

Technique 19 Resetting files to the last commit: git reset

Technique 20 Deleting untracked files: git clean

Technique 21 Ignoring files: .gitignore

Technique 22 Deleting ignored files

Technique 23 Temporarily stashing some changes: git stash

Technique 24 Reapplying stashed changes: git stash pop

Technique 25 Clearing stashed changes: git stash clear

Technique 26 Assuming files are unchanged

Technique 27 Listing assumed-unchanged files

Technique 28 Stopping assuming files are unchanged

3.1. Summary

Chapter 4. History visualization

Technique 29 Listing only certain commits

4.1. git show

Technique 30 Listing commits with different formatting

4.2. Custom output format

4.3. Releasing logs: git shortlog

4.4. The ultimate log output

Technique 31 Showing who last changed each line of a file: git blame

Technique 32 Finding which commit caused a particular bug: git bisect

4.5. Automating git bisect

4.6. Summary

Chapter 5. Advanced branching

Technique 33 Merging branches and always creating a merge commit

5.1. Merge strategies

Technique 34 Resolving a merge conflict

5.2. Using a graphical merge tool

Technique 35 Resolving each merge conflict only once: git rerere

Technique 36 Creating a tag: git tag

Technique 37 Generating a version number based on previous tags: git describe

Technique 38 Adding a single commit to the current branch: git cherry-pick

Technique 39 Reverting a previous commit: git revert

Technique 40 Listing what branches contain a commit: git cherry

5.3. Summary

Chapter 6. Rewriting history and disaster recovery

Technique 41 Listing all changes including history rewrites: git reflog

6.1. Avoiding and recovering from disasters

Technique 42 Resetting a branch to a previous commit: git reset

Technique 43 Rebasing commits on top of another branch: git rebase

Technique 44 Rebasing commits interactively: git rebase --interactive

Technique 45 Pulling a branch and rebasing commits: git pull --rebase

Technique 46 Rewriting history on a remote branch: git push --force

Technique 47 Rewriting the entire history of a branch: git filter-branch

6.2. Summary

3. Advanced Git

Chapter 7. Personalizing Git

Technique 48 Setting the configuration for all repositories

Technique 49 Setting the configuration for a single repository

7.1. Useful configuration settings

7.1.1. Colored output in Git

7.1.2. Git 2.0’s push defaults

7.1.3. Pruning branches automatically

7.1.4. Ignoring files across all repositories: global ignore file

7.1.5. Displaying help output in your web browser

7.1.6. Storing passwords in the OS X keychain

7.1.7. Storing arbitrary text in Git configuration

7.1.8. Autocorrecting misspelled commands

7.2. Sharing your Git (or other) configuration between machines

Technique 51 Showing the current branch in your terminal prompt

7.3. Summary

Chapter 8. Vendoring dependencies as submodules

8.1. When are submodules useful?

Technique 52 Adding a git submodule: git submodule add

Technique 53 Showing the status of submodules: git submodule status

Technique 54 Updating and initializing all submodules: git submodule update --init

Technique 55 Running a command in every submodule: git submodule foreach

8.2. Summary

Chapter 9. Working with Subversion

Technique 56 Importing an SVN repository into a Git repository

9.1. Subversion branches and tags

9.2. Subversion ignore rules

9.3. Updating a Subversion repository

9.4. Subversion authors and committers

9.5. Viewing a Subversion repository in GitX

9.6. Migrating a Subversion repository to Git

Technique 57 Committing and pushing to an SVN repository from a Git repository

9.7. Local branching and tagging

Technique 58 Accessing a GitHub repository with Subversion

9.8. Summary

Chapter 10. GitHub pull requests

10.1. What are pull requests and forks?

10.2. Interacting with GitHub from the command-line: gh

Technique 59 Making a pull request in the same repository: gh pull-request

Technique 60 Making a pull request from a forked repository: gh fork

Technique 61 Merging a pull request from the same repository

Technique 62 Merging a pull request from a forked repository: gh merge

10.3. Summary

Chapter 11. Hosting a repository

Technique 63 Initializing a local repository in a server hosting format: git init --bare

Technique 64 Mirroring a repository: git clone --mirror

Technique 65 Sharing a repository with other users on the same network: git daemon

Technique 66 Displaying a repository in a browser: git instaweb

11.1. Advanced Git hosting

11.2. Summary

4. Git best practices

Chapter 12. Creating a clean history

12.1. Writing a good commit message

12.2. Building a commit from parts of files: git add --patch

12.3. Graphically building a commit from parts of files

12.3.1. Graphically building a commit in GitX

12.3.2. Graphically building a commit in Git Gui

12.4. Avoiding whitespace issues: git diff --check

12.5. Summary

Chapter 13. Merging vs. rebasing

13.1. CMake’s workflow

13.1.1. Workflow commands

13.2. Homebrew’s workflow

13.2.1. Workflow commands

13.3. CMake workflow pros and cons

13.4. Homebrew workflow pros and cons

13.5. Picking your strategy

13.6. Summary

Chapter 14. Recommended team workflows

14.1. GitHub Flow

14.1.1. Pros and cons

14.2. Git Flow

14.2.1. Pros and cons

14.3. Mike Flow

14.3.1. Mike Flow Single

14.3.2. Mike Flow Multiple

14.3.3. Pros and cons

14.4. Which workflow is for you?

14.5. Summary

Appendix A. Git installation

A.1. How to install and run Git on Apple OS X

A.1.1. Installing Git on Apple OS X Mavericks or newer

A.1.2. Installing Git on Apple OS X Mountain Lion or older

A.2. How to install and run Git on Linux or Unix

A.3. How to install and run Git on Microsoft Windows

A.4. Verifying that Git has installed correctly

Appendix B. Creating a GitHub account and repository

B.1. Signing up for a GitHub account

B.2. Creating a new repository on GitHub

Appendix C. Commented Git configuration

Appendix D. Why use version control?

D.1. Handling changes

D.2. Version control workflow

D.3. Version control for programmer collaboration

Index

List of Figures

List of Tables

List of Listings

Foreword

I first heard of Git probably nine years ago, around the end of 2005, right around when v1.0 was being tagged. Reading through my friend Mike’s book, it amazes me just how much has changed in what I’m only now realizing has been nearly a decade of history.

At the time I was introduced to Git, it was still very much more of a collection of tools for content management and less a version control system. It was installed as hundreds of commands such as git-update-index instead of the single git command we now call. In fact, in my first exposure to it, I used it at work as a transport mechanism for media content, instead using Perforce to version-control the code we wrote around Git.

It fascinated me how clean and simple the data model was and how easy it was to manipulate to do anything you wanted, not simply what was prescribed and intended. Equally fascinating was how difficult it was to learn. It took my friend weeks to drill into me how this thing worked, but after that I was totally hooked and have spent most of the decade since helping people use it and understand it.

If you’re still struggling with Git, this book does a great job of leading you through the basics in a straightforward manner. If you’re already comfortable with those, parts 3 and 4 will help you see other ways you could be using Git to make your team’s workflows simpler or more effective. Although I’ve spent a lot of time learning and explaining to others the elegant simplicity of the data model, I’ve never helped run a huge open source effort such as Homebrew as Mike has, and that experience is invaluable in explaining more advanced and complex workflows as Mike describes here.

I hope you enjoy this book, and I hope you use it to understand and enjoy the amazing benefits that a tool like Git can bring to your daily development life. I’m proud of Mike for taking on this challenge and for producing a great book on a tool that has made the transition over the past decade from being an interesting hobbyist’s script to an essential part of millions of software developers’ daily lives.

SCOTT CHACON

AUTHOR OF PRO GIT

Preface

As all good software engineers will agree: version control is important. I was first introduced to version control while studying computer science at university and dabbling with open source in my free time. CVS and Subversion started being used for managing our small pieces of coursework, group projects, and larger open source projects we interacted with. Initially it all felt a bit like overkill: why do I need this when I’m working by myself? It only took a few personal screw-ups for me to understand why version control is so important.

I was introduced to Git in 2006 while working on audio drivers for the Linux kernel at a summer internship. It was a fairly terrifying beast in those days: incredibly fast compared to its competitors, impressive in how it managed almost everything offline, but with an arcane user interface that was full of jargon. The jargon has decreased, but everything else remains fairly similar: Git is fast, impressive, and confusing.

My first few jobs after graduating found most companies still using Subversion. Git was one of those things used by kernel hackers and the particularly hardcore open source crowd, and it wasn’t ready for mainstream use yet. I liked the glimpse I’d had of it, so I continued using it stealthily with git svn, a bridge that allows you to access Subversion repositories using Git. Eventually GitHub came along and convinced people that Git could be easy to use, and I was able to use Git more regularly. I became a maintainer of the Homebrew Mac OS X package manager, and more employers and clients began to use GitHub.

I think because I was a relatively early adopter of Git and the type of person who needs to know how things work, I ended up helping others with Git: first at my own workplace and then through training courses, talks, and helping others in the open-source community. Eventually I was contacted by another publisher about writing a Git book, and I contacted Manning to ask if I could write with them. The result is Git in Practice: a book that takes you from intermediate- to advanced-level Git tools and workflows that I’ve found useful while working as a software engineer.

Acknowledgments

There are many people without whom this book wouldn’t be a reality. First, I’d like to thank Dan Maharry, my development editor at Manning, who kept me motivated and provided excellent feedback that has resulted in a far better book than I could have created on my own. Additionally, many others who work at and with Manning have helped me along the way, particularly Jay Bryant, Michael Stephens, Bert Bates, Benjamin Berg, Glenn Burnside, Tiffany Taylor, and Ozren Harlovic.

Thanks to Scott Chacon for his great book Pro Git, which helped me while I was learning Git, and for agreeing to write the foreword.

Thanks to Karsten Strøbæk for his technical proofread of the manuscript shortly before it went into production, to Gerd Koenig for his help, and to the following reviewers who provided invaluable feedback: Alan Lenton, Art Bergquist, Chris Couchoud, Clare Macrae, Corey Gallon, Gregor Zurowski, Jean-Pol Landrain, John Guthrie, Ken Fricklas, Nathan Davies, Patrick Toohey, Rick Umali, Tarin Gamberini, and Zarko Jovicic.

Thanks to Linus Torvalds, without whom this wonderful version control system would not exist, and to Tom Preston-Werner, Chris Wanstrath, and PJ Hyett for starting GitHub, without which Git wouldn’t have nearly the widespread usage it does today. Also thanks to them for hiring me!

Finally, I’d like to thank my wife Lindsay for putting up with my occasional whining about writing this book and for her understanding about how much time I had to spend on it.

About this Book

Git in Practice is not a comprehensive guide of every single command and feature available with Git. A book that covered this would be considerably longer than the one you find before you, and you’d learn a lot of unnecessary information. Instead, this book provides a detailed understanding of all the features of Git that are useful when you’re using Git to manage source code for software projects. I decided on the contents by looking at my Terminal shell history and prioritizing commands based on how much I use them. I also discuss workflows and best practices used in the real world to organize software projects when using Git.

This book isn’t written for every software developer but, as an In Practice book, aims at the intermediate to advanced level. To get the most out of this book, you should be able to relate to or exceed the knowledge of one of these target readers:

Software developer who uses Git and knows enough to interact with a Git repository but lacks understanding of any features beyond those provided by other version control systems

Software developer with advanced Subversion knowledge (able to branch, merge, and set/get Subversion metadata variables) who wishes to learn how to use Git

Software developer who wishes to learn Git best practices to contribute to open source projects on GitHub

Trainer with Git experience who wants to learn about more conceptual topics, commands they may have missed, and workflows, and who wants a resource to help coach others about using Git

System administrator who has a good understanding of version control but wants to create Git repositories for scripts and/or host repositories for teams

If you don’t know this much yet, don’t worry. You can either try to work through this book and reread parts you struggle with or pick up a beginner’s guide to Git through Manning, another publisher, or online and come back to Git in Practice afterward.

This book also has some recommended prerequisite knowledge and tools:

Command line—I assume you’re familiar with the command line, because Git is only used as a command-line tool in this book. For example, you need to be able to create, edit, and remove directories and files from the command line. This includes Windows users, who will need to install MSYSGit (which provides Git in a Unix shell on Windows).

Software development—This book assumes a basic understanding of (and desire to learn more about) how version control is used by a team of software developers to build software projects.

Git or version control—You should understand versions, branching, and merging in Git (such as git commit) or another version control system (such as Subversion commit, log, copy, switch, and merge).

GitHub—You should be able to access a free GitHub account.

Programming languages—You don’t need to know any particular programming language.

Again, if you don’t know any of this, don’t worry. The book may be trickier for you, but it shouldn’t be impossible to work through, and there’s nothing that another beginner title or a search online won’t be able to help you with.

Many of the chapters are organized into numbered Techniques, and these in turn consist of a Problem, Solution, and Discussion section for each Technique.

Roadmap

Part 1 (chapters 1 and 2) flies through the basics of using Git while teaching you the underlying concepts that are often misunderstood or omitted in beginners’ guides. You may know how to use Git already, but I encourage you to persevere through this part anyway; it sets a good foundation that the rest of the book builds on:

Chapter 1 covers why Git was created and how to create and interact with a local Git repository by committing files, viewing history, examining changes between commits, and using graphical tools.

Chapter 2 discusses connecting your local Git repository to other repositories over the internet, allowing you to send and receive changes.

Part 2 (chapters 3–6) covers the most essential commands to learn when using Git either alone or with a team. This is where you’ll start to see some of the more powerful differences between Git and other version control systems, with advanced branching capabilities and history rewriting:

Chapter 3 discusses using interacting with files and directories in a Git repository, ignoring files or changes, deleting files based on their status in the repository, resetting files to a previous state, and temporarily storing changes to files to be reapplied later.

Chapter 4 covers using Git’s history-querying capabilities and configuring history to display the most useful information for any particular task.

Chapter 5 examines using branches effectively with merges, dealing with conflicts, tagging, and reverting commits.

Chapter 6 covers changing previous commits in a Git repository (known as rewriting history) and avoiding losing your work when doing so or through data corruption.

Part 3 (chapters 7–11) provides some bonus Git skills that will help you be more efficient. These chapters introduce a collection of techniques that won’t be used with every Git project but may be important for new ones:

Chapter 7 covers personalizing Git’s configuration so you can work most effectively and efficiently given the current project, computer, or version of Git you’re working on.

Chapter 8 discusses using Git’s submodules to reference other Git repositories from inside a Git repository. This is particularly useful when you’re dealing with software dependencies.

Chapter 9 covers interacting with Subversion repositories using Git either as a day-to-day workflow or as a one-time import and migration of a Subversion repository to Git.

Chapter 10 examines using GitHub’s pull request and forks features to work effectively in teams and contribute to open source software.

Chapter 11 covers how Git repositories are hosted and migrated.

Part 4 (chapters 12–14) discusses different workflows and best practices for using Git as part of a team of software developers. You will be able to compare and contrast differing approaches and decide which is best for your team:

Chapter 12 covers how to ensure that your commit history is clean, readable, and useful by using small, meaningful commits.

Chapter 13 discusses the CMake and Homebrew open source projects’ different workflows and compares them to examine the differences between rebase-heavy and merge-heavy workflows.

Chapter 14 examines the most widely used workflows and my recommended workflows for working with Git as part of a software team, and how to manage branching, merging, rebasing, and releasing new versions of software.

The book also has four appendixes:

Appendix A explains how to install Git on Windows, Mac, Linux, and Unix.

Appendix B covers creating a GitHub account and repository for use throughout this book.

Appendix C examines my Git configuration file: what I’ve customized, how, and why.

Appendix D discusses why you should use version control

Enjoying the preview?
Page 1 of 1