在命令行中使用 Git 创建分支与合并
通过操作分支拓展 Git 基础:创建、切换、合并分支,解决冲突,并遵循整洁的功能分支工作流。
在命令行中使用 Git 创建分支与合并 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Branches?
You know how to commit and track files. Branches let you develop features in isolation without disturbing the main line of work, then merge them back when ready.
Listing Branches
git branch lists branches and marks the current one with an asterisk.
git branchCreating and Switching
git switch -c name creates a branch and moves to it in one step. (Older Git uses git checkout -b.)
git switch -c feature-loginCommitting on a Branch
Commits you make now belong only to the feature branch, leaving main untouched.
git add .
git commit -m 'Add login form'Switching Back
Return to main with git switch main. Your feature work is safely stored on its own branch.
git switch mainMerging a Branch
From main, git merge brings the feature commits in. A fast-forward happens when main has no new commits.
git merge feature-loginHandling Merge Conflicts
If the same lines changed on both branches, Git pauses and marks conflicts with <<<<<<< markers. Edit the file to resolve, then stage and commit.
git add resolved-file.txt
git commitDeleting Merged Branches
After merging, clean up with git branch -d. Git refuses if the branch is not merged, protecting your work.
git branch -d feature-loginComparing Branches
git diff between branches shows exactly what differs before you merge.
git diff main..feature-loginPushing a Branch
Share a branch by pushing it and setting upstream tracking with -u.
git push -u origin feature-loginA Clean Workflow
The feature-branch loop: branch from main, commit, push, open a pull request, merge, delete the branch. This keeps history organized and reviewable.
Quick Check
Which single command both creates a new branch and switches to it?
Recap
You can now manage parallel work:
git switch -ccreates and moves to a branchgit mergeintegrates; conflicts are resolved by editing then committinggit branch -dcleans up merged branchesgit push -u originshares them
Branch per feature, merge when done.
常见问题解答
「在命令行中使用 Git 创建分支与合并」课时是免费的吗?
是的 — 「在命令行中使用 Git 创建分支与合并」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「在命令行中使用 Git 创建分支与合并」这节课中我会学到什么?
通过操作分支拓展 Git 基础:创建、切换、合并分支,解决冲突,并遵循整洁的功能分支工作流。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「在命令行中使用 Git 创建分支与合并」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 从命令行学习 Git 基础
- 环境变量与别名
- Shell 配置:`.bashrc`、`.zshrc`、`.profile`
- 在命令行中使用 Git 创建分支与合并