Git分支命令

git 分支常用命令清单,包括git branch git switch git merge

1 git-branch

1.1 查询

# 列出本地仓库分支
git branch
git branch -l
git branch --list

# 列出远程仓库分支
git branch -r
git branch --remotes
git branch -lr
git branch --list --remotes

# 列出所有分支
git branch -a
git branch --all

# 查询上游分支(远程分支),verbose是“详细的“意思
git branch -vv
git branch --verbose --verbose

1.2 创建与删除

# 创建新分支
git branch <new-branch>

# 创建新分支并追踪远程分支(上游分支)
git branch --track <new-local-branch> <remote-branch>

# 删除分支
git branch -d <branch>
git branch --delete <branch>

# 强制删除分支
git branch -D <branch>
git branch --delete --force <branch>

# 删除远程分支追踪
git branch -dr <remote/branch>
git branch --delete --remote <remote/branch>
# 删除远程分支
git push -d <remote> <branch>
git push --delete <remote> <branch>

1.3 移动(重命名)与复制

# 移动(重命名)当前分支
git branch -m [current-branch] <new-branch>
git branch --move [current-branch] <newbranch>

# 移动(重命名)指定分支
git branch -m <target-branch> <new-branch>
git branch --move <target-branch> <new-branch>

# 复制当前分支
git branch -c <new-branch> [current-branch]
git branch --copy <new-branch> [current-branch]

1.4 其他

# 当前分支追踪远程分支(上游分支)
git branch -u <remote-branch>
git branch --set-upstream-to=<remote-branch>

2 git-switch

# 切换到分支
git switch <target-branch>

# 切换到新分支(会创建新分支)
git switch -c <new-branch>
git switch --create <new-branch>

# 切换到新孤立分支(没有父提交,空分支)
git switch --orphan <new-branch>

3 git-merge

# 合并指定分支到当前分支
git merge <target-branch>

参考资料

更新日志

  • 20241229:初稿

  • 20241217:创建