Git 分支管理是版本控制系统中的一个重要概念,它允许开发者在不同的开发线上进行工作,而不会影响主分支的稳定性。以下是一些基本的 Git 分支管理命令和概念:

  1. 创建分支:

bash git branch <branch-name>

这会创建一个新分支,但不会切换到该分支。

  1. 切换分支:

bash git checkout <branch-name>

或者使用 switch 命令(在 Git 2.23 版本之后引入):

bash git switch <branch-name>

  1. 创建并切换分支:

bash git checkout -b <branch-name>

或者使用 switch 命令:

bash git switch -c <branch-name>

  1. 查看所有分支:

bash git branch

或者更详细的分支列表:

bash git branch -a

  1. 合并分支:

bash git merge <branch-name>

这会将指定分支的更改合并到当前分支。

  1. 删除分支:

bash git branch -d <branch-name>

对于已经合并的分支,可以使用 -D 强制删除:

bash git branch -D <branch-name>

  1. 推送分支到远程仓库:

bash git push origin <branch-name>

  1. 拉取远程分支到本地:

bash git fetch origin <branch-name> git checkout -b <branch-name> origin/<branch-name>

  1. 设置上游分支:

bash git branch --set-upstream-to=origin/<branch-name>

  1. 解决合并冲突: 当合并时出现冲突,需要手动解决冲突,然后提交更改:

    bash git mergetool

  2. 查看分支的合并图:

    bash git log --graph --oneline --decorate --all

  3. 重置分支到某个提交:

    bash git reset --hard <commit-hash>

  4. 检出文件到特定版本:

    bash git checkout <branch-name> -- <file-path>

  5. 比较分支差异:

    bash git diff <branch-name>

  6. 创建临时分支:

    bash git worktree add -b temp-branch <path> <commit-hash>

这些是 Git 分支管理的一些基本操作,实际使用中可能会根据项目的具体需求进行调整。记得在进行分支操作之前,确保你的工作已经提交,以避免丢失更改。