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?
The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.
We can use the branch in git for any reason we want. We create different branches for different teams working on the project (or the same module). Additionally, one can create them for any other feature you are creating in the project.
Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository. You always create a branch from an existing branch. Typically, you might create a new branch from the default branch of your repository.
Use Status Command Your branch is up to date with 'origin/master'. The first line shows which branch are you on. The second shows your upstream branch if there are any set.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With