Git 分支管理是版本控制系统中的一个重要概念,它允许开发者在不同的开发线上进行工作,而不会影响主分支的稳定性。以下是一些基本的 Git 分支管理命令和概念:
- 创建分支:
bash
git branch <branch-name>
这会创建一个新分支,但不会切换到该分支。
- 切换分支:
bash
git checkout <branch-name>
或者使用 switch 命令(在 Git 2.23 版本之后引入):
bash
git switch <branch-name>
- 创建并切换分支:
bash
git checkout -b <branch-name>
或者使用 switch 命令:
bash
git switch -c <branch-name>
- 查看所有分支:
bash
git branch
或者更详细的分支列表:
bash
git branch -a
- 合并分支:
bash
git merge <branch-name>
这会将指定分支的更改合并到当前分支。
- 删除分支:
bash
git branch -d <branch-name>
对于已经合并的分支,可以使用 -D 强制删除:
bash
git branch -D <branch-name>
- 推送分支到远程仓库:
bash
git push origin <branch-name>
- 拉取远程分支到本地:
bash
git fetch origin <branch-name>
git checkout -b <branch-name> origin/<branch-name>
- 设置上游分支:
bash
git branch --set-upstream-to=origin/<branch-name>
-
解决合并冲突: 当合并时出现冲突,需要手动解决冲突,然后提交更改:
bash git mergetool -
查看分支的合并图:
bash git log --graph --oneline --decorate --all -
重置分支到某个提交:
bash git reset --hard <commit-hash> -
检出文件到特定版本:
bash git checkout <branch-name> -- <file-path> -
比较分支差异:
bash git diff <branch-name> -
创建临时分支:
bash git worktree add -b temp-branch <path> <commit-hash>
这些是 Git 分支管理的一些基本操作,实际使用中可能会根据项目的具体需求进行调整。记得在进行分支操作之前,确保你的工作已经提交,以避免丢失更改。