What is used for "git branch -d"
Today I help employees and found that git branch -D
and git branch -D
do a few different things. From the git reference documentation:
-d --delete delete fully merged branch
-D delete branch (even if not merged)
I can figure out that arbitrary branch deletion can be avoided, but how does git determine when it is branch -d
valid? When would anyone use it correctly branch -d
?
source to share
Think of it -D
as a forced deletion of a branch. It will delete the branch even if it has not been merged with the branch you are on.
-D
however, will warn you and will not delete the branch until it is merged.
for example
You have split a branch master
into a branch A
. Made in A
. If you switched to the branch again master
and tried git branch -d A
, you would get a message like this
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 push into a branch A
that it master
doesn't have, and make sure you want to delete it before you commit those changes to the current branch.
source to share
When a branch is completely merged (i.e. all revisions of that branch are either pushed to it appropriately, or the branch is merged with master
), -d
sufficient is sufficient. From the docs:
Delete the branch. A branch should be fully merged in its upstream or HEAD unless an upstream has been set using --track or --set-upstream.
If you did merge --squash
(i.e. not all revisions of that branch are in master
) or you haven't merged the branch anywhere because you admitted you were doing wrong and want to discard that branch -d
:
Delete the branch regardless of its merged state.
source to share
When is someone using the -d branch correctly?
This is done after the pull request has been merged into the main branch. The branch from which the delete request was made is good for deleting after a merge (assuming the branch is for a specific project only and the project is complete).
source to share