1. Git core concepts
Git has 几个core concepts, understanding这些concepts for 于MasterGit using非常 important . under 面我们将介绍Git 主要concepts:
1.1 仓library (Repository)
仓library is Git用来storeprojectcode and versionhistory 地方. Git仓library分 for 两种class型:
- 本地仓library: store in 本地计算机 on 仓library, package含project 完整history记录
- 远程仓library: store in 远程server on 仓library, 用于团队协作 and code共享
你可以through以 under commands初始化一个本地仓library:
git init
or 者 from 远程仓libraryclone一个仓library:
git clone https://github.com/user/repository.git
1.2 工作区 (Working Directory)
工作区 is 你 in 计算机 on 看 to projectTable of Contents, package含了你当 before 正 in 编辑 file. 当你modifyfile时, 这些modify首先发生 in 工作区.
工作区 is Gitmanagement 起点, 所 has modify都 from 这里开始.
1.3 stage区 (Staging Area)
stage区 is Gitin 一个特殊区域, 用于临时保存你 modify, 准备submitting to versionlibrary. stage区也被称 for index (Index) .
当你usinggit addcommands时, 你 is 将工作区 modify添加 to stage区. stage区允许你选择性地将modifysubmitting to versionlibrary, 而不 is submitting所 has modify.
1.4 versionlibrary (Repository)
versionlibrary is Git用来storeprojecthistoryversion 地方, package含了所 has submitting记录. versionlibrary位于工作区 .gitTable of Contentsin, package含了Git 所 has 元data and objectdatalibrary.
当你usinggit submittingcommands时, 你 is 将stage区 modifysubmitting to versionlibrary.
1.5 Gitobject
Gitusing四种主要 objectclass型来storedata:
- Blobobject: storefile in 容, 但不package含file名
- Treeobject: storeTable of Contentsstructure and file名, 指向Blobobject or otherTreeobject
- Commitobject: storesubmittinginformation, including作者, 日期, submittingmessage and 指向Treeobject 指针
- Tagobject: storetaginformation, 指向specific Commitobject
2. Git working principles
Git working principles可以用以 under 几个步骤来describes:
2.1 filestatus转换
in Gitin, file has 四种主要status:
- Untracked: file未被Git跟踪, 不 in versionlibraryin
- Modified: file已被modify, 但modify尚未添加 to stage区
- Staged: file已被modify, 且modify已添加 to stage区, 准备submitting
- Committed: file已被submitting to versionlibrary
filestatus转换graph:
Untracked → Modified → Staged → Committed
↓ ↑ ↑
└─────────────────┘ │
└──────────────────────────┘
2.2 Git 三个工作区域
Git has 三个主要 工作区域, 它们之间 relationshipssuch as under :
- 工作区: 你当 before 正 in 编辑 file
- stage区: 临时保存你 modify, 准备submitting
- versionlibrary: store所 has submitting记录
当你usingGitcommands时, 你practical on is in 这三个区域之间movefile or modify:
git add: 将工作区 modify添加 to stage区git submitting: 将stage区 modifysubmitting to versionlibrarygit checkout: 将versionlibrary filecheckout to 工作区
提示
understandingGit 三个工作区域 and filestatus转换 is MasterGit 关键. 建议花时间熟悉这些concepts, 这将 has 助于你更 good 地usingGitcommands.
3. Git submittingmodel
Gitusingsnapshot (Snapshot) model来storefile, 而不 is diff (Delta) model. 这意味着每次submitting时, Git会保存整个file snapshot, 而不 is 仅保存file 变化部分.
3.1 snapshotmodel vs diffmodel
传统 version控制system (such asSVN) usingdiffmodel, 只保存file 变化部分. 而Gitusingsnapshotmodel, 每次submitting时保存整个file snapshot.
snapshotmodel 优点including:
- 更 fast branch and mergeoperation
- 更 simple history记录management
- 更 good integrity and reliability
3.2 submittinggraph
Gitusing has 向无环graph (DAG) 来表示submittinghistory. 每个submitting都指向它 父submitting, 形成一个tree状structure.
你可以using以 under commands查看submittinggraph:
git log --graph --oneline --all
这将显示一个简化 submittinggraph, package含submitting哈希, submittinginformation and branchrelationships.
3.3 引用 (References)
Gitusing引用来指向submitting, including:
- branch引用: 指向当 before branch 最 new submitting
- 远程branch引用: 指向远程branch 最 new submitting
- tag引用: 指向specific submitting
- HEAD引用: 指向当 before 正 in using branch or submitting
你可以using以 under commands查看引用:
git branch # 查看本地branch git branch -r # 查看远程branch git tag # 查看tag git log --oneline HEAD~5..HEAD # 查看最近5个submitting
4. Git branchmodel
branch is Git 一个corefeatures, 允许你 in 不影响主线Development circumstances under forparallelDevelopment. Git branchmodel非常轻量级, creation and 切换branch 成本很 low .
4.1 branch 本质
in Gitin, branch本质 on is 指向submitting 指针. 当你creation一个branch时, Git只 is creation了一个指向当 before submitting new 指针. 当你 in branch on forsubmitting时, 该branch指针会自动 before 进 to new submitting.
你可以using以 under commandscreation and 切换branch:
git branch new-branch # creation new branch git checkout new-branch # 切换 to new branch # or 者using更简洁 commands git checkout -b new-branch # creation并切换 to new branch
4.2 branch 优势
Gitbranch 主要优势including:
- parallelDevelopment: many 个Development者可以同时 in 不同 branch on 工作
- 隔离functions: new functions可以 in 独立 branch on Development, 不会影响主线
- securitytest: 可以 in branch on fortest, 确保 stable after 再merge to 主线
- simple merge: branchmergeoperation simple high 效
4.3 HEAD指针
HEAD is Gitin 一个特殊指针, 指向你当 before 正 in using branch or submitting. 当你切换branch时, HEAD指针会update for 指向 new branch 最 new submitting.
你可以using以 under commands查看HEAD 指向:
git log --oneline -1 HEAD # 查看HEAD指向 submitting
5. 实践case: understandingGit workflow程
让我们through一个实践case来understandingGit workflow程:
- creation一个 new Git仓library:
- creation一个 new file并查看status:
- 将file添加 to stage区并查看status:
- submittingfile to versionlibrary并查看status:
- modifyfile并查看status:
- creation一个 new branch并切换 to 该branch:
- in new branch on creation一个 new file并submitting:
- 切换回主branch并查看file:
- 查看submittinggraph:
mkdir git-concepts git init git config user.name "Test User" git config user.email "test@example.com"
echo "Hello, Git!" > file1.txt git status
你会看 to file1.txt处于Untrackedstatus.
git add file1.txt git status
现 in file1.txt处于Stagedstatus.
git submitting -m "Add file1.txt" git status
现 in file1.txt处于Committedstatus, 工作区 is 干净 .
echo "Modified content" >> file1.txt git status
现 in file1.txt处于Modifiedstatus.
git checkout -b feature-branch
echo "New feature" > file2.txt git add file2.txt git submitting -m "Add file2.txt for new feature"
git checkout master ls
你会看 to 主branch on 只 has file1.txt, 没 has file2.txt, 因 for file2.txt is in feature-branch on creation .
git log --graph --oneline --all
这将显示一个 simple submittinggraph, package含masterbranch and feature-branchbranch.
实践练习
请按照以 under 步骤completion练习:
- creation一个 new Git仓library
- configurationGit user名 and 邮箱
- creation两个 new file: file1.txt and file2.txt
- 将file1.txt添加 to stage区
- submittingfile1.txt to versionlibrary
- modifyfile1.txt in 容
- 查看Gitstatus, 确认file1.txt处于Modifiedstatus
- 将file2.txt添加 to stage区
- 再次查看Gitstatus, 确认file1.txt处于Modifiedstatus, file2.txt处于Stagedstatus
- creation一个 new branchfeature
- 切换 to featurebranch
- in featurebranch on creation一个 new filefile3.txt并submitting
- 切换回主branch
- usinggit log --graph --oneline --allcommands查看submittinggraph