Mastering Git

Book description

Attain expert-level proficiency with Git for enhanced productivity and efficient collaboration by mastering advanced distributed version control features

About This Book

  • Set up Git for solo and collaborative development
  • Harness the full power of Git version control system to customize Git behavior, manipulate history, integrate external tools and explore platform shortcuts
  • A detailed guide, which explains how to apply advanced Git techniques and workflows and ways to handle submodules

Who This Book Is For

If you are a Git user with reasonable knowledge of Git and familiarity with basic concepts such as branching, merging, staging, and workflows, this is the book for you. Basic knowledge of installing Git and software configuration management concepts is essential.

What You Will Learn

  • Explore project history, find revisions using different criteria, and filter and format how history looks
  • Manage your working directory and staging area for commits and interactively create new revisions and amend them
  • Set up repositories and branches for collaboration
  • Submit your own contributions and integrate contributions from other developers via merging or rebasing
  • Customize Git behavior system-wide, on a per-user, per-repository, and per-file basis
  • Take up the administration and set up of Git repositories, configure access, find and recover from repository errors, and perform repository maintenance
  • Chose a workflow and configure and set up support for the chosen workflow

In Detail

Git is one of the most popular types of Source Code Management (SCM) and Distributed Version Control System (DVCS). Despite the powerful and versatile nature of the tool enveloping strong support for nonlinear development and the ability to handle large projects efficiently, it is a complex tool and often regarded as ?user-unfriendly?. Getting to know the ideas and concepts behind the architecture of Git will help you make full use of its power and understand its behavior. Learning the best practices and recommended workflows should help you to avoid problems and ensure trouble-free development.

The book scope is meticulously designed to help you gain deeper insights into Git's architecture, its underlying concepts, behavior, and best practices. Mastering Git starts with a quick implementation example of using Git for a collaborative development of a sample project to establish the foundation knowledge of Git operational tasks and concepts. Furthermore, as you progress through the book, the tutorials provide detailed descriptions of various areas of usage: from archaeology, through managing your own work, to working with other developers. This book also helps augment your understanding to examine and explore project history, create and manage your contributions, set up repositories and branches for collaboration in centralized and distributed version control, integrate work from other developers, customize and extend Git, and recover from repository errors. By exploring advanced Git practices, you will attain a deeper understanding of Git's behavior, allowing you to customize and extend existing recipes and write your own.

Style and approach

Step-by-step instructions and useful information make this book the ultimate guide to understanding and mastering Git. This book will show road to mastery example by example, while explaining mental model of Git. The Introduction section covers the 'Essentials' just for refreshing the basics. The main highlight is that the concepts are based on HOW the technology/framework works and not just practical 'WHAT to do'.

