0Pricing
Linux Command Line Mastery · 课时

在命令行中使用 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 branch

Creating 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-login

Committing 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 main

Merging a Branch

From main, git merge brings the feature commits in. A fast-forward happens when main has no new commits.

git merge feature-login

Handling 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 commit

Deleting 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-login

Comparing Branches

git diff between branches shows exactly what differs before you merge.

git diff main..feature-login

Pushing a Branch

Share a branch by pushing it and setting upstream tracking with -u.

git push -u origin feature-login

A 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 -c creates and moves to a branch
  • git merge integrates; conflicts are resolved by editing then committing
  • git branch -d cleans up merged branches
  • git push -u origin shares 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 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从命令行学习 Git 基础
  2. 环境变量与别名
  3. Shell 配置:`.bashrc`、`.zshrc`、`.profile`
  4. 在命令行中使用 Git 创建分支与合并
← 返回 Linux Command Line Mastery