概述
Git已成为业界广泛使用的版本控制系统,分支管理更是日常开发的核心环节。本文将深入探讨如何删除Git分支,包括本地和远程场景的操作细节。
准备Git仓库
为便于演示分支删除操作,我们先准备一个测试仓库。首先从GitHub克隆测试仓库:
$ git clone [email protected]:sk1418/myRepo.git
Cloning into 'myRepo'...
...
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
进入仓库目录查看分支:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
当前仓库仅有一个master
分支(默认分支)。接下来创建测试分支,演示本地和远程分支的删除操作。本文聚焦命令行操作方式。
删除本地分支
使用-d选项删除本地分支
创建测试分支:
$ git checkout -b feature
Switched to a new branch 'feature'
尝试删除feature
分支:
$ git branch -d feature
error: Cannot delete branch 'feature' checked out at '/tmp/test/myRepo'
踩坑了!这是因为当前正位于feature
分支:
$ git branch
* feature
master
Git不允许删除当前检出的分支。切换到master
分支后重试:
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -d feature
Deleted branch feature (was 3aac499)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
成功删除本地feature
分支。
使用-D选项删除本地分支
重新创建feature
分支并提交更改:
$ git checkout -b feature
Switched to a new branch 'feature'
# 修改README.md文件
$ echo "new feature" >> README.md
$ git status
On branch feature
Changes not staged for commit:
...
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git ci -am'add "feature" to the readme'
[feature 4a87db9] add "feature" to the readme
1 file changed, 1 insertion(+)
此时再用-d
选项删除会报错:
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -d feature
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
因为待删除分支(feature)领先默认分支(master):
$ git log --graph --abbrev-commit
* commit 4a87db9 (HEAD -> feature)
| Author: ...
| Date: ...|
| add "feature" to the readme
|
* commit 3aac499 (origin/master, origin/HEAD, master)
| Author: ...
| Date: ...|
| the first commit
|
* commit e1ccb56
Author: ...
Date: ...
Initial commit
两种解决方案:
- 先合并分支再删除
- 直接强制删除(使用
-D
选项):
$ git branch -D feature
Deleted branch feature (was 4a87db9)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master.
git branch -d/-D不会删除远程分支
关键点:无论使用-d
还是-D
,都仅删除本地分支,不会影响远程分支。通过示例说明:
创建feature
分支并推送到远程:
$ git checkout -b feature
Switched to a new branch 'feature'
# 添加新文件
$ echo "a wonderful new file" > wonderful.txt
$ git add . && git ci -am'add wonderful.txt'
[feature 2dd012d] add wonderful.txt
1 file changed, 1 insertion(+)
create mode 100644 wonderful.txt
$ git push
...
To github.com:sk1418/myRepo.git
* [new branch] feature -> feature
此时本地分支跟踪远程分支:
$ git remote show origin | grep feature
feature tracked
feature pushes to feature (up to date)
强制删除本地分支(未合并状态):
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -D feature
Deleted branch feature (was 2dd012d).
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/feature
remotes/origin/master
本地feature
分支消失,但remotes/origin/feature
仍存在。重新检出分支时,历史记录完整保留:
$ git checkout feature
Switched to branch 'feature'
Your branch is up to date with 'origin/feature'.
$ cat wonderful.txt
a wonderful new file
删除远程分支
Git 1.7.0+版本推荐使用:
$ git push origin -d <branchName>
旧版(<1.7.0)使用:
$ git push origin :<branchName>
继续前文示例,删除远程feature
分支。先创建本地跟踪分支:
$ git checkout feature
branch 'feature' set up to track 'origin/feature'.
Switched to a new branch 'feature'
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/feature
remotes/origin/master
删除远程分支:
$ git push origin -d feature
To github.com:sk1418/myRepo.git
- [deleted] feature
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
重要发现:
- 删除远程分支不影响本地跟踪分支
- 可在任意本地分支上删除远程分支(示例中就在
feature
分支上操作了)
总结
Git分支删除操作要点:
本地分支删除
$ git branch -d <branchName> # 安全删除(需合并) $ git branch -D <branchName> # 强制删除
远程分支删除
$ git push origin -d <branchName> # 推荐方式 $ git push origin :<branchName> # 兼容旧版
核心原则
- 本地/远程分支删除互不影响
- 删除远程分支时,本地跟踪分支保留
- 可在任意本地分支上删除远程分支