Deleting Branches
The command git branch -d
branch
removes the named branch from a
repository. Git prevents you from removing the current branch:
$ git branch -d bug/pr-3
error: Cannot delete the branch 'bug/pr-3' which you are currently on.
Removing the current branch would leave Git unable to determine what the resulting working directory tree should look like. Instead, you must always name a noncurrent branch.
But there is another subtle issue. Git wonât allow you to delete a branch that contains commits that are not also present on the current branch. That is, Git prevents you from accidentally removing development in commits that will be lost if the branch were to be deleted.
$git checkout master
Switched to branch "master" $git branch -d bug/pr-3
error: The branch 'bug/pr-3' is not an ancestor of your current HEAD. If you are sure you want to delete it, run 'git branch -D bug/pr-3'.
In this git show-branch output, the commit
âAdded a bug fix for pr-3â is found only on the
bug/pr-3
branch. If that branch were to be deleted,
there would no longer be a way to access that commit.
By stating that the bug/pr-3
branch is not an
ancestor of your current HEAD
, Git is telling you
that the line of development represented by the
bug/pr-3
branch does not contribute to the
development of the current branch, master
.
Git is not mandating that all branches be merged into the
master
branch before they can be deleted. Remember, a branch is simply a name or pointer to a commit that has actual ...
Get Version Control with Git now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.