1. Overview
1.概述
Git has been widely used as the version control system in the industry. Further, Git branches are a part of our everyday development process.
Git已经作为版本控制系统在业界被广泛使用。此外,Git分支是我们日常开发过程中的一部分。
In this tutorial, we’ll explore how to delete Git branches.
在本教程中,我们将探讨如何删除 Git 分支。
2. Preparation of the Git Repository
2.准备Git存储库
To easier address how to delete a Git branch, let’s first prepare a Git repository as an example.
为了更容易解决如何删除一个 Git 分支的问题,我们先准备一个 Git 仓库作为例子。
First, let’s clone the myRepo repository (https://github.com/sk1418/myRepo) from GitHub for testing:
首先,让我们从GitHub克隆myRepo仓库(https://github.com/sk1418/myRepo)进行测试。
$ git clone git@github.com:sk1418/myRepo.git
Cloning into 'myRepo'...
...
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done
Second, let’s enter the local myRepo directory and check the branches:
第二,让我们进入本地myRepo目录并检查分支。
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
As we can see from the output above, currently, we have only one master branch in the myRepo repository. Also, the master branch is myRepo‘s default branch.
从上面的输出可以看出,目前,我们在myRepo版本库中只有一个master分支。而且,master分支是myRepo的默认分支。
Next, let’s create some branches and show how to delete a branch locally and remotely. In this tutorial, we’ll focus on deleting branches in the command line.
接下来,让我们创建一些分支,并展示如何在本地和远程删除一个分支。在本教程中,我们将重点介绍在命令行中删除分支。
3. Deleting a Local Branch
3.删除一个本地分支
Let’s first have a look at deleting a local branch.
让我们先看看如何删除一个本地分支。
Git’s git branch command has two options for deleting a local branch: -d and -D.
Git的git branch命令有两个选项用于删除一个本地分支。-d和-D。
Next, let’s take a closer look at them and understand the difference between these two options through an example.
接下来,让我们仔细看看它们,通过一个例子了解这两个选项之间的区别。
3.1. Deleting a Local Branch With the -d Option
3.1.用-d选项删除一个本地分支
First, let’s try to create a local branch:
首先,让我们试着创建一个本地分支。
$ git checkout -b feature
Switched to a new branch 'feature'
Next, let’s delete the feature branch using the -d option:
接下来,让我们用-d选项删除feature分支。
$ git branch -d feature
error: Cannot delete branch 'feature' checked out at '/tmp/test/myRepo'
Oops, as we can see, we’ve got an error message. This is because we’re currently on the feature branch:
哎呀,我们可以看到,我们得到了一个错误信息。这是因为我们目前是在feature分支上。
$ git branch
* feature
master
In other words, we cannot delete a currently checked-out branch. So, let’s switch to the master branch and fire the command again:
换句话说,我们不能删除当前已签出的分支。因此,让我们切换到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
As we can see, we’ve deleted the local feature branch successfully.
我们可以看到,我们已经成功删除了本地feature分支。
3.2. Deleting a Local Branch With the -D Option
3.2.用-D选项删除一个本地分支
First, let’s create the feature branch again. But this time, we’re going to make some changes and commit it:
首先,让我们再次创建feature分支。但这一次,我们要做一些修改并提交。
$ git checkout -b feature
Switched to a new branch 'feature'
# ... modify the README.md file ...
$ 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(+)
Now, Git will refuse to delete the feature branch if we still use the -d option:
现在,如果我们仍然使用-d选项,Git会拒绝删除feature分支。
$ 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'.
This is because the to-be-deleted branch (feature) is ahead of the default branch (master):
这是因为待删除的分支(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
There are two ways to solve the problem. First, we can merge the feature branch into master and then execute “git branch -d feature” again.
有两种方法来解决这个问题。首先,我们可以将feature分支合并到master,然后再次执行”git branch -d feature“。
However, if we want to discard the unmerged commits, as the error message suggested, we can run “git branch -D feature” to execute a force deletion:
然而,如果我们想放弃未合并的提交,正如错误信息所建议的,我们可以运行”git branch -D feature“来执行强制删除:
$ git branch -D feature
Deleted branch feature (was 4a87db9)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master.
3.3. git branch -d/-D Won’t Delete the Remote Branch
3.3.git branch -d/-D 不能删除远程分支
So far, we’ve deleted a local branch using git branch with the -d and -D options. It’s worth mentioning that no matter whether we delete with -d or -D, this command will only remove the local branch. No remote branch will be deleted, even if the deleted local branch is tracking a remote branch.
到目前为止,我们已经用git branch和-d选项,删除了一个本地分支。值得一提的是,无论我们用-d还是-D删除,这个命令都只会删除本地分支。不会删除远程分支,即使被删除的本地分支正在跟踪一个远程分支。
Next, let’s understand this through an example. Again, let’s create a feature branch, make some changes, and push the commit to the remote repository:
接下来,让我们通过一个例子来理解这个问题。同样,让我们创建一个feature分支,做一些修改,然后把提交推送到远程仓库。
$ git checkout -b feature
Switched to a new branch 'feature'
# add a new file
$ 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
As the output above shows, we’ve created a new file, wonderful.txt, on the feature branch and pushed the commit to the remote repository.
如上面的输出所示,我们在feature分支上创建了一个新文件wonderful.txt,并将提交推送到远程仓库。
Therefore, the local feature branch is tracking the remote feature branch:
因此,本地feature分支正在跟踪远程feature分支。
$ git remote show origin | grep feature
feature tracked
feature pushes to feature (up to date)
Since we haven’t merged feature into master, let’s delete the local feature branch with the -D option:
由于我们还没有将feature 合并到master,让我们用-D选项删除本地feature分支。
$ 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
As we can see in the output of the command git branch -a, the local feature branch is gone. But the /remotes/origin/feature branch is not removed.
正如我们在 git branch -a 命令的输出中所看到的,本地的 feature 分支已经消失。但/remotes/origin/feature分支没有被删除。
Now, if we check out the feature branch again, the changes we’ve made are still there:
现在,如果我们再次查看feature分支,我们所做的修改仍然在那里。
$ git checkout feature
Switched to branch 'feature'
Your branch is up to date with 'origin/feature'.
$ cat wonderful.txt
a wonderful new file
Next, let’s see how to remove a remote branch.
接下来,让我们看看如何删除一个远程分支。
4. Deleting a Remote Branch
4.删除一个远程分支
We can use the command git push origin :<branchName> to remove a remote branch if our Git version is before 1.7.0. However, this command doesn’t look like a deletion operation. Therefore, since version 1.7.0, Git has introduced the git push origin -d <branchName> command to delete a remote branch. Apparently, this command is easier to understand and memorize than the older version.
如果我们的 Git 版本在 1.7.0 之前,我们可以使用 git push origin :<branchName> 命令来删除一个远程分支。然而,这个命令看起来并不像一个删除操作。因此,从1.7.0版本开始,Git引入了git push origin -d <branchName>命令来删除一个远程分支。显然,这个命令比旧版本更容易理解和记忆。
Just now, we’ve deleted the local branch feature, and we’ve seen that the remote feature branch is still there. So now, let’s use the mentioned command to remove the remote feature branch.
刚才,我们已经删除了本地分支feature,我们看到远程feature分支还在。所以现在,让我们用上述命令来删除远程feature分支。
Before we delete the remote feature, let’s first create a local feature branch to track the remote one. This is because we want to check if removing a remote branch will impact the tracking of local branches:
在我们删除远程feature之前,让我们先创建一个本地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
So, now we have the local and remote feature branches. Further, we’re currently on the local feature branch.
所以,现在我们有了本地和远程feature分支。此外,我们目前在本地feature分支上。
Next, let’s remove the remote feature branch:
接下来,让我们删除远程feature分支。
$ 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
As we can see, after we execute the git push -d feature command, the remote feature branch has been deleted. However, the local feature branch is still there. That is to say, deleting a remote branch won’t impact the local tracking branches. Therefore, if we launch git push now, the local feature branch will be pushed to remote again.
我们可以看到,在我们执行git push -d feature命令后,远程feature分支已经被删除。然而,本地的feature分支还在。也就是说,删除一个远程分支不会影响本地的跟踪分支。因此,如果我们现在启动git push,本地feature分支将再次被推送到远程。
Moreover, unlike the local branch deletion, we can delete a remote branch no matter which local branch we’re currently working on. In the example above, we’re on the local feature branch, but we can still remove the remote feature branch without any problem.
此外,与本地分支的删除不同,我们可以删除一个远程分支,无论我们目前在哪个本地分支上工作。在上面的例子中,我们在本地的feature分支上,但我们仍然可以毫无问题地删除远程的feature分支。
5. Conclusion
5.总结
In this article, we’ve explored how to delete Git’s local and remote branches using commands.
在本文中,我们探讨了如何使用命令删除 Git 的本地和远程分支。
Let’s summarize them quickly:
让我们快速总结一下。
- Delete a local branch: git branch -d/-D <branchName> (the -D option is for force deletion)
- Delete a remote branch: git push origin -d <branchName> or git push origin :<branchName>
Also, we’ve understood that removing a branch on a local or remote will not impact the branches on the other side.
另外,我们已经了解到,删除本地或远程的一个分支不会影响到另一边的分支。