Pro Git
Second Edition
Scott Chacon, Ben Straub
Version 2.1.223, 2020-04-21

Front Matter

Licence
Preface by Scott Chacon
Preface by Ben Straub
Dedications
Contributors
Introduction

Vocabularies

ubiquity [juˋbɪkwətɪ] <n> 到處存在,無所不在
incredible [ɪnˋkrɛdəb!] <a> 難以置信的
explosion [ɪkˋsploʒən] <n> 爆發;爆炸
frontier [frʌnˋtɪr] <n> 國境,邊境
swath [swɑθ] <n>(鐮刀或割草機)收割的寬度;划幅
barely [ˋbɛrlɪ] <a> 僅僅,勉強;幾乎沒有
conceivable [kənˋsivəb!] <a> 可想像的;可理解的
dedication [͵dɛdəˋkeʃən] <n> 獻辭,題辭
spontaneously [spɔnˋteniəslɪ] <a> 自然地;自發地
combust [kəmˈbʌst] <vi> 燒掉
wield [wild] <vt> 揮舞(劍等)
ponder [ˋpɑndɚ] <vt> 仔細考慮;衡量
deftly [ˋdɛftlɪ] <a> 靈巧地;熟練地
juggle [ˋdʒʌg!] <vi> 玩雜耍;變戲法
cope [kop] <vt> 妥善處理
plunge [plʌndʒ] <n> 投入
delve [dɛlv] <vi> 探究,鑽研
murky [ˋmɝkɪ] <a> 黑暗的;陰鬱的

Getting Started

About Version Control

Local Version Control Systems
Centralized Version Control Systems
Distributed Version Control Systems

A Short History of Git

What is Git?

Snapshots, Not Differences
Nearly Every Operation Is Local
Git Has Integrity
Git Generally Only Adds Data
The Three States

The Command Line

Installing Git

Installing on Linux
Installing on macOS
Installing on Windows
Installing from Source

First-Time Git Setup

  1. /etc/gitconfig file:
  2. ~/.gitconfig or ~/.config/git/config file:
  3. config file
Your Identity
Your Editor
Checking Your Settings

Getting Help


Summary

Vocabularies

overhead [ˋovɚˋhɛd] n. 經常性開支
centralize [ˋsɛntrəl͵aɪz] vt.使集中於中央
subversion [səbˋvɝʃən] n. 顛覆;覆滅
downside [ˋdaʊnˋsaɪd] n.不利之處
fiery [ˋfaɪərɪ] adj. 激烈的,熱烈的
controversy [ˋkɑntrə͵vɝsɪ] n.爭論,辯論
fairly [ˋfɛrlɪ] adv. 頗為,相當地
miniature [ˋmɪnɪətʃɚ] n. 縮影;縮小模型
latency [ˋletnsɪ] n. 潛伏;潛在因素
overhead [ˋovɚˋhɛd] n. 經常費用
unworldly [ʌnˋwɝldlɪ] adj. 超脫世俗的
integrity [ɪnˋtɛgrətɪ] n.完善;健全
checksum [ˋtʃɛksʌm] v. 依據封包內容計算出校驗值
parlance [ˋpɑrləns] n. 專門用語
sane [sen] adj. 神志正常的;健全的
suffice [səˋfaɪs] vi. 足以..., 足夠...
trump [trʌmp] vt. 勝過,贏牌

Git Basics

Getting a Git Repository
Initializing a Repository in an Existing Directory
Cloning an Existing Repository
Recording Changes to the Repository
Checking the Status of Your Files
Tracking New Files
Staging Modified Files
Short Status
Ignoring Files
.gitignore
github-gitignore
Viewing Your Staged and Unstaged Changes
Committing Your Changes
Skipping the Staging Area
Removing Files
Viewing the Commit History
Table 1. Useful options for git log --pretty=format
OptionDescription of Output
%HCommit hash
%hAbbreviated commit hash
%TTree hash
%tAbbreviated tree hash
%PParent hashes
%pAbbreviated parent hashes
%anAuthor name
%aeAuthor email
%adAuthor date (format respects the --date=option)
%arAuthor date, relative
%cnCommitter name
%ceCommitter email
%cdCommitter date
%crCommitter date, relative
%sSubject
Table 2. Common options to git log
OptionDescription
-pShow the patch introduced with each commit.
--statShow statistics for files modified in each commit.
--shortstatDisplay only the changed/insertions/deletions line from the --stat command.
--name-onlyShow the list of files modified after the commit information.
--name-statusShow the list of files affected with added/modified/deleted information as well.
--abbrev-commitShow only the first few characters of the SHA-1 checksum instead of all 40.
--relative-dateDisplay the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.
--graphDisplay an ASCII graph of the branch and merge history beside the log output.
--prettyShow commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).
--onelineShorthand for --pretty=oneline --abbrev-commit used together.
Limiting Log Output
Table 3. Options to limit the output of git log
OptionDescription
-<n>Show only the last n commits
--since, --afterLimit the commits to those made after the specified date.
--until, --beforeLimit the commits to those made before the specified date.
--authorOnly show commits in which the author entry matches the specified string.
--committerOnly show commits in which the committer entry matches the specified string.
--grepOnly show commits with a commit message containing the string
-SOnly show commits adding or removing code matching the string
Undoing Things


