分支 Workflow Simulation
你将构建什么
仓库哪里你 创建 feature 分支, 添加更改 on 它, 切换后面到 主要, 合并分支, and 删除它. 完整分支 lifecycle on toy 项目.
学习目标
- 创建 and 切换分支使用 git checkout -b
- 制作提交 on 分支不使用 affecting 主要
- 合并分支后面到 主要使用 git 合并
- 删除合并分支到 keep 仓库 clean
前置要求
- git init, 添加, 提交 (创建仓库 Workflow)
- 理解那个分支是 independent 行的 工作
步骤
- 1
设置主要使用基础提交
创建仓库 and 制作 one 提交 on 默认主要分支. 这个是 stable 基础那个你的 feature 分支将 构建 on.
代码mkdir branch-sim cd branch-sim git init echo "# Branch Sim" > README.md git add . git commit -m "Initial commit"预期结果
仓库 on 主要使用 one 提交.
- 2
创建 and 切换到 feature 分支
运行 git checkout -b 添加-greeting 到创建新 分支 and 切换到 它 in one 步骤. 提交制作这里将 不 affect 主要直到你 合并.
代码git checkout -b add-greeting预期结果
Git prints "Switched to a new branch 'add-greeting'".
提示
git 分支 shows 所有分支; current one 是 marked 使用 *.
- 3
制作提交 on feature 分支
添加新 file and 提交它 on feature 分支. 因为你 是 on 添加-greeting, 主要是 untouched — 这个是 整个点 的 branching.
代码echo "print('hi from feature branch')" > greet.py git add greet.py git commit -m "Add greeting script on feature branch"预期结果
提交 exists on 添加-greeting; 主要 still 有仅 初始提交.
- 4
切换后面到 主要 and 合并
运行 git checkout 主要到 返回到 主要分支, 然后 git 合并添加-greeting 到 bring feature 提交 in. 之后合并, 主要有 相同 files as 分支.
代码git checkout main git merge add-greeting预期结果
主要现在包含 greet.py; git log shows 两者都提交.
- 5
删除合并分支
运行 git 分支 -d 添加-greeting 到删除分支. -d flag 仅删除 fully 合并分支, 所以它 是 safe — 否工作是 lost.
代码git branch -d add-greeting git branch git log --oneline预期结果
仅主要 remains; log shows 两者都提交 on 主要.
自己动手试试
# TODO: set up repo, create branch, commit, merge back, delete branch
mkdir branch-sim
cd branch-sim
git initmkdir branch-sim
cd branch-sim
git init
echo "# Branch Sim" > README.md
git add .
git commit -m "Initial commit"
git checkout -b add-greeting
echo "print('hi from feature branch')" > greet.py
git add greet.py
git commit -m "Add greeting script on feature branch"
git checkout main
git merge add-greeting
git branch -d add-greeting
git branch
git log --oneline你学到了什么
你 simulated 完整 feature-分支 lifecycle — 创建, 提交, 合并, 删除 — 哪个是 workflow 每个 team uses 到 develop features 不使用 destabilizing 主要分支.
相关资源
相关课程
相关练习
- 分支 Workflow Predict
Predict 哪个分支提交 lands on 之后序列的 commands.
- Git Commands MCQ
测验 on checkout, 合并, and 分支 -d behavior.
相关工具
- Code Diff
Compare 分支更改之前 and 之后合并.
- .gitignore Generator
Keep generated files out 的你的分支提交.
相关速查表
- Git Cheatsheet
引用 for 分支 and 合并 command 语法.
相关参考文档
- Git Commands 引用
引用 for checkout, 合并, and 分支 -d 语法.
相关错误
- 合并 Conflict
Resolving conflicting 更改.
- Detached Head
工作 in detached HEAD 状态.
- 分支做 不 Exist
Handling missing 分支 names.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
相关错误
用大白话解释常见错误——什么意思、为什么发生、如何修复。
- 合并 Conflict
Resolving conflicting 更改.
- Detached Head
工作 in detached HEAD 状态.
- 分支做 不 Exist
Handling missing 分支 names.