Dear Reader
Notes on Usage
Table of Contents
Preface
The Story of Git
Everyone Uses It, but No One Understands It
About This Book
A Note to Readers
1 Git in Ten Minutes
1.1 What Is Git?
1.1.1 The git Command
1.1.2 Git User Interfaces
1.1.3 Git versus GitHub versus GitLab
1.2 Downloading Software from GitHub
1.2.1 Example: Hello World!
1.2.2 Example: Python Game
1.3 Learning to Program with Git Support
1.3.1 Preparation Tasks
1.3.2 Programming and Synchronizing
1.3.3 The Git Time Machine
2 Learning by Doing
2.1 Installing the git Command
2.1.1 Linux
2.1.2 macOS
2.1.3 Windows
2.1.4 Changing Options Later and Performing Updates
2.1.5 Changing the Default Editor
2.1.6 Git Bash, cmd.exe, PowerShell, or Windows Terminal?
2.1.7 Git Bash
2.1.8 Git in the Windows Subsystem for Linux
2.2 Setting Up a GitHub Account and Repositories
2.2.1 Setting Up a GitHub Account
2.2.2 Setting Up Repositories
2.2.3 Giving Access to a Repository
2.2.4 GitHub Organizations
2.2.5 Setting Up Personal Access Tokens
2.3 Using the git Command
2.3.1 Setting the Name and Email Address (git config)
2.3.2 Downloading a Repository (git clone)
2.3.3 Adding Files (git add)
2.3.4 Saving an Intermediate State (git commit)
2.3.5 Adding and Changing Files, More Commits
2.3.6 Status (git status)
2.3.7 Excluding Files from Git Management (.gitignore file)
2.3.8 Transferring the Repository to a Remote Server (git push)
2.3.9 Updating the Local Repository (git pull)
2.3.10 Uploading a Local Repository to GitHub/GitLab
2.3.11 Branches (git checkout and git merge)
2.3.12 Logging (git log)
2.3.13 More Git Commands, Options, Special Cases, and Basics
2.4 Authentication
2.4.1 Windows Credential Manager
2.4.2 macOS Keychain
2.4.3 libsecret (Linux)
2.4.4 SSH instead of HTTPS
2.4.5 Different SSH Keys for Multiple GitHub/GitLab Accounts
2.4.6 If It Doesn't Work
2.5 Learning Git in a Playful Way (Githug)
2.5.1 Requirements
2.5.2 Game Structure
2.6 IDEs and Editors
2.6.1 Git GUI
2.6.2 GitHub Desktop
2.6.3 IntelliJ IDEA
2.6.4 TortoiseGit
2.6.5 Visual Studio Code
2.6.6 Xcode
2.7 Contributing to a Third-Party GitHub Project
2.7.1 Forks
2.7.2 Pull Requests
2.8 Synchronization and Backups
2.8.1 Git Issues
2.8.2 Conclusion
3 Basic Principles of Git
3.1 Terminology
3.1.1 About Commits
3.1.2 Log and Logging
3.1.3 Local and Remote Repositories
3.1.4 Hooks, Submodules, and Subtrees
3.2 The Git Database
3.2.1 The .git Directory
3.2.2 Git Object Types: Commits, BLOBs, Trees, and Tags
3.2.3 References
3.3 Commits
3.3.1 The Staging Area
3.3.2 The Commit
3.3.3 More Details
3.3.4 Renaming, Moving, or Deleting Files from the Repository
3.4 Commit-Undo
3.4.1 Not Saving a Change Permanently after All (git reset)
3.4.2 Restoring Changes Made since the Last Commit (git restore)
3.4.3 Viewing a File in an Old Version (git show)
3.4.4 Viewing Changes Compared to an Old Version (git diff)
3.4.5 Restoring a File to an Old Version (git restore)
3.4.6 Reverting the Last Commits (git revert)
3.4.7 Reverting the Last Commits (git reset)
3.4.8 Switching Temporarily to an Older Commit (git checkout)
3.4.9 Changing the Commit Message
3.5 Branches
3.5.1 Using Branches
3.5.2 Problems Switching between Branches (git checkout)
3.5.3 Determining “main” as the Default Name for New Repositories
3.5.4 Renaming “master” to “main”
3.5.5 Internal Details
3.6 Merging
3.6.1 Merging Branches (git merge)
3.6.2 Main Merge or Feature Merge?
3.6.3 Fast-Forward Merges
3.6.4 Octopus Merges
3.6.5 Merge Process
3.6.6 Cherry-Picking
3.7 Stashing
3.7.1 Caching and Restoring Changes
3.7.2 Stashing in Practice
3.7.3 Managing Multiple Changes
3.8 Remote Repositories
3.8.1 Initialization Work
3.8.2 Push and Pull
3.8.3 Remote Branches
3.8.4 Internal Details
3.8.5 Multiple Remote Repositories
3.8.6 Workflows
3.8.7 Configuring Your Own Git Server
3.9 Resolving Merge Conflicts
3.9.1 Collisions in the Code
3.9.2 Merge Tools
3.9.3 Binary File Conflicts
3.9.4 Merge Abort and Undo
3.9.5 Content-Related Merge Conflicts
3.9.6 MERGE Files
3.10 Rebasing
3.10.1 Example
3.10.2 Concept
3.10.3 Merge Conflicts during Rebasing
3.10.4 Side Effects
3.10.5 Pull with Rebasing
3.10.6 Special Rebasing Cases and Undo
3.10.7 Squashing
3.11 Tags
3.11.1 Listing Tags
3.11.2 Simple Tags versus Annotated Tags
3.11.3 Synchronizing Tags
3.11.4 Setting Tags Subsequently
3.11.5 Deleting Tags
3.11.6 Modifying or Correcting Tags (Retagging)
3.11.7 Signed Tags
3.12 References to Commits
3.12.1 Reference Names
3.12.2 refname@{date} and refname@{n}
3.12.3 Accessing Previous Versions
3.12.4 Examples
3.12.5 References to Files
3.13 Internal Details of Git
3.13.1 Object Packages
3.13.2 SHA-1 Hash Codes
3.13.3 The .git/index File
3.13.4 Commands for Managing the Git Database
4 Data Analysis in the Git Repository
4.1 Searching Commits (git log)
4.1.1 Clear Logging
4.1.2 Custom Formatting (Pretty Syntax)
4.1.3 Searching Commit Messages
4.1.4 Searching Commits That Modify Specific Files
4.1.5 Searching Commits of a Specific Developer
4.1.6 Restricting the Commit Range (Range Syntax)
4.1.7 Limiting Commits in Time
4.1.8 Sorting Commits
4.1.9 Tagged Commits (git tag)
4.1.10 Reference Log (git reflog)
4.2 Searching Files
4.2.1 Viewing Old Versions of a File (git show)
4.2.2 Viewing Differences between Files (git diff)
4.2.3 Viewing Differences between Commits
4.2.4 Searching Files (git grep)
4.2.5 Determining the Authorship of Code (git blame)
4.3 Searching for Errors (git bisect)
4.4 Statistics and Visualization
4.4.1 Simple Number Games (git shortlog)
4.4.2 Statistical Tools and Scripts
4.4.3 Visualizing Branches
4.4.4 GitGraph.js
5 GitHub
5.1 Pull Requests
5.1.1 Pull Requests on a Team
5.1.2 Pull Requests in Public Projects
5.2 Actions
5.2.1 YAML Syntax
5.2.2 Notification to Slack
5.2.3 The Continuous Integration Pipeline
5.3 Package Manager (GitHub Packages)
5.3.1 Example
5.4 Automatic Security Scans
5.4.1 Node.js Security
5.5 Other GitHub Features
5.5.1 Collaboration
5.5.2 Issues
5.5.3 Discussions and Teams
5.5.4 Wiki
5.5.5 Gists
5.5.6 GitHub Pages
5.6 GitHub Command-Line Interface
5.6.1 Installation
5.6.2 Examples of Use
5.7 Codespaces
6 GitLab
6.1 On-Premise versus Cloud
6.2 Installation
6.2.1 Installing GitLab Runner
6.2.2 Backup
6.3 The First Project
6.4 Pipelines
6.4.1 Auto DevOps
6.4.2 Manual Pipelines
6.4.3 Test Stage in the Manual Pipeline
6.4.4 Release Stage in the Manual Pipeline
6.4.5 Debugging Pipelines
6.5 Merge Requests
6.6 Web IDE
6.7 Gitpod
7 Azure DevOps, Bitbucket, Gitea, and Gitolite
7.1 Azure DevOps
7.1.1 Trying Out Azure DevOps
7.1.2 Test Plans
7.1.3 Conclusion
7.2 Bitbucket
7.3 Gitea
7.3.1 Trying Out Gitea
7.3.2 Server Installation with Docker
7.3.3 Server Installation on Ubuntu 20.04
7.3.4 A First Example with Gitea
7.4 Gitolite
7.4.1 Installation
7.4.2 Application
8 Workflows
8.1 Instructions for the Team
8.2 Solo Development
8.2.1 Conclusion
8.3 Feature Branches for Teams
8.3.1 New Function, New Branch
8.3.2 Example
8.3.3 Code Review
8.3.4 Merge
8.3.5 Rebasing
8.3.6 Conclusion
8.4 Merge/Pull Requests
8.4.1 Forks
8.4.2 Conclusion
8.5 Long-Running Branches: Gitflow
8.5.1 Main, Develop, Feature
8.5.2 Hot Bugfixes
8.5.3 Bugfixes in the develop Branch
8.5.4 Another New Function
8.5.5 Conclusion
8.6 Trunk-Based Development
8.6.1 Continuous Integration
8.6.2 Release-Ready
8.6.3 Continuous Deployment
8.6.4 Feature Flags
8.6.5 Conclusion
8.7 Which Workflow Is the Right One?
9 Working Techniques
9.1 Hooks
9.1.1 Hooks in Real Life
9.1.2 Explanation of the Sample Script
9.1.3 More Hooks
9.1.4 Hooks versus Actions/Pipelines
9.2 Concise Commit Messages
9.2.1 Multiline Commit Messages in the Console
9.2.2 Commit Title and Text
9.2.3 Character Set and Language
9.2.4 Links to Issues or Pull Requests
9.2.5 Commit Messages of the Angular Project
9.2.6 Conclusion
9.3 Submodules and Subtrees
9.3.1 Copying
9.3.2 Using the Package Manager
9.3.3 Submodules
9.3.4 Subtrees
9.3.5 Internal Details
9.3.6 Subtree Split
9.3.7 Monorepos
9.4 Bash and Zsh
9.4.1 Git Aliases
9.4.2 Autocompletion in Bash
9.4.3 Oh My Zsh!
9.5 Two-Factor Authentication
9.5.1 Enabling Two-Factor Authentication on GitHub
9.5.2 Hardware Security Keys
9.5.3 Applying Two-Factor Authentication
10 Git in Real Life
10.1 Etckeeper
10.1.1 Usage Example
10.1.2 Installation and Configuration
10.2 Managing Dotfiles with Git
10.2.1 Dotfiles
10.2.2 Dotfiles under Git Control
10.2.3 git-dir and work-tree
10.2.4 Setting Up Dotfiles on a New Computer
10.2.5 Shell Commands
10.2.6 Vim Configuration
10.2.7 Miscellaneous
10.3 Accessing Apache Subversion (git-svn)
10.3.1 Application
10.3.2 Subversion Commits
10.4 Migrating from Apache Subversion to Git
10.4.1 Authors
10.4.2 Import
10.4.3 Tags and Branches
10.5 Blog with Git and Hugo
10.5.1 From WordPress to Hugo
10.5.2 Hugo
10.5.3 Hugo Themes as Git Submodules
10.5.4 Filling a Blog with Content
10.5.5 Git LFS
10.5.6 Deploying with Netlify
10.5.7 Deploying with a GitHub Action and GitHub Pages
11 Git Problems and Their Solutions
11.1 Git Error Messages (Root Cause and Solution)
11.1.1 Repository Not Found
11.1.2 Please Tell Me Who You Are (git commit)
11.1.3 Authentication Failed (git pull, git push)
11.1.4 Invalid User Name or Password (git clone, git pull, git push)
11.1.5 Permission Denied, Public Key (git clone, git pull, git push)
11.1.6 Permission Denied, Unable to Access <repo> (git push)
11.1.7 Changes Not Staged for Commit (git commit)
11.1.8 Your Local Changes Would Be Overwritten (git checkout, git switch)
11.1.9 Your Branch Is Ahead of a Remote/Branch by n Commits (git pull, git status)
11.1.10 You’re in a Detached HEAD State (git checkout)
11.1.11 Pathspec Did Not Match Any Files Known to Git (git checkout)
11.1.12 Please Enter a Commit Message to Explain Why This Merge Is Necessary (git pull)
11.1.13 Pulling without Specifying How to Reconcile Divergent Branches Is Discouraged (git pull)
11.1.14 Cannot Pull with Rebase: You Have Unstaged/Uncommitted Changes (git pull)
11.1.15 There Is No Tracking Information for the Current Branch (git pull)
11.1.16 Your Local Changes Would Be Overwritten (git merge, git pull)
11.1.17 Failed to Push Some Refs to <somerepo.git> (git push)
11.1.18 The Current Branch <name> Has No Upstream Branch (git push)
11.1.19 Merge Failed, Merge Conflict in <file> (git merge, etc.)
11.2 Saving Empty Directories
11.3 Merge for a Single File
11.3.1 git merge-file
11.3.2 git checkout
11.4 Deleting Files Permanently from Git
11.4.1 Local Changes Only, without Push (git rm)
11.4.2 Previously Uploaded Changes, with Push (git filter-branch)
11.4.3 Previously Uploaded Changes, after Push (git filter-repo)
11.4.4 Previously Uploaded Changes, after Push (BFG Repo Cleaner)
11.5 Splitting a Project
11.6 Moving Commits to a Different Branch
11.6.1 git reset
11.6.2 git cherrypick
12 Command Reference
12.1 The git Command
12.1.1 Porcelain versus Plumbing
12.1.2 General Options
12.1.3 git add
12.1.4 git bisect
12.1.5 git blame
12.1.6 git branch
12.1.7 git checkout
12.1.8 git cherry-pick
12.1.9 git clean
12.1.10 git clone
12.1.11 git commit
12.1.12 git config
12.1.13 git diff
12.1.14 git fetch
12.1.15 git gc
12.1.16 git gui
12.1.17 git grep
12.1.18 git init
12.1.19 git log
12.1.20 git ls-files
12.1.21 git merge
12.1.22 git merge-base
12.1.23 git merge-file
12.1.24 git mergetool
12.1.25 git mv
12.1.26 git pull
12.1.27 git push
12.1.28 git rebase
12.1.29 git reflog
12.1.30 git remote
12.1.31 git reset
12.1.32 git restore
12.1.33 git rev-list
12.1.34 git revert
12.1.35 git rm
12.1.36 git shortlog
12.1.37 git show
12.1.38 git stage
12.1.39 git status
12.1.40 git submodule
12.1.41 git subtree
12.1.42 git switch
12.1.43 git tag
12.2 Revision Syntax
12.2.1 Commit Ranges (rev1..rev2 versus rev1…rev2)
12.3 git Configuration
12.3.1 Configuration File .git/config
12.3.2 Basic Settings
12.3.3 Configuration File .gitignore
12.3.4 Configuration File .gitmodules
12.3.5 Configuration File .gitattributes
The Authors
Index
Service Pages
Legal Notes
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
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.
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.
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.