the pros and cons [] ph.贊成和反對的理由
bona fide [] ph.【拉】真實的;有誠意的
stage [stedʒ] vt.把……搬上舞臺,上演
diverge [daɪˋvɝdʒ] vi.分叉;叉開
heck [hɛk] int.(表示詛咒、惱怒、厭煩等,hell的委婉語)真見鬼,在感歎句中加強語氣
wordy [ˋwɝdɪ] adj.嘮叨的;冗長的
tilde [ˋtɪldə] n. ~ 符號
glob [glɑb] n.水珠;(一團)東西
negate [nɪˋget] vt.否定;取消;使無效
vague [veg] adj.不明確的;含糊的
synonym [ˋsɪnə͵nɪm] n.同義字
craft [kræft] vt.精巧地製作
chronological [͵krɑnəˋlɑdʒɪk!] adj.依時間前後排列而記載的
stats [stæts] abbr.= statistics
distinction [dɪˋstɪŋkʃən] n.區別;差別
colloquially [kəˋlokwɪəlɪ] adv.用口語體;用通俗語
pickaxe [ˋpɪkˋæks] n. 鶴嘴鋤
amend [əˋmɛnd] vt. 修訂,修改
darn [dɑrn] vt.縫補;damn 的委婉說法
	
Git Basics
  Getting a Git Repository
  Recording Changes to the Repository
  Viewing the Commit History
  Undoing Things
  Working with Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
  Tagging . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 54
  Git Aliases . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 59
  Summary . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 61
	
Janas 2023/11/25

Symbols

SymbolNameCode
\back slash
/forward slash
!exclamation point
*asterisk
[]brackets
?question mark
-hyphen
--double dashes
navi

縮寫

VCSsVersion Control Systems版本控制系統
SVNSubversion
RCSRevision Control System修訂控制系統
CVCSsCentralized Version Control Systems集中式版本控制系統
DVCSsDistributed Version Control Systems分散式版本控制系統


Table of Contents

Git Branching. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
  Branches in a Nutshell . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . 62
  Basic Branching and Merging . . . . . . . . . . . . . . . . . . . . .. . . . .  . . . . . . . . . . . . . . . . 69
  Branch Management. . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . 77
  Branching Workflows . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . .. . . . . . 78
  Remote Branches . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .  . . . . . . . . . 82
  Rebasing . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . .. . .  . . . . . . . . . . . . . . . . . . 92
  Summary . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . .  . . . . . .. . . . . . . . . . . . . . . 101
Git on the Server. . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . .  . . . . . . . . . . . . . . 102
  The Protocols . . . . . . . . . . . . . . . . . .  . . . . . . . . .  . .  . . . . . . . . . . . . . . . . . . 102
  Getting Git on a Server . . . . . . . . . . . . .  . . .. . . . . . ..  . . .  . . . . . . . . . . . . . . . . 107
  Generating Your SSH Public Key . . . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . . . . . . . . . 109
  Setting Up the Server . . . . . . . . . . . . . . . . .  . . . . . .  . .  . . . . . . . . . . . . . . . . . . 110
  Git Daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . . . . . . . . . . . . . . . . . . . 113
  Smart HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   . . . . . . . . . . . . . . . . . . . . 114
  GitWeb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   . . . . . . . . . . . . . . . . . . . . 116
  GitLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
  Third Party Hosted Options. . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 122
  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 123
Distributed Git. . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 124
  Distributed Workflows . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . 124
  Contributing to a Project . . . . . . . . . . . .  . . . . . . . . .   . . . . . . . . . . . . . . . . . . . . 127
  Maintaining a Project . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
  Summary . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
GitHub. . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . 165
  Account Setup and Configuration . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 165
  Contributing to a Project . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 170
  Maintaining a Project . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . 190
  Managing an organization. . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
  Scripting GitHub . . . . . . . . . . . . . . . . .. . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 208
  Summary . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Git Tools . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
  Revision Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
  Interactive Staging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
  Stashing and Cleaning . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . 230
  Signing Your Work. . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 236
  Searching . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
  Rewriting History. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
  Reset Demystified . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . 252
  Advanced Merging. . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . 272
  Rerere . . . . . . . . . . . . . . . . . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 291
  Debugging with Git . . . . . . . . . . . . . .. . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 297
  Submodules. . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 300
  Bundling. . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
  Replace . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
  Credential Storage . . . . . . . . . . . . . .. . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 334
  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 339
Customizing Git. . . . . . . . . . . . . . . . .. . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . 340
  Git Configuration . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 340
  Git Attributes. . . . . . . . . . . . . . .. .. . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . 350
  Git Hooks . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
  An Example Git-Enforced Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
  Summary . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Git and Other Systems. . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 371
  Git as a Client . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . 371
  Migrating to Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..  . .  . . . . . . . . . . . . 436
Git Internals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   . . . . . . . . . . . 437
  Plumbing and Porcelain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . 437
  Git Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 438
  Git References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 448
  Packfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . 452
  The Refspec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 455
  Transfer Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
  Maintenance and Data Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
  Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 470
  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . 476
Appendix A: Git in Other Environments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
  Graphical Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
  Git in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 482
  Git in Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . 484
  Git in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
  Git in IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
  Git in Sublime Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . 486
  Git in Bash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
  Git in Zsh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
  Git in PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . 491
Appendix B: Embedding Git in your Applications . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 492
  Command-line Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
  Libgit2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
  JGit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
  go-git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . 501
  Dulwich . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . 502
Appendix C: Git Commands . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 504
  Setup and Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . 504
  Getting and Creating Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
  Basic Snapshotting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
  Branching and Merging . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
  Sharing and Updating Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 511
  Inspection and Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
  Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
  Patching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . 515
  Email. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
  External Systems . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 517
  Administration . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . 517
  Plumbing Commands . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
  Index . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
  
Git 常用指令