You are previewing Git Pocket Guide.

Git Pocket Guide

Cover of Git Pocket Guide by Richard E. Silverman Published by O'Reilly Media, Inc.
  1. Special Upgrade Offer
  2. Preface
    1. What Is Git?
    2. Goals of This Book
    3. Conventions Used in This Book
      1. Unix
      2. Shell
      3. Command Syntax
      4. Typography
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  3. 1. Understanding Git
    1. Overview
      1. Terminology
      2. Branches
      3. Sharing Work
    2. The Object Store
      1. Blob
      2. Tree
      3. Commit
      4. Tag
    3. Object IDs and SHA-1
      1. Security
    4. Where Objects Live
    5. The Commit Graph
    6. Refs
      1. Related Commands
    7. Branches
    8. The Index
    9. Merging
      1. Merging Content
      2. Merging History
    10. Push and Pull
      1. Notes
  4. 2. Getting Started
    1. Basic Configuration
      1. Personal Identification
      2. Text Editor
      3. Commit ID Abbreviation
      4. Pagination
      5. Color
      6. Cryptographic Keys
      7. Command Aliases
      8. Getting Help
      9. References
    2. Creating a New, Empty Repository
      1. Selected Options
    3. Importing an Existing Project
    4. Ignoring Files
      1. Syntax of “Ignore Patterns”
  5. 3. Making Commits
    1. Changing the Index
      1. Adding a New File
      2. Adding the Changes to an Existing File
      3. Adding Partial Changes
      4. Shortcuts
      5. Removing a File
      6. Renaming a File
      7. Unstaging Changes
    2. Making a Commit
      1. Commit Messages
      2. What Makes a Good Commit?
      3. Shortcuts
      4. Empty Directories
      5. A Commit Workflow
  6. 4. Undoing and Editing Commits
    1. Changing the Last Commit
      1. Double Oops!
    2. Discarding the Last Commit
      1. Discarding Any Number of Commits
    3. Undoing a Commit
      1. Partial Undo
    4. Editing a Series of Commits
      1. Conflicts
      2. The exec Action
  7. 5. Branching
    1. The Default Branch, master
    2. Making a New Branch
    3. Switching Branches
      1. Uncommitted Changes
      2. Untracked Files
      3. Losing Your Head
    4. Deleting a Branch
    5. Renaming a Branch
  8. 6. Tracking Other Repositories
    1. Cloning a Repository
      1. Clones and Hard Links
      2. Bare Repositories
      3. Reference Repositories
    2. Local, Remote, and Tracking Branches
    3. Synchronization: Push and Pull
      1. Pulling
      2. Pushing
      3. Push Defaults
      4. Pull with Rebase
      5. Notes
    4. Access Control
  9. 7. Merging
    1. Merge Conflicts
      1. Resolving Merge Conflicts
      2. Notes
    2. Details on Merging
    3. Merge Tools
      1. Notes
    4. Custom Merge Tools
    5. Merge Strategies
    6. Why the Octopus?
    7. Reusing Previous Merge Decisions
  10. 8. Naming Commits
    1. Naming Individual Commits
      1. Commit ID
      2. Ref Name
      3. Names Relative to a Given Commit
      4. Names Relative to the Reflog
      5. The Upstream Branch
      6. Matching a Commit Message
      7. Following Chains
      8. Addressing Pathnames
    2. Naming Sets of Commits
  11. 9. Viewing History
    1. Command Format
    2. Output Formats
    3. Defining Your Own Formats
      1. Notes
    4. Limiting Commits to Be Shown
    5. Regular Expressions
    6. Reflog
    7. Decoration
    8. Date Style
    9. Listing Changed Files
    10. Showing and Following Renames or Copies
      1. Detecting Copies
    11. Rewriting Names and Addresses: The “mailmap”
      1. Shortening Names
    12. Searching for Changes: The “pickaxe”
    13. Showing Diffs
      1. Color
      2. Word Diff
    14. Comparing Branches
      1. Displaying Sides
    15. Showing Notes
    16. Commit Ordering
    17. History Simplification
    18. Related Commands
      1. git cherry
      2. git shortlog
  12. 10. Editing History
    1. Rebasing
      1. Undoing a Rebase
    2. Importing from One Repository to Another
      1. Importing Disconnected History
      2. Importing Linear History
      3. Importing Nonlinear History
    3. Commit Surgery: git replace
      1. Keeping It Real
    4. The Big Hammer: git filter-branch
      1. Examples
    5. Notes
  13. 11. Understanding Patches
    1. Applying Plain Diffs
    2. Patches with Commit Information
  14. 12. Remote Access
    1. SSH
    2. HTTP
    3. Storing Your Username
    4. Storing Your Password
    5. References
  15. 13. Miscellaneous
    1. git cherry-pick
    2. git notes
      1. git notes Subcommands
    3. git grep
      1. Combining Regular Expressions
      2. What to Search
      3. What to Show
      4. How to Match
    4. git rev-parse
    5. git clean
    6. git stash
      1. Subcommands
    7. git show
    8. git tag
      1. Deleting a Tag from a Remote
      2. Following Tags
      3. Backdating Tags
    9. git diff
      1. git diff
      2. git diff --staged
      3. git diff <commit>
      4. git diff <A> <B>
      5. Options and Arguments
    10. git instaweb
    11. Git Hooks
    12. Visual Tools
    13. Submodules
  16. 14. How Do I…?
    1. …Make and Use a Central Repository?
    2. …Fix the Last Commit I Made?
    3. …Edit the Previous n Commits?
    4. …Undo My Last n Commits?
    5. …Reuse the Message from an Existing Commit?
    6. …Reapply an Existing Commit from Another Branch?
    7. …List Files with Conflicts when Merging?
    8. …Get a Summary of My Branches?
    9. …Get a Summary of My Working Tree and Index State?
    10. …Stage All the Current Changes to My Working Files?
    11. …Show the Changes to My Working Files?
    12. …Save and Restore My Working Tree and Index Changes?
    13. …Add a Downstream Branch Without Checking It Out?
    14. …List the Files in a Specific Commit?
    15. …Show the Changes Made by a Commit?
    16. …Get Tab Completion of Branch Names, Tags, and So On?
    17. …List All Remotes?
    18. …Change the URL for a Remote?
    19. …Remove Old Remote-Tracking Branches?
    20. …Have git log:
      1. Find Commits I Made but Lost?
      2. Not Show the diffs for Root Commits?
      3. Show the Changes for Each Commit?
      4. Show the Committer as well as the Author?
  17. Index
  18. About the Author
  19. Special Upgrade Offer
  20. Copyright

