You are previewing Version Control with Git, 2nd Edition.

Version Control with Git, 2nd Edition

Cover of Version Control with Git, 2nd Edition by Jon Loeliger... Published by O'Reilly Media, Inc.
  1. Version Control with Git
  2. 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
  3. 1. Introduction
    1. Background
    2. The Birth of Git
    3. Precedents
    4. Timeline
    5. What’s in a Name?
  4. 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)
  5. 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
  6. 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
  7. 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
  8. 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
      3. Commit Ranges
    4. Finding Commits
      1. Using git bisect
      2. Using git blame
      3. Using Pickaxe
  9. 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
  10. 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
  11. 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
      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
      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?
  12. 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
  13. 11. The Stash and the Reflog
    1. The Stash
    2. The Reflog
  14. 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
    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
  15. 13. Repository Management
    1. A Word About Servers
    2. Publishing Repositories
      1. Repositories with Controlled Access
      2. Repositories with Anonymous Read Access
      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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
    3. Miscellaneous Notes on Working with Subversion
      1. svn:ignore Versus .gitignore
      2. Reconstructing the git-svn Cache
  21. 19. Advanced Manipulations
    1. Using git filter-branch
      1. Examples Using git filter-branch
      2. filter-branch Pitfalls
    2. How I Learned to Love git rev-list
      1. Date-Based Checkout
      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
  22. 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?
  23. 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
  24. Index
  25. About the Authors
  26. Colophon
  27. Copyright
O'Reilly logo

Chapter 20. Tips, Tricks, and Techniques

With a plethora of commands and options, Git provides a rich resource for performing varied and powerful changes to a repository. Sometimes, though, the actual means for accomplishing some particular task are a bit elusive. Sometimes, the purpose of a particular command and option isn’t really clear or becomes lost in a technical description.

This chapter provides a collection of various tips, tricks, and techniques that highlight Git’s ability to do interesting transformations.

Interactive Rebase with a Dirty Working Directory

Frequently, when developing a multicommit change sequence on a local branch, I realize that I need to make an additional modification to some commit I’ve already made earlier in the sequence. Rather than scribbling a note about it on the side and coming back to it later, I will immediately edit and introduce that change directly into a new commit with a reminder note in the commit log entry that it should be squashed into a previous commit.

When I eventually get around to cleaning up my commit sequence, and want to use git rebase -i, I am often midstride and find myself with a dirty working directory. In this case, Git will refuse to do the rebase.

    $ git show-branch --more=10
    [master] Tinker bar
    [master^] Squash into 'More foo and bar'
    [master~2] Modify bar
    [master~3] More foo and bar
    [master~4] Initial foo and bar.

    $ git rebase -i master~4
    Cannot rebase: You have unstaged changes.
    Please commit or stash them.

As suggested, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.