Reputation: 1696
I was helping out a co-worker today, and discovered that git branch -D
and git branch -d
do slightly different things. From the git help documentation:
-d --delete delete fully merged branch
-D delete branch (even if not merged)
I can appreciate avoiding deleting branching arbitrarily, but how does git determine when branch -d
is allowable? When would someone use branch -d
correctly?
Upvotes: 15
Views: 14955
Reputation: 19839
Think of -D
as a forced branch delete. It will delete the branch even if it has not been merged into the branch you're currently in.
-d
however, will warn you and won't delete the branch until its been merged.
For example
You've branched off master
branch into branch A
. Made commits into A
. If you then switched to master
branch again and attempted to git branch -d A
you'd get a message like so
git branch -d A
error: The branch 'A' is not fully merged.
If you are sure you want to delete it, run 'git branch -D A'.
This is because you have commits in A
branch that master
does not have and it's making sure you want to delete it before pulling those changes into the current branch.
Upvotes: 29
Reputation: 67197
When a branch is fully merged (i.e. all revisions of this branch are either pushed to it's corresponding
remote or the branch is merged to master
), -d
is sufficient. From docs:
Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream.
If you did a merge --squash
(i.e. not all revisions of this branch are contained in master
) or you didn't merge the branch anywhere because you recognized that you're doing wrong and want to throw this branch away, -D
is needed:
Delete a branch irrespective of its merged status.
Upvotes: 4
Reputation: 3055
When would someone use branch -d correctly?
This is done once a pull request has been merged into the main branch. The branch from which pull request was made is good to be deleted after the merge (assuming the branch is only for a specific project and the project is done).
Upvotes: 1