Chapter 14. How Do I…?

This final chapter presents some commands and recipes for accomplishing a grab bag of specific tasks. Some were presented earlier and are repeated or referred to here for easy reference, and some are new. Remember that you don’t usually want to edit history for commits you’ve already published with git push. Examples that refer to a remote repository use the most common case, origin. rev is any revision name as described in Chapter 8.

…Make and Use a Central Repository?

Suppose you have an account named ares on a server, which you want to use to coordinate your own work on a project foo (perhaps among repositories at home, work, and on your laptop). First, log into the server and create a “bare” repository (which you will not use directly):

$ ssh
ares> git init --bare foo
Initialized empty Git repository in /u/ares/foo/.git
$ logout

If this is for a project with existing content, connect that repository to the new remote as its origin (assuming here a single, local master branch):

$ cd foo
$ git remote add origin
$ git push -u origin master
* [new branch]        master -> master
Branch master set up to track remote branch master
from foo.

You can just use plain git push from then on. To clone this repository elsewhere:

$ git clone

…Fix the Last Commit I Made?

Make your corrections and stage them with git add, then:

$ git commit --amend

Add -a to automatically stage all changes to tracked files (skipping git add). Add -C HEAD to reuse the previous commit message without stopping to edit it.

See Changing the Last Commit.

…Edit the Previous n Commits?

$ git rebase -i HEAD~n

The history involved should be linear. You can add -p to preserve merge commits, but this can get tricky depending on the changes you want to make.

See Editing a Series of Commits.

…Undo My Last n Commits?

$ git reset HEAD~n

