Languages
[Edit]
EN

Git - delete local branch

6 points
Created by:
JustMike
3584

In this short article we would like to show how to remove local branch in git.

Run following command:

git branch -d my_branch_name

or

git branch --delete my_branch_name

Example output:

john@ubuntu-pc:/home/john/Projects/shop$ git branch -d Task-43-user-login-ui
Deleted branch Task-43-user-login-ui (was 3968585).

Note: do not forget to checkout to other branch from my_branch_name - below example shows how to do it.

Removing with force example

The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream-to.

Alternatively we can run delete with force, parameter -D

# -D - Shortcut for --delete --force.
git branch -D my_branch_name

Removing active branch example

This section shows how to remove branch that we are currently using.

Run:

git checkout master
git branch -d my_branch_name

Example output:

john@ubuntu-pc:/home/john/Projects/shop$ git branch -d Task-43-user-login-ui
error: Cannot delete branch 'Task-43-user-login-ui' checked out at '/home/john/Projects/shop'

john@ubuntu-pc:/home/john/Projects/shop$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

john@ubuntu-pc:/home/john/Projects/shop$ git branch -d Task-43-user-login-ui
Deleted branch Task-43-user-login-ui (was 3968585).

john@ubuntu-pc:/home/john/Projects/shop$ 

 

Hey 👋
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.