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
- /etc/gitconfig file:
- ~/.gitconfig or ~/.config/git/config file:
- 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
Option | Description of Output |
%H | Commit hash |
%h | Abbreviated commit hash |
%T | Tree hash |
%t | Abbreviated tree hash |
%P | Parent hashes |
%p | Abbreviated parent hashes |
%an | Author name |
%ae | Author email |
%ad | Author date (format respects the --date=option) |
%ar | Author date, relative |
%cn | Committer name |
%ce | Committer email |
%cd | Committer date |
%cr | Committer date, relative |
%s | Subject |
Table 2. Common options to git log
Option | Description |
-p | Show the patch introduced with each commit. |
--stat | Show statistics for files modified in each commit. |
--shortstat | Display only the changed/insertions/deletions line from the --stat command. |
--name-only | Show the list of files modified after the commit information. |
--name-status | Show the list of files affected with added/modified/deleted information as well. |
--abbrev-commit | Show only the first few characters of the SHA-1 checksum instead of all 40. |
--relative-date | Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format. |
--graph | Display an ASCII graph of the branch and merge history beside the log output. |
--pretty | Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format). |
--oneline | Shorthand for --pretty=oneline --abbrev-commit used together. |
Limiting Log Output
Table 3. Options to limit the output of git log
Option | Description |
-<n> | Show only the last n commits |
--since, --after | Limit the commits to those made after the specified date. |
--until, --before | Limit the commits to those made before the specified date. |
--author | Only show commits in which the author entry matches the specified string. |
--committer | Only show commits in which the committer entry matches the specified string. |
--grep | Only show commits with a commit message containing the string |
-S | Only 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 的委婉說法
Symbols
Symbol | Name | Code |
\ | back slash | |
/ | forward slash | |
! | exclamation point | |
* | asterisk | |
[] | brackets | |
? | question mark | |
- | hyphen | |
-- | double dashes | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
navi
縮寫
VCSs | Version Control Systems | 版本控制系統 |
SVN | Subversion | |
RCS | Revision Control System | 修訂控制系統 |
CVCSs | Centralized Version Control Systems | 集中式版本控制系統 |
DVCSs | Distributed 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