1. Git Introduction
Git is 目 before 最流行 distributedversion控制system, 用于跟踪file 变化, 协调 many 人Development, 以及managementcode historyversion. VS Code in 置了 for Git 强 big support, 让您可以 in 编辑器in直接for big 部分Gitoperation.
1.1 Git basicconcepts
- 仓library (Repository) : storecode and versionhistory 地方
- submitting (Commit) : 将更改保存 to 本地仓library operation
- branch (Branch) : 用于parallelDevelopment不同functions code线
- merge (Merge) : 将一个branch 更改merge to 另一个branch
- 远程仓library (Remote) : store in network on Git仓library, 用于 many 人协作
- push (Push) : 将本地submittingpush to 远程仓library
- pull (Pull) : from 远程仓librarypull最 new 更改
- clone (Clone) : from 远程仓librarycopy一个本地仓library
2. VS Code Git 界面
VS Codeproviding了直观 Git界面, 您可以through以 under 方式访问:
- 点击 left 侧活动栏in sourcescodemanagementgraph标 (branch形状)
- using fast 捷键
Ctrl+Shift+G(Windows/Linux) orCmd+Shift+G(macOS)
2.1 Git 侧edge栏组成
- 更改 (Changes) : 显示当 before 工作区 更改, including已stage and 未stage 更改
- stage 更改 (Staged Changes) : 已添加 to stage区 更改, 准备submitting
- 未stage 更改 (Changes) : 尚未添加 to stage区 更改
- merge更改 (Merge Changes) : merge时产生 conflict
- branch (Branches) : 显示当 before branch and 可用branchlist
- submittinghistory (Commit History) : 显示submitting记录
3. Git basicoperation
3.1 初始化仓library
such as果您 project还没 has Git仓library, 可以 in VS Codein初始化:
- 打开要初始化 projectfile夹
- 点击 left 侧活动栏in sourcescodemanagementgraph标
- 点击"初始化仓library"按钮
3.2 clone远程仓library
from 远程仓libraryclone一个本地仓library:
- in VS Codein, 点击 left under 角 "打开file夹"按钮
- 选择"clone仓library"选项
- 输入远程仓libraryURL (such asGitHub仓library地址)
- 选择本地保存位置
- 点击"clone"按钮
3.3 stage更改
in submitting更改之 before , 需要将更改stage to stage区:
- stage单个file: 点击file right 侧 "+"按钮
- stage所 has 更改: 点击"+"按钮 (位于"更改"标题 right 侧)
- revertstage: 点击已stagefile right 侧 "-"按钮
3.4 submitting更改
将stage 更改submitting to 本地仓library:
- in "message"输入框in输入submittinginformation
- 点击"√"按钮 or 按
Ctrl+Enter(Windows/Linux) orCmd+Enter(macOS) - or 者usingcommands面板:
Git: Commit
提示
submittinginformation应该简洁明了, describes您做了what更改, 遵循"动词+名词" 格式, 例such as: "修复loginfunctionsbug" or "添加userregister页面".
3.5 push and pull
and 远程仓librarysynchronization更改:
- push (Push) : 将本地submittingpush to 远程仓library
- 点击 left under 角 branch名称, 选择"push"选项
- or usingcommands面板:
Git: Push - or using fast 捷键:
Ctrl+Shift+P>Git: Push
- pull (Pull) : from 远程仓librarypull最 new 更改
- 点击 left under 角 branch名称, 选择"pull"选项
- or usingcommands面板:
Git: Pull - or using fast 捷键:
Ctrl+Shift+P>Git: Pull
- push and pull: 同时执行pull and pushoperation
- 点击 left under 角 branch名称, 选择"push and pull"选项
3.6 查看submittinghistory
查看project submittinghistory:
- in Git侧edge栏in, 点击"更 many operation"按钮 (三个点)
- 选择"查看submittinghistory"
- or usingcommands面板:
Git: View History
in submittinghistory视graphin, 您可以:
- 查看submitting 详细information
- 比较不同submitting之间 diff
- 查看specificfile history更改
4. branchmanagement
branch is Git corefunctions, 用于parallelDevelopment不同 functions or 修复bug.
4.1 creation and 切换branch
- creationbranch:
- 点击 left under 角 branch名称
- 选择"creation new branch"选项
- 输入 new branch名称
- 点击"creation"按钮
- 切换branch:
- 点击 left under 角 branch名称
- from listin选择要切换 branch
- usingcommands面板:
- creationbranch:
Git: Create Branch - 切换branch:
Git: Checkout to...
- creationbranch:
4.2 mergebranch
将一个branch 更改merge to 当 before branch:
- 切换 to 要merge to 目标branch (通常 is 主branch)
- 点击 left under 角 branch名称
- 选择"mergebranch"选项
- 选择要merge sourcesbranch
- 点击"merge"按钮
4.3 解决mergeconflict
当mergebranch时, such as果两个branch for 同一file 同一部分for了不同 更改, 就会产生conflict. VS Codeproviding了直观 conflict解决界面:
- in Git侧edge栏 "merge更改"部分, 点击conflict file
- VS Code会显示conflict in 容, 分 for "当 before 更改" and "传入更改"
- using编辑器 on 方 选项解决conflict:
- 接受当 before 更改: 保留当 before branch 更改
- 接受传入更改: 保留要merge branch 更改
- 接受两者: 保留两者 更改
- 比较更改: 查看详细 diff
- 解决所 has conflict after , 保存file
- 将解决conflict after filestage
- completionmergesubmitting
4.4 deletebranch
delete不再需要 branch:
- 点击 left under 角 branch名称
- 选择"managementbranch"选项
- 找 to 要delete branch, 点击 right 侧 deletegraph标
- 确认delete
5. Git advancedoperation
5.1 revert更改
revert工作区in 更改:
- revert单个file 更改: right 键点击file, 选择"revert更改"
- revert所 has 更改: 点击"更改"标题 right 侧 "revert所 has 更改"按钮
5.2 查看diff
查看file 更改diff:
- 查看未stage 更改: 点击file名称, VS Code会显示更改diff
- 查看已stage 更改: 点击"stage 更改"in file
- 查看 and specificsubmitting diff: in submittinghistoryin, right 键点击submitting, 选择" and 当 before branch比较"
5.3 resetsubmitting
resetsubmitting可以将当 before branch指向不同 submitting:
- in submittinghistoryin, right 键点击要reset to submitting
- 选择"resetbranch to 此次submitting"选项
- 选择resetclass型:
- 软reset (Soft) : 保留更改 in stage区
- 混合reset (Mixed) : 保留更改 in 工作区
- 硬reset (Hard) : discard所 has 更改
warning
硬reset会永久discard未submitting 更改, using时要特别 small 心!
5.4 stage (Stash)
stage可以保存当 before 工作区 更改, 以便切换 to otherbranch or 执行otheroperation:
- stage更改:
- in Git侧edge栏in, 点击"更 many operation"按钮
- 选择"stage更改"选项
- 输入stage名称 (可选)
- applicationstage:
- in Git侧edge栏in, 点击"更 many operation"按钮
- 选择"managementstage"选项
- 找 to 要application stage, 点击"application"按钮
6. Git scale推荐
除了VS Code in 置 Gitfunctions, 您还可以installationscale来增强Git体验:
- GitLens — Git supercharged: providing更强 big Gitfunctions, such ascode作者information, submittinghistory, branch比较etc.
- Git History: 增强 Githistory查看functions
- GitHub Pull Requests and Issues: 直接 in VS CodeinmanagementGitHub PR and Issues
- Git Graph: 以graph形方式查看Gitsubmittinghistory and branchrelationships
- Gitignore Generator: 生成.gitignorefile
7. Git configuration
in usingGit之 before , 您需要configuration您 user名 and 邮箱, 这些information会出现 in 您 submitting记录in:
# 设置全局user名
git config --global user.name "Your Name"
# 设置全局邮箱
git config --global user.email "your.email@example.com"
# 查看configuration
git config --list
7.2 VS Code Git 设置
您可以 in VS Code设置in调整Git相关选项:
- Git: Enable: 启用 or 禁用Gitfunctions
- Git: Auto Fetch: 自动 from 远程仓library获取更改
- Git: Commit On Close: 关闭submitting输入框时自动submitting
- Git: Show Branch Status Bar: in status栏显示当 before branch
- Git: Merge Editor: 启用 or 禁用merge编辑器
8. 练习
练习 1: Git basicoperation
- 初始化一个 new Git仓library
- creation几个file并添加一些 in 容
- stage这些file
- submitting更改, 输入 has 意义 submittinginformation
- 查看submittinghistory
练习 2: branchmanagement
- from 主branchcreation一个 new branch
- in new branchinmodifyfile
- submitting更改
- 切换回主branch
- merge new branch to 主branch
练习 3: 解决mergeconflict
- from 主branchcreation两个 new branch
- in 每个branchinmodify同一个file 同一行
- 分别submitting两个branch 更改
- 将第一个branchmerge to 主branch
- 尝试将第二个branchmerge to 主branch, 观察conflict
- 解决conflict并completionmerge
练习 4: 远程仓libraryoperation
- in GitHub on creation一个 new 远程仓library
- 将本地仓library and 远程仓library关联
- push本地submitting to 远程仓library
- in GitHub on modifyfile
- from 远程仓librarypull更改 to 本地