创建新分支

GitGitBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在 Git 中,分支是指向提交对象的轻量级可移动指针。创建新分支可让你在不影响主代码库的情况下处理新功能或修复漏洞。在本实验中,你将学习如何在 Git 中创建新分支。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12714{{"`创建新分支`"}} end

创建新分支

对于本实验,请将名为 https://github.com/labex-labs/git-playground 的 Git 仓库复刻到你的 GitHub 账户中。你正在名为 https://github.com/your-username/git-playground 的 Git 仓库中处理一个项目。你需要创建一个名为 feature-1 的新分支来处理一项新功能。

  1. 克隆仓库,导航到该目录并配置身份信息:
git clone https://github.com/your-username/git-playground.git
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. 检查当前分支:
git branch
  1. 创建一个名为 feature-1 的新分支:
git checkout -b feature-1
  1. 验证你现在是否位于 feature-1 分支上:
git branch
  1. 将更改推送到远程仓库:
git push -u origin feature-1

这是你运行 git branch -r 命令时的输出结果:

git branch remote output

总结

在 Git 中创建新分支是一个简单的过程,它允许你在不影响主代码库的情况下处理新功能或修复漏洞。使用 git checkout -b <分支> 命令来创建具有指定名称的新分支并切换到该分支。你可以选择添加 -t <远程>/<分支> 来为新创建的分支设置远程跟踪分支。

您可能感兴趣的其他 Git 教程