Table of contents

  1. Mastering Git
    1. Table of Contents
    2. Mastering Git
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Git Basics in Practice
      1. An introduction to version control and Git
      2. Git by example
        1. Repository setup
        2. Creating a Git repository
        3. Cloning the repository and creating the first commit
        4. Publishing changes
        5. Examining history and viewing changes
        6. Renaming and moving files
        7. Updating your repository (with merge)
        8. Creating a tag
        9. Resolving a merge conflict
        10. Adding files in bulk and removing files
        11. Undoing changes to a file
        12. Creating a new branch
        13. Merging a branch (no conflicts)
        14. Undoing an unpublished merge
      3. Summary
    9. 2. Exploring Project History
      1. Directed Acyclic Graphs
        1. Whole-tree commits
        2. Branches and tags
        3. Branch points
        4. Merge commits
      2. Single revision selection
        1. HEAD – the implicit revision
        2. Branch and tag references
        3. SHA-1 and the shortened SHA-1 identifier
        4. Ancestry references
        5. Reverse ancestry references: the git describe output
        6. Reflog shortnames
        7. Upstream of remote-tracking branches
        8. Selecting revision by the commit message
      3. Selecting the revision range
        1. Single revision as a revision range
        2. Double dot notation
        3. Multiple points – including and excluding revisions
        4. The revision range for a single revision
        5. Triple-dot notation
      4. Searching history
        1. Limiting the number of revisions
        2. Matching revision metadata
          1. Time-limiting options
          2. Matching commit contents
          3. Commit parents
        3. Searching changes in revisions
        4. Selecting types of change
      5. History of a file
        1. Path limiting
        2. History simplification
        3. Blame – the line-wise history of a file
      6. Finding bugs with git bisect
      7. Selecting and formatting the git log output
        1. Predefined and user defined output formats
        2. Including, formatting, and summing up changes
        3. Summarizing contributions
        4. Viewing a revision and a file at revision
      8. Summary
    10. 3. Developing with Git
      1. Creating a new commit
        1. The DAG view of creating a new commit
        2. The index – a staging area for commits
        3. Examining the changes to be committed
          1. The status of the working directory
          2. Examining differences from the last revision
          3. Unified Git diff format
        4. Selective commit
          1. Selecting files to commit
          2. Interactively selecting changes
          3. Creating a commit step by step
        5. Amending a commit
      2. Working with branches
        1. Creating a new branch
        2. Creating orphan branches
        3. Selecting and switching to a branch
          1. Obstacles to switching to a branch
          2. Anonymous branches
          3. Git checkout DWIM-mery
        4. Listing branches
        5. Rewinding or resetting a branch
        6. Deleting a branch
        7. Changing the branch name
      3. Summary
    11. 4. Managing Your Worktree
      1. Ignoring files
        1. Marking files as intentionally untracked
        2. Which types of file should be ignored?
        3. Listing ignored files
        4. Ignoring changes in tracked files
      2. File attributes
        1. Identifying binary files and end-of-line conversions
        2. Diff and merge configuration
          1. Generating diffs and binary files
          2. Configuring diff output
          3. Performing a 3-way merge
        3. Transforming files (content filtering)
          1. Obligatory file transformations
        4. Keyword expansion and substitution
        5. Other built-in attributes
        6. Defining attribute macros
      3. Fixing mistakes with the reset command
        1. Rewinding the branch head, softly
          1. Removing or amending a commit
          2. Squashing commits with reset
        2. Resetting the branch head and the index
          1. Splitting a commit with reset
          2. Saving and restoring state with the WIP commit
        3. Discarding changes and rewinding branch
          1. Moving commits to a feature branch
          2. Undoing a merge or a pull
        4. Safer reset – keeping your changes
          1. Rebase changes to an earlier revision
      4. Stashing away your changes
        1. Using git stash
        2. Stash and the staging area
        3. Stash internals
          1. Un-applying a stash
          2. Recovering stashes that were dropped erroneously
      5. Managing worktrees and the staging area
        1. Examining files and directories
        2. Searching file contents
        3. Un-tracking, un-staging, and un-modifying files
        4. Resetting a file to the old version
        5. Cleaning the working area
      6. Multiple working directories
      7. Summary
    12. 5. Collaborative Development with Git
      1. Collaborative workflows
        1. Bare repositories
        2. Interacting with other repositories
        3. The centralized workflow
        4. The peer-to-peer or forking workflow
        5. The maintainer or integration manager workflow
        6. The hierarchical or dictator and lieutenants workflows
      2. Managing remote repositories
        1. The origin remote
        2. Listing and examining remotes
        3. Adding a new remote
        4. Updating information about remotes
          1. Renaming remotes
          2. Changing the remote URLs
          3. Changing the list of branches tracked by remote
          4. Setting the default branch of remote
          5. Deleting remote-tracking branches
        5. Support for triangular workflows
      3. Transport protocols
        1. Local transport
        2. Smart transports
          1. Native Git protocol
          2. SSH protocol
          3. Smart HTTP(S) protocol
        3. Offline transport with bundles
          1. Cloning and updating with bundle
          2. Using bundle to update an existing repository
          3. Utilizing bundle to help with the initial clone
        4. Remote transport helpers
          1. Transport relay with remote helpers
          2. Using foreign SCM repositories as remotes
        5. Credentials/password management
          1. Asking for passwords
          2. Public key authentication for SSH
          3. Credential helpers
      4. Publishing your changes upstream
        1. Pushing to a public repository
        2. Generating a pull request
        3. Exchanging patches
      5. Chain of trust
        1. Content-addressed storage
        2. Lightweight, annotated, and signed tags
          1. Lightweight tags
          2. Annotated tags
          3. Signed tags
          4. Publishing tags
          5. Tag verification
        3. Signed commits
        4. Merging signed tags (merge tags)
      6. Summary
    13. 6. Advanced Branching Techniques
      1. Types and purposes of branches
        1. Long-running, perpetual branches
          1. Integration, graduation, or progressive-stability branches
          2. Per-release branches and per-release maintenance
          3. Hotfix branches for security fixes
          4. Per-customer or per-deployment branches
          5. Automation branches
          6. Mob branches for anonymous push access
          7. The orphan branch trick
        2. Short-lived branches
          1. Topic or feature branches
          2. Bugfix branches
          3. Detached HEAD – the anonymous branch
      2. Branching workflows and release engineering
        1. The release and trunk branches workflow
        2. The graduation, or progressive-stability branches workflow
        3. The topic branches workflow
          1. Graduation branches in a topic branch workflow
          2. Branch management for a release in a topic branch workflow
        4. Git-flow – a successful Git branching model
        5. Fixing a security issue
      3. Interacting with branches in remote repositories
        1. Upstream and downstream
        2. Remote-tracking branches and refspec
          1. Remote-tracking branches
          2. Refspec – remote to local branch mapping specification
        3. Fetching and pulling versus pushing
          1. Pull – fetch and update current branch
          2. Pushing to the current branch in a nonbare remote repository
          3. The default fetch refspec and push modes
        4. Fetching and pushing branches and tags
          1. Fetching branches
          2. Fetching tags and automatic tag following
          3. Pushing branches and tags
        5. Push modes and their use
          1. The simple push mode – the default
          2. The matching push mode for maintainers
          3. The upstream push mode for the centralized workflow
          4. The current push mode for the blessed repository workflow
      4. Summary
    14. 7. Merging Changes Together
      1. Methods of combining changes
        1. Merging branches
          1. No divergence – fast-forward and up-to-date cases
          2. Creating a merge commit
          3. Merge strategies and their options
          4. Reminder – merge drivers
          5. Reminder – signing merges and merging tags
        2. Copying and applying a changeset
          1. Cherry-pick – creating a copy of a changeset
          2. Revert – undoing an effect of a commit
          3. Applying a series of commits from patches
          4. Cherry-picking and reverting a merge
        3. Rebasing a branch
          1. Merge versus rebase
          2. Types of rebase
          3. Advanced rebasing techniques
      2. Resolving merge conflicts
        1. The three-way merge
        2. Examining failed merges
          1. Conflict markers in the worktree
          2. Three stages in the index
          3. Examining differences – the combined diff format
          4. How do we get there: git log --merge
        3. Avoiding merge conflicts
          1. Useful merge options
          2. Rerere – reuse recorded resolutions
        4. Dealing with merge conflicts
          1. Aborting a merge
          2. Selecting ours or theirs version
          3. Scriptable fixes – manual file remerging
          4. Using graphical merge tools
          5. Marking files as resolved and finalizing merges
          6. Resolving rebase conflicts
          7. git-imerge – incremental merge and rebase for git
      3. Summary
    15. 8. Keeping History Clean
      1. An introduction to Git internals
        1. Git objects
        2. The plumbing and porcelain Git commands
        3. Environment variables used by Git
          1. Environment variables affecting global behavior
          2. Environment variables affecting repository locations
          3. Environment variables affecting committing
      2. Rewriting history
        1. Amending the last commit
        2. An interactive rebase
          1. Reordering, removing, and fixing commits
          2. Squashing commits
          3. Splitting commits
          4. Testing each rebased commit
        3. External tools – patch management interfaces
        4. Scripted rewrite with the git filter-branch
          1. Running the filter-branch without filters
          2. Available filter types for filter-branch and their use
          3. Examples of using the git filter-branch
        5. External tools for large-scale history rewriting
          1. Removing files from the history with BFG Repo Cleaner
          2. Editing the repository history with reposurgeon
        6. The perils of rewriting published history
          1. The consequences of upstream rewrite
          2. Recovering from an upstream history rewrite
      3. Amending history without rewriting
        1. Reverting a commit
          1. Reverting a faulty merge
          2. Recovering from reverted merges
        2. Storing additional information with notes
          1. Adding notes to a commit
          2. How notes are stored
          3. Other categories and uses of notes
          4. Rewriting history and notes
          5. Publishing and retrieving notes
        3. Using the replacements mechanism
          1. The replacements mechanism
          2. Example – joining histories with git replace
          3. Historical note – grafts
          4. Publishing and retrieving replacements
      4. Summary
    16. 9. Managing Subprojects – Building a Living Framework
      1. Managing library and framework dependencies
        1. Managing dependencies outside Git
        2. Manually importing the code into your project
        3. A Git subtree for embedding the subproject code
          1. Creating a remote for a subproject
          2. Adding a subproject as a subtree
          3. Cloning and updating superprojects with subtrees
          4. Getting updates from subprojects with a subtree merge
          5. Showing changes between a subtree and its upstream
          6. Sending changes to the upstream of a subtree
        4. The Git submodules solution: repository inside repository
          1. Gitlinks, .git files, and the git submodule command
          2. Adding a subproject as a submodule
          3. Cloning superprojects with submodules
          4. Updating submodules after superproject changes
          5. Examining changes in a submodule
          6. Getting updates from the upstream of the submodule
          7. Sending submodule changes upstream
        5. Transforming a subfolder into a subtree or submodule
        6. Subtrees versus submodules
          1. Use cases for subtrees
          2. Use cases for submodules
          3. Third-party subproject management solutions
      2. Managing large Git repositories
        1. Handling repositories with a very long history
          1. Using shallow clones to get truncated history
          2. Cloning only a single branch
        2. Handling repositories with large binary files
          1. Splitting the binary asset folder into a separate submodule
          2. Storing large binary files outside the repository
      3. Summary
    17. 10. Customizing and Extending Git
      1. Git on the command line
        1. Git-aware command prompt
        2. Command-line completion for Git
        3. Autocorrection for Git commands
        4. Making the command line prettier
        5. Alternative command line
      2. Graphical interfaces
        1. Types of graphical tools
        2. Graphical diff and merge tools
        3. Graphical interface examples
      3. Configuring Git
        1. Command-line options and environment variables
        2. Git configuration files
          1. The syntax of Git configuration files
          2. Accessing the Git configuration
          3. Basic client-side configuration
          4. The rebase and merge setup, configuring pull
          5. Preserving undo information – the expiry of objects
          6. Formatting and whitespace
          7. Server-side configuration
        3. Per-file configuration with gitattributes
      4. Automating Git with hooks
        1. Installing a Git hook
        2. A template for repositories
        3. Client-side hooks
          1. Commit process hooks
          2. Hooks for applying patches from e-mails
          3. Other client-side hooks
        4. Server-side hooks
      5. Extending Git
        1. Command aliases for Git
        2. Adding new Git commands
        3. Credential helpers and remote helpers
      6. Summary
    18. 11. Git Administration
      1. Repository maintenance
      2. Data recovery and troubleshooting
        1. Recovering a lost commit
        2. Troubleshooting Git
      3. Git on the server
        1. Server-side hooks
          1. The pre-receive hook
          2. Push-to-update hook for pushing to nonbare repositories
          3. The update hook
          4. The post-receive hook
          5. The post-update hook (legacy mechanism)
        2. Using hooks to implement the Git-enforced policy
          1. Enforcing the policy with server-side hooks
          2. Early notices about policy violations with client-side hooks
        3. Signed pushes
        4. Serving Git repositories
          1. Local protocol
          2. SSH protocol
          3. Anonymous Git protocol
          4. Smart HTTP(S) protocol
          5. Dumb protocols
          6. Remote helpers
        5. Tools to manage Git repositories
        6. Tips and tricks for hosting repositories
          1. Reducing the size taken by repositories
          2. Speeding up smart protocols with pack bitmaps
          3. Solving the large nonresumable initial clone problem
      4. Augmenting development workflows
      5. Summary
    19. 12. Git Best Practices
      1. Starting a project
        1. Dividing work into repositories
        2. Selecting the collaboration workflow
        3. Choosing which files to keep under version control
      2. Working on a project
        1. Working on a topic branch
        2. Deciding what to base your work on
        3. Splitting changes into logically separate steps
        4. Writing a good commit message
        5. Preparing changes for submission
      3. Integrating changes
        1. Submitting and describing changes
        2. The art of the change review
        3. Responding to reviews and comments
      4. Other recommendations
        1. Don't panic, recovery is almost always possible
        2. Don't change the published history
        3. Numbering and tagging releases
        4. Automate what is possible
      5. Summary
    20. Index

Product information

  • Title: Mastering Git
  • Author(s): Jakub Narębski
  • Release date: April 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781783553754