Statistics
61
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-12-09

AuthorBernd Öggl, Michael Kofler

Get started with Git―today! Walk through installation and explore the variety of development environments available. Understand the concepts that underpin Git’s workflows, from branching to commits, and see how to use major platforms, like GitHub. Learn the ins and outs of working with Git for day-to-day development. Get your versioning under control! Get hands-on practical experience with Git Understand branches, commands, commits, workflows, and more Learn to use GitHub, GitLab, and alternative Git platforms Installation and Setup Install Git on your machine and explore the interfaces and development environments available: IntelliJ, VS Code, Atom, and more. Understand the interactions between Git and the command line. Key Functionality Walk through the core functionality of Git and GitHub: repositories, commits, pushes and pulls, branches, merging, hooks, and the importance of properly-written commit messages. Best Practices and Troubleshooting Get the most out of Git using best practices for managing Unix dotfiles, switching a project from SVN to Git, and more. Explore common error messages and learn how to avoid and rectify them.

Tags
No tags
ISBN: 1493222899
Publisher: Rheinwerk Publishing
Language: 英文
File Format: PDF
File Size: 27.7 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
The Rheinwerk Computing series from Rheinwerk Publishing offers new and established professionals comprehensive guidance to enrich their skillsets and enhance their career prospects. Our publications are written by leading experts in the fields of programming, administration, security, analytics, and more. Each book is detailed and hands-on to help readers develop essential, practical skills that they can apply to their daily work. For further information, please visit our website: www.rheinwerk-computing.com. Philip Ackermann JavaScript: The Comprehensive Guide 2022, 982 pages, paperback and e-book www.rheinwerk-computing.com/5554 Sebastian Springer Node.js: The Comprehensive Guide 2022, 834 pages, paperback and e-book www.rheinwerk-computing.com/5556 Johannes Ernesti, Peter Kaiser Python 3: The Comprehensive Guide 2022, 1036 pages, paperback and e-book www.rheinwerk-computing.com/5566 Christian Ullenboom Java: The Comprehensive Guide 2023, 1128 pages, paperback and e-book www.rheinwerk-computing.com/5557 Bernd Öggl, Michael Kofler Docker: Practical Guide for Developers and DevOps Teams 2023, approx. 496 pp., paperback and e-book www.rheinwerk-computing.com/5650 Rheinwerk Computing
Bernd Öggl, Michael Kofler Git Project Management for Developers and DevOps Teams
Dear Reader, Never have I felt a greater need for version control than when I played a game of pool with five children under 10. On a recent family vacation to a cabin in Colorado, the basement pool table was a big hit. When I joined my nieces and nephews for a game, I quickly realized that the rules would be nontraditional, to say the least. There were spontaneous guidelines for using hands (offensive) versus pool cues (defensive), guarding pockets, and scoring combos. Learning the rules was hard enough; tracking them as they changed minute-by-minute was harder. And then there was mediating the different versions of the game between the five kids. One niece’s arrangement of stripes and solids was one nephew’s target for impact. Despite my best efforts, fingers were pinched in the chaotic flurry of ideas. Working together in an organized (and peaceful) way has its challenges, regardless of age or subject. In the programming world, collaboration is essential. Developers need to write, test, and iterate on code in tandem with other team members, and the differ- ent versions need to be controlled to maintain organized, traceable changes and avoid loss of work. That’s where Git comes in—and this book. What did you think about Git: Project Management for Developers and DevOps Teams? Your comments and suggestions are the most useful tools to help us make our books the best they can be. Please feel free to contact me and share any praise or criticism you may have. Thank you for purchasing a book from Rheinwerk Publishing! Megan Fuerst Editor, SAP PRESS meganf@rheinwerk-publishing.com www.rheinwerk-computing.com Rheinwerk Publishing • Boston, MA
Imprint This e-book is a publication many contributed to, specifically: Editor Megan Fuerst Acquisitions Editor Hareem Shafi German Edition Editors Christoph Meister, Anne Scheibe Translation Winema Language Services, Inc. Copyeditor Yvette Chin Cover Design Graham Geary Photo Credit Shutterstock: 80373751/© tovovana; iStockphoto: 157567712/© grandriver Layout Design Vera Brauner Production E-Book Graham Geary Typesetting E-Book III-satz, Germany We hope that you liked this e-book. Please share your feedback with us and read the Service Pages to find out how to contact us. Library of Congress Cataloging-in Publication Control Number: 2022035131 ISBN 978-1-4932-2289-6 (print) ISBN 978-1-4932-2290-2 (e-book) ISBN 978-1-4932-2291-9 (print and e-book) © 2023 by Rheinwerk Publishing, Inc., Boston (MA) 1st edition 2023 2nd German edition published 2022 by Rheinwerk Verlag, Bonn, Germany
Contents Preface ....................................................................................................................................................... 19 1 Git in Ten Minutes 23 1.1 What Is Git? .............................................................................................................................. 23 1.1.1 The git Command ................................................................................................... 23 1.1.2 Git User Interfaces ................................................................................................. 24 1.1.3 Git versus GitHub versus GitLab ....................................................................... 24 1.2 Downloading Software from GitHub ........................................................................... 25 1.2.1 Example: Hello World! .......................................................................................... 26 1.2.2 Example: Python Game ........................................................................................ 27 1.3 Learning to Program with Git Support ........................................................................ 27 1.3.1 Preparation Tasks ................................................................................................... 28 1.3.2 Programming and Synchronizing ..................................................................... 29 1.3.3 The Git Time Machine ........................................................................................... 30 2 Learning by Doing 31 2.1 Installing the git Command .............................................................................................. 31 2.1.1 Linux ........................................................................................................................... 32 2.1.2 macOS ........................................................................................................................ 32 2.1.3 Windows ................................................................................................................... 32 2.1.4 Changing Options Later and Performing Updates ...................................... 36 2.1.5 Changing the Default Editor ............................................................................... 36 2.1.6 Git Bash, cmd.exe, PowerShell, or Windows Terminal? ............................ 37 2.1.7 Git Bash ...................................................................................................................... 37 2.1.8 Git in the Windows Subsystem for Linux ....................................................... 38 2.2 Setting Up a GitHub Account and Repositories ....................................................... 38 2.2.1 Setting Up a GitHub Account ............................................................................. 39 2.2.2 Setting Up Repositories ........................................................................................ 39 2.2.3 Giving Access to a Repository ............................................................................. 41 2.2.4 GitHub Organizations ........................................................................................... 41 2.2.5 Setting Up Personal Access Tokens .................................................................. 427
Contents2.3 Using the git Command ...................................................................................................... 43 2.3.1 Setting the Name and Email Address (git config) ....................................... 44 2.3.2 Downloading a Repository (git clone) ............................................................. 44 2.3.3 Adding Files (git add) ............................................................................................. 46 2.3.4 Saving an Intermediate State (git commit) ................................................... 46 2.3.5 Adding and Changing Files, More Commits .................................................. 47 2.3.6 Status (git status) ................................................................................................... 47 2.3.7 Excluding Files from Git Management (.gitignore file) ............................. 48 2.3.8 Transferring the Repository to a Remote Server (git push) ...................... 49 2.3.9 Updating the Local Repository (git pull) ......................................................... 50 2.3.10 Uploading a Local Repository to GitHub/GitLab .......................................... 51 2.3.11 Branches (git checkout and git merge) ........................................................... 52 2.3.12 Logging (git log) ...................................................................................................... 55 2.3.13 More Git Commands, Options, Special Cases, and Basics ........................ 56 2.4 Authentication ..................................................................................................................... 56 2.4.1 Windows Credential Manager ........................................................................ 57 2.4.2 macOS Keychain ..................................................................................................... 59 2.4.3 libsecret (Linux) ....................................................................................................... 60 2.4.4 SSH instead of HTTPS ............................................................................................ 61 2.4.5 Different SSH Keys for Multiple GitHub/GitLab Accounts ....................... 64 2.4.6 If It Doesn't Work .................................................................................................... 65 2.5 Learning Git in a Playful Way (Githug) ........................................................................ 66 2.5.1 Requirements .......................................................................................................... 66 2.5.2 Game Structure ....................................................................................................... 67 2.6 IDEs and Editors ...................................................................................................................... 68 2.6.1 Git GUI ........................................................................................................................ 68 2.6.2 GitHub Desktop ...................................................................................................... 69 2.6.3 IntelliJ IDEA ............................................................................................................... 71 2.6.4 TortoiseGit ................................................................................................................ 73 2.6.5 Visual Studio Code ................................................................................................ 73 2.6.6 Xcode .......................................................................................................................... 74 2.7 Contributing to a Third-Party GitHub Project ........................................................... 76 2.7.1 Forks ............................................................................................................................ 76 2.7.2 Pull Requests ............................................................................................................ 76 2.8 Synchronization and Backups .......................................................................................... 78 2.8.1 Git Issues ................................................................................................................... 78 2.8.2 Conclusion ................................................................................................................ 788
Contents3 Basic Principles of Git 81 3.1 Terminology ............................................................................................................................. 81 3.1.1 About Commits ....................................................................................................... 82 3.1.2 Log and Logging ...................................................................................................... 84 3.1.3 Local and Remote Repositories .......................................................................... 84 3.1.4 Hooks, Submodules, and Subtrees ................................................................... 85 3.2 The Git Database ................................................................................................................... 85 3.2.1 The .git Directory .................................................................................................... 85 3.2.2 Git Object Types: Commits, BLOBs, Trees, and Tags ................................... 87 3.2.3 References ................................................................................................................. 88 3.3 Commits ..................................................................................................................................... 89 3.3.1 The Staging Area ..................................................................................................... 89 3.3.2 The Commit .............................................................................................................. 91 3.3.3 More Details ............................................................................................................. 92 3.3.4 Renaming, Moving, or Deleting Files from the Repository ....................... 95 3.4 Commit-Undo ......................................................................................................................... 96 3.4.1 Not Saving a Change Permanently after All (git reset) .............................. 96 3.4.2 Restoring Changes Made since the Last Commit (git restore) ................ 96 3.4.3 Viewing a File in an Old Version (git show) .................................................. 97 3.4.4 Viewing Changes Compared to an Old Version (git diff) ......................... 98 3.4.5 Restoring a File to an Old Version (git restore) ............................................ 98 3.4.6 Reverting the Last Commits (git revert) ......................................................... 99 3.4.7 Reverting the Last Commits (git reset) ........................................................... 100 3.4.8 Switching Temporarily to an Older Commit (git checkout) ..................... 101 3.4.9 Changing the Commit Message ........................................................................ 103 3.5 Branches .................................................................................................................................... 104 3.5.1 Using Branches ...................................................................................................... 105 3.5.2 Problems Switching between Branches (git checkout) ............................. 107 3.5.3 Determining “main” as the Default Name for New Repositories .......... 108 3.5.4 Renaming “master” to “main” ........................................................................... 108 3.5.5 Internal Details ........................................................................................................ 109 3.6 Merging ...................................................................................................................................... 110 3.6.1 Merging Branches (git merge) ...................................................................... 110 3.6.2 Main Merge or Feature Merge? ......................................................................... 113 3.6.3 Fast-Forward Merges ............................................................................................ 114 3.6.4 Octopus Merges ...................................................................................................... 115 3.6.5 Merge Process .......................................................................................................... 115 3.6.6 Cherry-Picking ......................................................................................................... 1159
Contents3.7 Stashing .................................................................................................................................... 117 3.7.1 Caching and Restoring Changes ........................................................................ 117 3.7.2 Stashing in Practice ............................................................................................... 118 3.7.3 Managing Multiple Changes .............................................................................. 118 3.8 Remote Repositories ............................................................................................................ 118 3.8.1 Initialization Work ................................................................................................. 119 3.8.2 Push and Pull .......................................................................................................... 120 3.8.3 Remote Branches ................................................................................................... 122 3.8.4 Internal Details ....................................................................................................... 125 3.8.5 Multiple Remote Repositories ............................................................................ 126 3.8.6 Workflows ................................................................................................................. 128 3.8.7 Configuring Your Own Git Server ..................................................................... 128 3.9 Resolving Merge Conflicts ............................................................................................. 129 3.9.1 Collisions in the Code ............................................................................................ 129 3.9.2 Merge Tools .............................................................................................................. 131 3.9.3 Binary File Conflicts .............................................................................................. 132 3.9.4 Merge Abort and Undo ......................................................................................... 133 3.9.5 Content-Related Merge Conflicts ..................................................................... 134 3.9.6 MERGE Files .............................................................................................................. 135 3.10 Rebasing ........................................................................................................................... 135 3.10.1 Example ..................................................................................................................... 136 3.10.2 Concept ...................................................................................................................... 137 3.10.3 Merge Conflicts during Rebasing ...................................................................... 137 3.10.4 Side Effects ............................................................................................................... 138 3.10.5 Pull with Rebasing .................................................................................................. 138 3.10.6 Special Rebasing Cases and Undo .................................................................... 139 3.10.7 Squashing ................................................................................................................ 140 3.11 Tags ........................................................................................................................................ 141 3.11.1 Listing Tags ............................................................................................................... 141 3.11.2 Simple Tags versus Annotated Tags ................................................................ 142 3.11.3 Synchronizing Tags ................................................................................................ 143 3.11.4 Setting Tags Subsequently .................................................................................. 143 3.11.5 Deleting Tags ........................................................................................................... 144 3.11.6 Modifying or Correcting Tags (Retagging) ..................................................... 144 3.11.7 Signed Tags ............................................................................................................. 145 3.12 References to Commits ....................................................................................................... 145 3.12.1 Reference Names .................................................................................................... 146 3.12.2 refname@{date} and refname@{n} ................................................................ 147 3.12.3 Accessing Previous Versions ............................................................................... 148 3.12.4 Examples ................................................................................................................... 149 3.12.5 References to Files .................................................................................................. 15010
Contents3.13 Internal Details of Git .......................................................................................................... 150 3.13.1 Object Packages ..................................................................................................... 150 3.13.2 SHA-1 Hash Codes .................................................................................................. 151 3.13.3 The .git/index File ................................................................................................... 151 3.13.4 Commands for Managing the Git Database ................................................. 152 4 Data Analysis in the Git Repository 153 4.1 Searching Commits (git log) ............................................................................................. 153 4.1.1 Clear Logging ........................................................................................................... 155 4.1.2 Custom Formatting (Pretty Syntax) ................................................................. 156 4.1.3 Searching Commit Messages ............................................................................. 157 4.1.4 Searching Commits That Modify Specific Files ............................................ 158 4.1.5 Searching Commits of a Specific Developer .................................................. 158 4.1.6 Restricting the Commit Range (Range Syntax) ............................................ 159 4.1.7 Limiting Commits in Time ................................................................................... 160 4.1.8 Sorting Commits ..................................................................................................... 160 4.1.9 Tagged Commits (git tag) .................................................................................... 162 4.1.10 Reference Log (git reflog) ..................................................................................... 162 4.2 Searching Files ........................................................................................................................ 163 4.2.1 Viewing Old Versions of a File (git show) ....................................................... 163 4.2.2 Viewing Differences between Files (git diff) ................................................. 164 4.2.3 Viewing Differences between Commits ......................................................... 165 4.2.4 Searching Files (git grep) ...................................................................................... 166 4.2.5 Determining the Authorship of Code (git blame) ....................................... 168 4.3 Searching for Errors (git bisect) ....................................................................................... 169 4.4 Statistics and Visualization ............................................................................................... 170 4.4.1 Simple Number Games (git shortlog) .............................................................. 170 4.4.2 Statistical Tools and Scripts ................................................................................ 171 4.4.3 Visualizing Branches ............................................................................................. 172 4.4.4 GitGraph.js ................................................................................................................ 173 5 GitHub 175 5.1 Pull Requests ........................................................................................................................... 176 5.1.1 Pull Requests on a Team ...................................................................................... 177 5.1.2 Pull Requests in Public Projects ......................................................................... 17911
Contents5.2 Actions ........................................................................................................................................ 180 5.2.1 YAML Syntax ............................................................................................................ 181 5.2.2 Notification to Slack .............................................................................................. 182 5.2.3 The Continuous Integration Pipeline ............................................................... 184 5.3 Package Manager (GitHub Packages) ......................................................................... 188 5.3.1 Example ..................................................................................................................... 189 5.4 Automatic Security Scans .................................................................................................. 193 5.4.1 Node.js Security ...................................................................................................... 193 5.5 Other GitHub Features ........................................................................................................ 197 5.5.1 Collaboration ........................................................................................................... 197 5.5.2 Issues .......................................................................................................................... 197 5.5.3 Discussions and Teams ........................................................................................ 198 5.5.4 Wiki ............................................................................................................................. 199 5.5.5 Gists ............................................................................................................................ 200 5.5.6 GitHub Pages ........................................................................................................... 201 5.6 GitHub Command-Line Interface ................................................................................... 202 5.6.1 Installation ................................................................................................................ 203 5.6.2 Examples of Use ...................................................................................................... 204 5.7 Codespaces ............................................................................................................................... 206 6 GitLab 209 6.1 On-Premise versus Cloud ................................................................................................... 210 6.2 Installation ............................................................................................................................... 211 6.2.1 Installing GitLab Runner ...................................................................................... 214 6.2.2 Backup ........................................................................................................................ 216 6.3 The First Project ...................................................................................................................... 218 6.4 Pipelines .................................................................................................................................... 220 6.4.1 Auto DevOps ............................................................................................................ 220 6.4.2 Manual Pipelines .................................................................................................... 223 6.4.3 Test Stage in the Manual Pipeline .................................................................... 225 6.4.4 Release Stage in the Manual Pipeline ............................................................. 226 6.4.5 Debugging Pipelines .............................................................................................. 227 6.5 Merge Requests ...................................................................................................................... 229 6.6 Web IDE ...................................................................................................................................... 232 6.7 Gitpod ......................................................................................................................................... 23312
Contents7 Azure DevOps, Bitbucket, Gitea, and Gitolite 237 7.1 Azure DevOps .......................................................................................................................... 237 7.1.1 Trying Out Azure DevOps .................................................................................... 238 7.1.2 Test Plans .................................................................................................................. 241 7.1.3 Conclusion ................................................................................................................ 242 7.2 Bitbucket ................................................................................................................................... 242 7.3 Gitea ............................................................................................................................................ 244 7.3.1 Trying Out Gitea ..................................................................................................... 244 7.3.2 Server Installation with Docker ......................................................................... 245 7.3.3 Server Installation on Ubuntu 20.04 ................................................................ 247 7.3.4 A First Example with Gitea .................................................................................. 250 7.4 Gitolite ........................................................................................................................................ 255 7.4.1 Installation ................................................................................................................ 255 7.4.2 Application ................................................................................................................ 256 8 Workflows 259 8.1 Instructions for the Team .................................................................................................. 259 8.2 Solo Development ................................................................................................................. 260 8.2.1 Conclusion ................................................................................................................ 261 8.3 Feature Branches for Teams ............................................................................................. 262 8.3.1 New Function, New Branch ................................................................................ 262 8.3.2 Example ..................................................................................................................... 262 8.3.3 Code Review ............................................................................................................. 266 8.3.4 Merge ......................................................................................................................... 266 8.3.5 Rebasing .................................................................................................................... 267 8.3.6 Conclusion ................................................................................................................ 268 8.4 Merge/Pull Requests ............................................................................................................ 269 8.4.1 Forks ............................................................................................................................ 271 8.4.2 Conclusion ................................................................................................................ 272 8.5 Long-Running Branches: Gitflow ................................................................................... 272 8.5.1 Main, Develop, Feature ......................................................................................... 273 8.5.2 Hot Bugfixes ............................................................................................................. 275 8.5.3 Bugfixes in the develop Branch ......................................................................... 276 8.5.4 Another New Function ......................................................................................... 276 8.5.5 Conclusion ................................................................................................................ 27713
Contents8.6 Trunk-Based Development ............................................................................................... 277 8.6.1 Continuous Integration ........................................................................................ 278 8.6.2 Release-Ready .......................................................................................................... 279 8.6.3 Continuous Deployment ...................................................................................... 279 8.6.4 Feature Flags ............................................................................................................ 279 8.6.5 Conclusion ................................................................................................................ 280 8.7 Which Workflow Is the Right One? ............................................................................... 280 9 Working Techniques 283 9.1 Hooks .......................................................................................................................................... 283 9.1.1 Hooks in Real Life .................................................................................................... 284 9.1.2 Explanation of the Sample Script ..................................................................... 286 9.1.3 More Hooks .............................................................................................................. 286 9.1.4 Hooks versus Actions/Pipelines ......................................................................... 287 9.2 Concise Commit Messages ................................................................................................ 288 9.2.1 Multiline Commit Messages in the Console ................................................. 288 9.2.2 Commit Title and Text .......................................................................................... 289 9.2.3 Character Set and Language ............................................................................... 291 9.2.4 Links to Issues or Pull Requests .......................................................................... 291 9.2.5 Commit Messages of the Angular Project ..................................................... 293 9.2.6 Conclusion ................................................................................................................ 294 9.3 Submodules and Subtrees ................................................................................................. 295 9.3.1 Copying ...................................................................................................................... 296 9.3.2 Using the Package Manager ............................................................................... 296 9.3.3 Submodules .............................................................................................................. 297 9.3.4 Subtrees ..................................................................................................................... 300 9.3.5 Internal Details ........................................................................................................ 302 9.3.6 Subtree Split ............................................................................................................. 303 9.3.7 Monorepos ................................................................................................................ 304 9.4 Bash and Zsh ............................................................................................................................ 305 9.4.1 Git Aliases ................................................................................................................. 305 9.4.2 Autocompletion in Bash ....................................................................................... 306 9.4.3 Oh My Zsh! ................................................................................................................ 306 9.5 Two-Factor Authentication ............................................................................................... 307 9.5.1 Enabling Two-Factor Authentication on GitHub ......................................... 308 9.5.2 Hardware Security Keys ....................................................................................... 310 9.5.3 Applying Two-Factor Authentication .............................................................. 31014
Contents10 Git in Real Life 313 10.1 Etckeeper ................................................................................................................................... 313 10.1.1 Usage Example ........................................................................................................ 314 10.1.2 Installation and Configuration .......................................................................... 315 10.2 Managing Dotfiles with Git .............................................................................................. 316 10.2.1 Dotfiles ....................................................................................................................... 316 10.2.2 Dotfiles under Git Control ................................................................................... 317 10.2.3 git-dir and work-tree ............................................................................................. 317 10.2.4 Setting Up Dotfiles on a New Computer ........................................................ 320 10.2.5 Shell Commands ..................................................................................................... 321 10.2.6 Vim Configuration ................................................................................................. 322 10.2.7 Miscellaneous .......................................................................................................... 323 10.3 Accessing Apache Subversion (git-svn) ....................................................................... 323 10.3.1 Application ................................................................................................................ 323 10.3.2 Subversion Commits ............................................................................................. 325 10.4 Migrating from Apache Subversion to Git ................................................................. 327 10.4.1 Authors ...................................................................................................................... 327 10.4.2 Import ......................................................................................................................... 329 10.4.3 Tags and Branches ................................................................................................. 329 10.5 Blog with Git and Hugo ...................................................................................................... 331 10.5.1 From WordPress to Hugo ..................................................................................... 331 10.5.2 Hugo ........................................................................................................................... 332 10.5.3 Hugo Themes as Git Submodules ..................................................................... 333 10.5.4 Filling a Blog with Content .................................................................................. 335 10.5.5 Git LFS ......................................................................................................................... 336 10.5.6 Deploying with Netlify .......................................................................................... 339 10.5.7 Deploying with a GitHub Action and GitHub Pages ................................... 340 11 Git Problems and Their Solutions 343 11.1 Git Error Messages (Root Cause and Solution) ......................................................... 343 11.1.1 Repository Not Found ........................................................................................... 344 11.1.2 Please Tell Me Who You Are (git commit) ...................................................... 344 11.1.3 Authentication Failed (git pull, git push) ........................................................ 344 11.1.4 Invalid User Name or Password (git clone, git pull, git push) .................. 344 11.1.5 Permission Denied, Public Key (git clone, git pull, git push) .................... 345 11.1.6 Permission Denied, Unable to Access <repo> (git push) ........................... 345 11.1.7 Changes Not Staged for Commit (git commit) ............................................ 34515
Contents11.1.8 Your Local Changes Would Be Overwritten (git checkout, git switch) ...................................................................................... 345 11.1.9 Your Branch Is Ahead of a Remote/Branch by n Commits (git pull, git status) ................................................................................................. 346 11.1.10 You’re in a Detached HEAD State (git checkout) ......................................... 346 11.1.11 Pathspec Did Not Match Any Files Known to Git (git checkout) ............ 346 11.1.12 Please Enter a Commit Message to Explain Why This Merge Is Necessary (git pull) ................................................................................................. 347 11.1.13 Pulling without Specifying How to Reconcile Divergent Branches Is Discouraged (git pull) .................................................................... 347 11.1.14 Cannot Pull with Rebase: You Have Unstaged/Uncommitted Changes (git pull) ................................................................................................... 347 11.1.15 There Is No Tracking Information for the Current Branch (git pull) ...... 348 11.1.16 Your Local Changes Would Be Overwritten (git merge, git pull) ............ 348 11.1.17 Failed to Push Some Refs to <somerepo.git> (git push) ............................ 348 11.1.18 The Current Branch <name> Has No Upstream Branch (git push) ....... 349 11.1.19 Merge Failed, Merge Conflict in <file> (git merge, etc.) ............................ 349 11.2 Saving Empty Directories ................................................................................................... 350 11.3 Merge for a Single File ......................................................................................................... 350 11.3.1 git merge-file ........................................................................................................... 350 11.3.2 git checkout .............................................................................................................. 351 11.4 Deleting Files Permanently from Git ............................................................................ 351 11.4.1 Local Changes Only, without Push (git rm) ................................................... 352 11.4.2 Previously Uploaded Changes, with Push (git filter-branch) ................... 353 11.4.3 Previously Uploaded Changes, after Push (git filter-repo) ....................... 356 11.4.4 Previously Uploaded Changes, after Push (BFG Repo Cleaner) ............... 357 11.5 Splitting a Project .................................................................................................................. 359 11.6 Moving Commits to a Different Branch ...................................................................... 359 11.6.1 git reset ...................................................................................................................... 360 11.6.2 git cherrypick ............................................................................................................ 361 12 Command Reference 363 12.1 The git Command .................................................................................................................. 363 12.1.1 Porcelain versus Plumbing .................................................................................. 365 12.1.2 General Options ...................................................................................................... 366 12.1.3 git add ........................................................................................................................ 366 12.1.4 git bisect .................................................................................................................... 36716
Contents12.1.5 git blame ................................................................................................................... 367 12.1.6 git branch .................................................................................................................. 368 12.1.7 git checkout .............................................................................................................. 369 12.1.8 git cherry-pick .......................................................................................................... 370 12.1.9 git clean ..................................................................................................................... 371 12.1.10 git clone ..................................................................................................................... 371 12.1.11 git commit ................................................................................................................ 372 12.1.12 git config ................................................................................................................... 373 12.1.13 git diff ......................................................................................................................... 374 12.1.14 git fetch ...................................................................................................................... 375 12.1.15 git gc ........................................................................................................................... 375 12.1.16 git gui .......................................................................................................................... 376 12.1.17 git grep ....................................................................................................................... 376 12.1.18 git init ......................................................................................................................... 377 12.1.19 git log .......................................................................................................................... 377 12.1.20 git ls-files ................................................................................................................... 379 12.1.21 git merge ................................................................................................................... 380 12.1.22 git merge-base ........................................................................................................ 381 12.1.23 git merge-file ........................................................................................................... 381 12.1.24 git mergetool ........................................................................................................... 381 12.1.25 git mv .......................................................................................................................... 381 12.1.26 git pull ........................................................................................................................ 382 12.1.27 git push ...................................................................................................................... 383 12.1.28 git rebase ................................................................................................................... 384 12.1.29 git reflog .................................................................................................................... 384 12.1.30 git remote ................................................................................................................. 384 12.1.31 git reset ...................................................................................................................... 385 12.1.32 git restore .................................................................................................................. 386 12.1.33 git rev-list .................................................................................................................. 387 12.1.34 git revert .................................................................................................................... 387 12.1.35 git rm .......................................................................................................................... 388 12.1.36 git shortlog ............................................................................................................... 388 12.1.37 git show ..................................................................................................................... 388 12.1.38 git stage ..................................................................................................................... 389 12.1.39 git status .................................................................................................................... 389 12.1.40 git submodule ......................................................................................................... 389 12.1.41 git subtree ................................................................................................................. 390 12.1.42 git switch ................................................................................................................... 390 12.1.43 git tag ......................................................................................................................... 391 12.2 Revision Syntax ...................................................................................................................... 392 12.2.1 Commit Ranges (rev1..rev2 versus rev1…rev2) ............................................ 39217
Contents12.3 git Configuration ................................................................................................................... 393 12.3.1 Configuration File .git/config ............................................................................. 393 12.3.2 Basic Settings ........................................................................................................... 394 12.3.3 Configuration File .gitignore ............................................................................... 396 12.3.4 Configuration File .gitmodules .......................................................................... 396 12.3.5 Configuration File .gitattributes ........................................................................ 397 The Authors ............................................................................................................................................. 399 Index .......................................................................................................................................................... 401 Service Pages .....................................................................................................................................  I Legal Notes .........................................................................................................................................  II18
Preface Whenever several people work together on a software project, a system is needed to store all the changes made in a traceable way. Such a version control system must also give all developers access to the entire project. Each programmer knows what the oth- ers have done recently; developers can try out the code of the others and test the code’s interaction with their own changes. In the past, many version control systems were used, such as Concurrent Versions Sys- tem (CVS), Apache Subversion (SVN), or Microsoft Visual SourceSafe (VSS). In the last decade, however, Git has become the de facto standard. The GitHub web platform played a significant role in this success, making learning and using Git much easier. Countless open-source projects use the free GitHub offering for project hosting. Commercial customers who didn’t want to publish their source code pay for this service. GitHub is not the only Git platform, of course: Major competitors include GitLab, Azure DevOps Services, and Bitbucket. Microsoft bought GitHub in 2018 for $7.5 billion. Unlike other acquisitions, this development hasn’t hurt GitHub’s popularity so far. The Story of Git Git came into being because Linus Torvalds needed a new version management system for the further development of the Linux kernel. The developer community had previ- ously used the BitKeeper program. Torvalds was basically satisfied with the program, but a license change necessitated a switch. Of the open-source programs available at the time, none met his high standards. So, the Linux chief developer briefly stopped his main work and created the basic framework for Git in just two weeks. The name Git stands for stupid or moron, and the help page man git also refers to the program as the stupid content tracker. That this definition was an understatement became clear only gradually, long after Tor- valds had given up on further development of Git and put it out of his hands. Not only did kernel developers quickly and easily switch their work to Git, but in the years that followed, more and more software projects outside the open-source world switched to Git as well. Git made its final breakthrough when web platforms such as GitHub and GitLab became established. These websites simplify the hosting of Git projects enormously and have become an indispensable part of everyday Git life. (Even the Linux kernel is now on GitHub!)19Personal Copy for Jaleel Hussain, alex76alex43@gmail.com
PrefaceIronically, Torvalds’ main goal in designing Git was to create a decentralized version control system. But the centralized approach of GitHub and others made Git attractive for developers outside the guru league. Some rate the importance of Git as highly as that of Linux. Torvalds has thus twice suc- ceeded in completely turning the software universe on its head. Everyone Uses It, but No One Understands It With all the enthusiasm, Git was clearly designed by professionals for professionals. We don’t want to give you the impression in this book that Git is easy because it isn’t. You’ll need to keep in mind the following considerations:  Often, more than one way exists to reach a goal. For readers already familiar with Git, this idea is useful, but if you’re just learning Git, this diversity can be confusing.  Many open-source projects are accused of being poorly documented. You really can’t say that about Git. On the contrary, every git command and every possible application is explained in man pages as well as on the website (https://git-scm.com/ docs) in such granularity and with so many conceivable special cases that you can get lost in the details.  Complicating matters further, some terms carry many different meanings, and eas- ily confused subcommands might perform widely divergent tasks. Some terms have different meanings depending on their context or are used inconsistently in the documentation. We have a confession: Despite years of using Git, we still learned a lot while writing this book! About This Book Of course, you can use Git in a minimalistic way. However, small deviations from the daily routine can then lead to surprising and often incomprehensible side effects or errors. Every Git beginner knows that feeling when a git command returns an incomprehen- sible error message: In a cold sweat, you wonder whether you’ve just permanently destroyed a repository for all your developers and try to find someone to persuade Git to continue working after all with the right commands. Thus, describing Git without going into depth isn’t useful. Only a good understanding of how Git works gives you the confidence you’ll need to cleanly fix merge conflicts or other problems.20 © 2025 by Rheinwerk Publishing Inc., Boston (MA)
About This BookAt the same time, however, we knew that this book could only work if we gave priority to the essential functions. Despite its over 400 pages, this book is not the all-encompassing guide to Git. We cannot consider every single special case or introduce every Git sub- command, no matter how exotic. We’ve therefore tried to separate the wheat from the chaff in this book. This book is divided into manageable chapters that you can read as needed, like build- ing blocks:  After a short introduction in Chapter 1, we’ll introduce the use of Git in Chapter 2 through Chapter 4. We’ll focus on the use of Git at the command level and only mar- ginally discuss platforms such as GitHub or other user interfaces (UIs). For Git beginners, we recommend starting with these first four chapters. Even if you have some Git experience, you should definitely take a few hours to read Chapter 3 and try out some of the techniques we present (merging, rebasing, etc.) in a test repository.  The next three chapters introduce the most important Git platforms. Especially for complex projects, these platforms provide useful additional functions, for instance, to perform automatic tests or to implement continuous integration (CI). Of course, we also show you how to host your own Git repository. With GitLab, Gitea, or Gitolite, this goal can be realized relatively easily.  Then, we’ll turn from the basics to the practical: – In Chapter 8, we’ll describe popular patterns for guiding the work of numerous developers into orderly paths (branches) with Git. – Chapter 9 focuses on advanced Git features, such as hooks, submodules, subtrees, and two-factor authentication, which are all supported by major Git platforms. – Chapter 10 shows how you can use version configuration files (dotfiles) or the entire /etc directory with Git on Linux systems, how to switch a project from SVN to Git, and how to realize a simple website quickly and easily with Git and Hugo. – Chapter 11 helps you break the deadlock on hard-to-understand error messages. In this chapter, you’ll also find instructions on implementing special requests, for example, removing large files from a Git repository or performing a merge oper- ation on a selected file only.  To close the book, Chapter 12 briefly summarizes the most important git com- mands and their options. In this chapter, we followed the motto “Less is more.” Our goal was not a complete reference, but rather a guide to the “essence of Git”. Sample Repositories Some examples from this book are available on GitHub at the following link: https://github.com/git-compendium21Personal Copy for Jaleel Hussain, alex76alex43@gmail.com