Cover; Copyright; Credits; About the Author; About the Reviewer; www.PacktPub.com; Table of Contents; Preface; Chapter 1: Git Basics in Practice; An introduction to version control and Git; Git by example; Repository setup; Creating a Git repository; Cloning the repository and creating the first commit; Publishing changes; Examining history and viewing changes; Renaming and moving files; Updating your repository (with merge); Creating a tag; Resolving a merge conflict; Adding files in bulk and removing files; Undoing changes to a file; Creating a new branch; Merging a branch (no conflicts).;Attain expert-level proficiency with Git for enhanced productivity and efficient collaboration by mastering advanced distributed version control featuresAbout 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 submodulesWho This Book Is ForIf 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 workflowIn DetailGit 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 approachStep-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'. Cover......Page 1 Copyright......Page 3 Credits......Page 4 About the Author......Page 5 About the Reviewer......Page 6 www.PacktPub.com......Page 7 Table of Contents......Page 8 Preface......Page 18 An introduction to version control and Git......Page 24 Repository setup......Page 25 Creating a Git repository......Page 26 Cloning the repository and creating the first commit......Page 27 Examining history and viewing changes......Page 30 Renaming and moving files......Page 33 Updating your repository (with merge)......Page 34 Creating a tag......Page 35 Resolving a merge conflict......Page 37 Adding files in bulk and removing files......Page 40 Undoing changes to a file......Page 41 Creating a new branch......Page 42 Merging a branch (no conflicts)......Page 43 Undoing an unpublished merge......Page 44 Summary......Page 45 Chapter 2: Exploring Project History......Page 46 Directed Acyclic Graphs......Page 47 Branches and tags......Page 49 Branch points......Page 51 Single revision selection......Page 52 Branch and tag references......Page 53 SHA-1 and the shortened SHA-1 identifier......Page 54 Ancestry references......Page 56 Reflog shortnames......Page 57 Upstream of remote-tracking branches......Page 58 Single revision as a revision range......Page 59 Double dot notation......Page 60 Multiple points – including and excluding revisions......Page 61 Triple-dot notation......Page 62 Limiting the number of revisions......Page 64 Time-limiting options......Page 65 Commit parents......Page 66 Searching changes in revisions......Page 67 Path limiting......Page 69 Blame – the line-wise history of a file......Page 71 Finding bugs with git bisect......Page 73 Predefined and user defined output formats......Page 76 Including, formatting, and summing up changes......Page 78 Summarizing contributions......Page 79 Viewing a revision and a file at revision......Page 81 Summary......Page 82 Chapter 3: Developing with Git......Page 84 The DAG view of creating a new commit......Page 85 The index – a staging area for commits......Page 86 The status of the working directory......Page 88 Examining differences from the last revision......Page 91 Unified Git diff format......Page 92 Interactively selecting changes......Page 97 Creating a commit step by step......Page 99 Amending a commit......Page 100 Working with branches......Page 102 Creating orphan branches......Page 103 Obstacles to switching to a branch......Page 104 Anonymous branches......Page 105 Listing branches......Page 106 Rewinding or resetting a branch......Page 107 Deleting a branch......Page 109 Summary......Page 110 Chapter 4: Managing Your Worktree......Page 112 Ignoring files......Page 113 Marking files as intentionally untracked......Page 114 Which types of file should be ignored?......Page 116 Listing ignored files......Page 117 Ignoring changes in tracked files......Page 118 File attributes......Page 119 Identifying binary files and end-of-line conversions......Page 120 Generating diffs and binary files......Page 122 Configuring diff output......Page 124 Transforming files (content filtering)......Page 125 Obligatory file transformations......Page 127 Keyword expansion and substitution......Page 128 Other built-in attributes......Page 129 Fixing mistakes with the reset command......Page 130 Removing or amending a commit......Page 131 Resetting the branch head and the index......Page 132 Saving and restoring state with the WIP commit......Page 133 Moving commits to a feature branch......Page 134 Safer reset – keeping your changes......Page 135 Stashing away your changes......Page 136 Using git stash......Page 137 Stash and the staging area......Page 138 Stash internals......Page 139 Recovering stashes that were dropped erroneously......Page 140 Examining files and directories......Page 141 Searching file contents......Page 142 Un-tracking, un-staging, and un-modifying files......Page 143 Cleaning the working area......Page 145 Multiple working directories......Page 146 Summary......Page 147 Chapter 5: Collaborative Development with Git......Page 148 Bare repositories......Page 149 Interacting with other repositories......Page 150 The centralized workflow......Page 151 The peer-to-peer or forking workflow......Page 152 The maintainer or integration manager workflow......Page 153 The hierarchical or dictator and lieutenants workflows......Page 154 Managing remote repositories......Page 155 Listing and examining remotes......Page 156 Adding a new remote......Page 157 Changing the remote URLs......Page 158 Deleting remote-tracking branches......Page 159 Support for triangular workflows......Page 160 Local transport......Page 161 SSH protocol......Page 163 Smart HTTP(S) protocol......Page 164 Offline transport with bundles......Page 165 Cloning and updating with bundle......Page 166 Using bundle to update an existing repository......Page 168 Remote transport helpers......Page 170 Transport relay with remote helpers......Page 171 Using foreign SCM repositories as remotes......Page 172 Asking for passwords......Page 173 Public key authentication for SSH......Page 174 Credential helpers......Page 175 Pushing to a public repository......Page 176 Generating a pull request......Page 177 Exchanging patches......Page 178 Lightweight, annotated, and signed tags......Page 180 Signed tags......Page 181 Signed commits......Page 182 Merging signed tags (merge tags)......Page 183 Summary......Page 185 Chapter 6: Advanced Branching Techniques......Page 188 Types and purposes of branches......Page 189 Integration, graduation, or progressive-stability branches......Page 190 Per-release branches and per-release maintenance......Page 191 Automation branches......Page 192 The orphan branch trick......Page 193 Topic or feature branches......Page 194 Detached HEAD – the anonymous branch......Page 195 The release and trunk branches workflow......Page 196 The graduation, or progressive-stability branches workflow......Page 197 The topic branches workflow......Page 199 Graduation branches in a topic branch workflow......Page 200 Branch management for a release in a topic branch workflow......Page 202 Git-flow – a successful Git branching model......Page 204 Fixing a security issue......Page 205 Upstream and downstream......Page 206 Remote-tracking branches......Page 207 Refspec – remote to local branch mapping specification......Page 208 Pull – fetch and update current branch......Page 209 The default fetch refspec and push modes......Page 210 Fetching and pushing branches and tags......Page 211 Fetching tags and automatic tag following......Page 212 Push modes and their use......Page 213 The matching push mode for maintainers......Page 214 The upstream push mode for the centralized workflow......Page 215 The current push mode for the blessed repository workflow......Page 217 Summary......Page 218 Chapter 7: Merging Changes Together......Page 220 No divergence – fast-forward and up-to-date cases......Page 221 Creating a merge commit......Page 223 Merge strategies and their options......Page 225 Copying and applying a changeset......Page 226 Revert – undoing an effect of a commit......Page 227 Applying a series of commits from patches......Page 228 Rebasing a branch......Page 229 Merge versus rebase......Page 230 Advanced rebasing techniques......Page 231 The three-way merge......Page 233 Examining failed merges......Page 234 Conflict markers in the worktree......Page 235 Three stages in the index......Page 236 Examining differences – the combined diff format......Page 237 Useful merge options......Page 239 Rerere – reuse recorded resolutions......Page 240 Scriptable fixes – manual file remerging......Page 241 Marking files as resolved and finalizing merges......Page 242 git-imerge – incremental merge and rebase for git......Page 243 Summary......Page 244 Chapter 8: Keeping History Clean......Page 246 Git objects......Page 247 Environment variables used by Git......Page 250 Environment variables affecting global behavior......Page 251 Environment variables affecting repository locations......Page 252 Environment variables affecting committing......Page 253 Amending the last commit......Page 254 An interactive rebase......Page 255 Reordering, removing, and fixing commits......Page 256 Squashing commits......Page 259 Splitting commits......Page 260 Testing each rebased commit......Page 262 Scripted rewrite with the git filter-branch......Page 263 Running the filter-branch without filters......Page 264 Available filter types for filter-branch and their use......Page 265 Examples of using the git filter-branch......Page 267 Removing files from the history with BFG Repo Cleaner......Page 270 The perils of rewriting published history......Page 271 The consequences of upstream rewrite......Page 272 Recovering from an upstream history rewrite......Page 273 Reverting a commit......Page 275 Reverting a faulty merge......Page 276 Recovering from reverted merges......Page 279 Adding notes to a commit......Page 282 How notes are stored......Page 283 Other categories and uses of notes......Page 285 Rewriting history and notes......Page 288 Publishing and retrieving notes......Page 289 Using the replacements mechanism......Page 290 The replacements mechanism......Page 291 Example – joining histories with git replace......Page 292 Historical note – grafts......Page 293 Summary......Page 294 Chapter 9: Managing Subprojects – Building a Living Framework......Page 296 Managing library and framework dependencies......Page 297 Managing dependencies outside Git......Page 299 Manually importing the code into your project......Page 300 A Git subtree for embedding the subproject code......Page 301 Creating a remote for a subproject......Page 303 Adding a subproject as a subtree......Page 304 Getting updates from subprojects with a subtree merge......Page 306 Showing changes between a subtree and its upstream......Page 308 Sending changes to the upstream of a subtree......Page 309 The Git submodules solution: repository inside repository......Page 310 Gitlinks, .git files, and the git submodule command......Page 311 Adding a subproject as a submodule......Page 314 Cloning superprojects with submodules......Page 316 Updating submodules after superproject changes......Page 317 Examining changes in a submodule......Page 319 Getting updates from the upstream of the submodule......Page 321 Transforming a subfolder into a subtree or submodule......Page 322 Subtrees versus submodules......Page 324 Use cases for subtrees......Page 325 Use cases for submodules......Page 326 Managing large Git repositories......Page 327 Using shallow clones to get truncated history......Page 328 Handling repositories with large binary files......Page 329 Storing large binary files outside the repository......Page 330 Summary......Page 331 Chapter 10: Customizing and Extending Git......Page 334 Git-aware command prompt......Page 335 Command-line completion for Git......Page 338 Autocorrection for Git commands......Page 339 Making the command line prettier......Page 340 Graphical interfaces......Page 341 Types of graphical tools......Page 342 Graphical diff and merge tools......Page 343 Graphical interface examples......Page 344 Command-line options and environment variables......Page 345 Git configuration files......Page 346 The syntax of Git configuration files......Page 347 Accessing the Git configuration......Page 348 Basic client-side configuration......Page 350 Preserving undo information – the expiry of objects......Page 352 Formatting and whitespace......Page 353 Per-file configuration with gitattributes......Page 355 Installing a Git hook......Page 358 Client-side hooks......Page 359 Commit process hooks......Page 360 Other client-side hooks......Page 362 Server-side hooks......Page 364 Extending Git......Page 365 Command aliases for Git......Page 366 Credential helpers and remote helpers......Page 368 Summary......Page 369 Chapter 11: Git Administration......Page 370 Repository maintenance......Page 371 Recovering a lost commit......Page 372 Troubleshooting Git......Page 374 Server-side hooks......Page 375 The pre-receive hook......Page 376 The update hook......Page 377 The post-receive hook......Page 378 Enforcing the policy with server-side hooks......Page 379 Early notices about policy violations with client-side hooks......Page 381 Serving Git repositories......Page 382 SSH protocol......Page 383 Anonymous Git protocol......Page 384 Smart HTTP(S) protocol......Page 385 Dumb protocols......Page 386 Tools to manage Git repositories......Page 387 Reducing the size taken by repositories......Page 388 Speeding up smart protocols with pack bitmaps......Page 389 Augmenting development workflows......Page 390 Summary......Page 391 Starting a project......Page 392 Choosing which files to keep under version control......Page 393 Working on a topic branch......Page 394 Deciding what to base your work on......Page 395 Splitting changes into logically separate steps......Page 396 Writing a good commit message......Page 397 Integrating changes......Page 399 Submitting and describing changes......Page 400 The art of the change review......Page 401 Responding to reviews and comments......Page 403 Don't panic, recovery is almost always possible......Page 404 Numbering and tagging releases......Page 405 Summary......Page 406 Index......Page 408 Attain expert-level proficiency with Git for enhanced productivity and efficient collaboration by mastering advanced distributed version control featuresKey Features[•]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 submodulesBook DescriptionGit 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. 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 workflowWho this book is forIf 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.