This removes the last n commits of a linear history from the current branch, leaving the corresponding changes in your working files. You can add --hard to make the working tree reflect the new branch tip, but beware: this will also discard any current uncommitted changes, which you will lose with no recourse. See Discarding Any Number of Commits. This will also work if there is a merge commit in the range, effectively undoing the merge for this branch; see Chapter 8 to understand how to interpret HEAD~n in this case.

…Reuse the Message from an Existing Commit?

$ git commit --reset-author -C rev

Add --edit to edit the message before committing.

…Reapply an Existing Commit from Another Branch?

$ git cherry-pick rev

If the commit is in a different local repository, ~/other:

$ git --git-dir ~/other/.git format-patch -1 --stdout rev | git am


…List Files with Conflicts when Merging?

git status shows these as part of its report, but to just list their names:

$ git diff --name-only --diff-filter=U

…Get a Summary of My Branches?

  • List local branches: git branch
  • List all branches: git branch -a
  • Get a compact summary of local branches and status with respect to their upstream counterparts: git branch -vv
  • Get detail about the remote as well: git remote show origin (or other named remote)

See Notes.

…Get a Summary of My Working Tree and Index State?

$ git status

Add -sb for a more compact listing; see the “Short Format” section of git-status(1) on how to interpret this.

…Stage All the Current Changes to My Working Files?

$ git add -A

This does git add for every changed, new, and deleted file in your working tree. Add --force to include normally ignored files; you might do this when adding a new release to a “vendor branch,” which tracks updates to other projects you obtain by means other than Git (e.g., tarballs).

…Show the Changes to My Working Files?

git diff shows unstaged changes; add --stage to see staged changes instead. Add --name-only or --name-status for a more compact listing.

…Save and Restore My Working Tree and Index Changes?

git stash saves and sets your outstanding changes aside, so you can perform other operations that might be blocked by them, such as checking out a different branch. You can restore your changes later with git stash pop. See git stash.

…Add a Downstream Branch Without Checking It Out?

$ git branch foo origin/foo

This adds a local branch and sets up push/pull tracking as if you had done git checkout foo, but does not do the checkout or change your current branch.

…List the Files in a Specific Commit?

$ git ls-tree -r --name-only rev

This listing is restricted to the current directory; add --full-tree for a complete list.

…Show the Changes Made by a Commit?

git show rev is easier that git diff rev~ rev, and shows the author, timestamp, commit ID, and message as well. Add -s to suppress the diff and just see the latter information; use --name-status or --stat to summarize the changes. It also works for merge commits, showing conflicts from the merge as with git log --cc (see Showing Diffs). The default for rev is HEAD.

…Get Tab Completion of Branch Names, Tags, and So On?

Git comes with a completion package for bash and zsh, installed in its git-core directory as git-completion.bash. You can use it by including (or “sourcing”) this file in your shell startup file (e.g., in ~/.bashrc):

# define completion for Git
[ -r $gitcomp ] && source $gitcomp

Pressing Tab in the middle of a Git command will then show possible completions for the given context. For example, if you type git checkout, space, and then press Tab, the shell will print the branches and tag names you could use here. If you type an initial part of one of these names, pressing Tab again will complete it for you. The exact behavior of completion is very customizable; see your shell manpage for details.

There is also a, which will make your shell prompt reflect the current branch status when your working directory is a Git repository.

…List All Remotes?

git remote does this; add -v to see the corresponding URLs configured for push and pull (ordinarily the same):

$ git remote -v
origin (fetch)
origin (push)

…Change the URL for a Remote?

$ git remote set-url remote URL

…Remove Old Remote-Tracking Branches?

$ git remote prune origin

This removes tracking for remote branches that have been deleted upstream.

…Have git log:

Find Commits I Made but Lost?

…perhaps after editing history with git rebase -i or git reset, or deleting a branch:

$ git log -g

See Double Oops!.

Not Show the diffs for Root Commits?

A root commit always shows the addition of all the files in its tree, which can be a large and uninformative list; you can suppress this with:

$ git config [--global] log.showroot false

Show the Changes for Each Commit?

git log -p shows the complete patch for each commit it lists, while these options summarize the changes in different ways:

$ git log --name-status
$ git log --stat

See Listing Changed Files.

Show the Committer as well as the Author?

$ git log --format=fuller

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