Version Control with Git, 2nd Edition

Book description

Get up to speed on Git for tracking, branching, merging, and managing code revisions. Through a series of step-by-step tutorials, this practical guide takes you quickly from Git fundamentals to advanced techniques, and provides friendly yet rigorous advice for navigating the many functions of this open source version control system.

This thoroughly revised edition also includes tips for manipulating trees, extended coverage of the reflog and stash, and a complete introduction to the GitHub repository. Git lets you manage code development in a virtually endless variety of ways, once you understand how to harness the system’s flexibility. This book shows you how.

  • Learn how to use Git for several real-world development scenarios
  • Gain insight into Git’s common-use cases, initial tasks, and basic functions
  • Use the system for both centralized and distributed version control
  • Learn how to manage merges, conflicts, patches, and diffs
  • Apply advanced techniques such as rebasing, hooks, and ways to handle submodules
  • Interact with Subversion (SVN) repositories—including SVN to Git conversions
  • Navigate, use, and contribute to open source projects though GitHub

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Audience
    2. Assumed Framework
    3. Book Layout and Omissions
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
    9. Attributions
  2. 1. Introduction
    1. Background
    2. The Birth of Git
    3. Precedents
    4. Timeline
    5. What’s in a Name?
  3. 2. Installing Git
    1. Using Linux Binary Distributions
      1. Debian/Ubuntu
      2. Other Binary Distributions
    2. Obtaining a Source Release
    3. Building and Installing
    4. Installing Git on Windows
      1. Installing the Cygwin Git Package
      2. Installing Standalone Git (msysGit)
  4. 3. Getting Started
    1. The Git Command Line
    2. Quick Introduction to Using Git
      1. Creating an Initial Repository
      2. Adding a File to Your Repository
      3. Configuring the Commit Author
      4. Making Another Commit
      5. Viewing Your Commits
      6. Viewing Commit Differences
      7. Removing and Renaming Files in Your Repository
      8. Making a Copy of Your Repository
    3. Configuration Files
      1. Configuring an Alias
    4. Inquiry
  5. 4. Basic Git Concepts
    1. Basic Concepts
      1. Repositories
      2. Git Object Types
      3. Index
      4. Content-Addressable Names
      5. Git Tracks Content
      6. Pathname Versus Content
      7. Pack Files
    2. Object Store Pictures
    3. Git Concepts at Work
      1. Inside the .git Directory
      2. Objects, Hashes, and Blobs
      3. Files and Trees
      4. A Note on Git’s Use of SHA1
      5. Tree Hierarchies
      6. Commits
      7. Tags
  6. 5. File Management and the Index
    1. It’s All About the Index
    2. File Classifications in Git
    3. Using git add
    4. Some Notes on Using git commit
      1. Using git commit --all
      2. Writing Commit Log Messages
    5. Using git rm
    6. Using git mv
    7. A Note on Tracking Renames
    8. The .gitignore File
    9. A Detailed View of Git’s Object Model and Files
  7. 6. Commits
    1. Atomic Changesets
    2. Identifying Commits
      1. Absolute Commit Names
      2. refs and symrefs
      3. Relative Commit Names
    3. Commit History
      1. Viewing Old Commits
      2. Commit Graphs
        1. Using gitk to View the Commit Graph
      3. Commit Ranges
    4. Finding Commits
      1. Using git bisect
      2. Using git blame
      3. Using Pickaxe
  8. 7. Branches
    1. Reasons for Using Branches
    2. Branch Names
      1. Dos and Don’ts in Branch Names
    3. Using Branches
    4. Creating Branches
    5. Listing Branch Names
    6. Viewing Branches
    7. Checking out Branches
      1. A Basic Example of Checking out a Branch
      2. Checking out When You Have Uncommitted Changes
      3. Merging Changes into a Different Branch
      4. Creating and Checking out a New Branch
      5. Detached HEAD Branches
    8. Deleting Branches
  9. 8. Diffs
    1. Forms of the git diff Command
    2. Simple git diff Example
    3. git diff and Commit Ranges
    4. git diff with Path Limiting
    5. Comparing How Subversion and Git Derive diffs
  10. 9. Merges
    1. Merge Examples
      1. Preparing for a Merge
      2. Merging Two Branches
      3. A Merge with a Conflict
    2. Working with Merge Conflicts
      1. Locating Conflicted Files
      2. Inspecting Conflicts
        1. git diff with conflicts
        2. git log with conflicts
      3. How Git Keeps Track of Conflicts
      4. Finishing Up a Conflict Resolution
      5. Aborting or Restarting a Merge
    3. Merge Strategies
      1. Degenerate Merges
      2. Normal Merges
        1. Recursive merges
        2. Octopus merges
      3. Specialty Merges
      4. Applying Merge Strategies
      5. Merge Drivers
    4. How Git Thinks About Merges
      1. Merges and Git’s Object Model
      2. Squash Merges
      3. Why Not Just Merge Each Change One by One?
  11. 10. Altering Commits
    1. Caution About Altering History
    2. Using git reset
    3. Using git cherry-pick
    4. Using git revert
    5. reset, revert, and checkout
    6. Changing the Top Commit
    7. Rebasing Commits
      1. Using git rebase -i
      2. rebase Versus merge
  12. 11. The Stash and the Reflog
    1. The Stash
    2. The Reflog
  13. 12. Remote Repositories
    1. Repository Concepts
      1. Bare and Development Repositories
      2. Repository Clones
      3. Remotes
      4. Tracking Branches
    2. Referencing Other Repositories
      1. Referring to Remote Repositories
      2. The refspec
    3. Example Using Remote Repositories
      1. Creating an Authoritative Repository
      2. Make Your Own Origin Remote
      3. Developing in Your Repository
      4. Pushing Your Changes
      5. Adding a New Developer
      6. Getting Repository Updates
        1. The fetch step
        2. The merge or rebase step
        3. Should you merge or rebase?
    4. Remote Repository Development Cycle in Pictures
      1. Cloning a Repository
      2. Alternate Histories
      3. Non–Fast-Forward Pushes
      4. Fetching the Alternate History
      5. Merging Histories
      6. Merge Conflicts
      7. Pushing a Merged History
    5. Remote Configuration
      1. Using git remote
      2. Using git config
      3. Using Manual Editing
    6. Working with Tracking Branches
      1. Creating Tracking Branches
      2. Ahead and Behind
    7. Adding and Deleting Remote Branches
    8. Bare Repositories and git push
  14. 13. Repository Management
    1. A Word About Servers
    2. Publishing Repositories
      1. Repositories with Controlled Access
      2. Repositories with Anonymous Read Access
        1. Publishing repositories using git-daemon
        2. Publishing repositories using an HTTP daemon
        3. Publishing a repository using Smart HTTP
        4. Publishing via Git and HTTP daemons
      3. Repositories with Anonymous Write Access
      4. Publishing Your Repository to GitHub
    3. Repository Publishing Advice
    4. Repository Structure
      1. The Shared Repository Structure
      2. Distributed Repository Structure
      3. Repository Structure Examples
    5. Living with Distributed Development
      1. Changing Public History
      2. Separate Commit and Publish Steps
      3. No One True History
    6. Knowing Your Place
      1. Upstream and Downstream Flows
      2. The Maintainer and Developer Roles
      3. Maintainer–Developer Interaction
      4. Role Duality
    7. Working with Multiple Repositories
      1. Your Own Workspace
      2. Where to Start Your Repository
      3. Converting to a Different Upstream Repository
      4. Using Multiple Upstream Repositories
      5. Forking Projects
        1. Isn’t forking a project bad?
        2. Reconciling forks
        3. Forking projects at GitHub
  15. 14. Patches
    1. Why Use Patches?
    2. Generating Patches
      1. Patches and Topological Sorts
    3. Mailing Patches
    4. Applying Patches
    5. Bad Patches
    6. Patching Versus Merging
  16. 15. Hooks
    1. Installing Hooks
      1. Example Hooks
      2. Creating Your First Hook
    2. Available Hooks
      1. Commit-Related Hooks
      2. Patch-Related Hooks
      3. Push-Related Hooks
      4. Other Local Repository Hooks
  17. 16. Combining Projects
    1. The Old Solution: Partial Checkouts
    2. The Obvious Solution: Import the Code into Your Project
      1. Importing Subprojects by Copying
      2. Importing Subprojects with git pull -s subtree
      3. Submitting Your Changes Upstream
    3. The Automated Solution: Checking out Subprojects Using Custom Scripts
    4. The Native Solution: gitlinks and git submodule
      1. Gitlinks
      2. The git submodule Command
  18. 17. Submodule Best Practices
    1. Submodule Commands
    2. Why Submodules?
    3. Submodules Preparation
    4. Why Read Only?
    5. Why Not Read Only?
    6. Examining the Hashes of Submodule Commits
    7. Credential Reuse
    8. Use Cases
    9. Multilevel Nesting of Repos
    10. Submodules on the Horizon
  19. 18. Using Git with Subversion Repositories
    1. Example: A Shallow Clone of a Single Branch
      1. Making Your Changes in Git
      2. Fetching Before Committing
      3. Committing Through git svn rebase
    2. Pushing, Pulling, Branching, and Merging with git svn
      1. Keeping Your Commit IDs Straight
      2. Cloning All the Branches
      3. Sharing Your Repository
      4. Merging Back into Subversion
        1. How dcommit handles merges
    3. Miscellaneous Notes on Working with Subversion
      1. svn:ignore Versus .gitignore
      2. Reconstructing the git-svn Cache
  20. 19. Advanced Manipulations
    1. Using git filter-branch
      1. Examples Using git filter-branch
        1. Using git filter-branch to expunge a file
        2. Using filter-branch to edit a commit message
      2. filter-branch Pitfalls
    2. How I Learned to Love git rev-list
      1. Date-Based Checkout
        1. Date-based checkout cautions
      2. Retrieve Old Version of a File
    3. Interactive Hunk Staging
    4. Recovering a Lost Commit
      1. The git fsck Command
      2. Reconnecting a Lost Commit
  21. 20. Tips, Tricks, and Techniques
    1. Interactive Rebase with a Dirty Working Directory
    2. Remove Left-Over Editor Files
    3. Garbage Collection
    4. Split a Repository
    5. Tips for Recovering Commits
    6. Subversion Conversion Tips
      1. General Advice
      2. Remove a Trunk After an SVN Import
      3. Removing SVN Commit IDs
    7. Manipulating Branches from Two Repositories
    8. Recovering from an Upstream Rebase
    9. Make Your Own Git Command
    10. Quick Overview of Changes
    11. Cleaning Up
    12. Using git-grep to Search a Repository
    13. Updating and Deleting refs
    14. Following Files that Moved
    15. Keep, But Don’t Track, This File
    16. Have You Been Here Before?
  22. 21. Git and GitHub
    1. Repo for Public Code
    2. Creating a GitHub Repository
    3. Social Coding on Open Source
    4. Watchers
    5. News Feed
    6. Forks
    7. Creating Pull Requests
    8. Managing Pull Requests
    9. Notifications
    10. Finding Users, Projects, and Code
    11. Wikis
    12. GitHub Pages (Git for Websites)
    13. In-Page Code Editor
    14. Subversion Bridge
    15. Tags Automatically Becoming Archives
    16. Organizations
    17. REST API
    18. Social Coding on Closed Source
    19. Eventual Open Sourcing
    20. Coding Models
    21. GitHub Enterprise
    22. GitHub in Sum
  23. Index
  24. About the Authors
  25. Colophon
  26. Copyright

Product information

  • Title: Version Control with Git, 2nd Edition
  • Author(s): Jon Loeliger, Matthew McCullough
  • Release date: August 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449316389