Git: Delete Branch Locally and Remotely at Origin


If you have a git branch at the remote origin and you want to completely delete this branch both from the local and the remote location, then you should follow the below git commands on the command line (or the terminal if using the Mac)

For demonstration, lets create a feature/local branch myproject/ticket-1

% git checkout -b "myproject/ticket-1"
Switched to a new branch 'myproject/ticket-1' 

Now let us push this branch to origin (remote git server, which is GitHub in our case),

% git push git push -u origin myproject/ticket-1
...
remote: 
To https://github.com/myuser/mynotes.git
 * [new branch]      myproject/ticket-1 -> myproject/ticket-1
branch 'myproject/ticket-1' set up to track 'origin/myproject/ticket-1'.

Git Delete Branch: Local

Options to delete Git Branch Locally

  • git branch -d local-branch-name
  • git branch -delete local-branch-name
  • git push origin -d -f local branch-name
  • git push origin --detete --force local branch-name


Example:
# git branch -d myproject/ticket-1
error: The branch 'myproject/ticket-1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D myproject/ticket-1'.
% git branch --delete --force myproject/ticket-1
Deleted branch myproject/ticket-1 (was 601a63a).

Make use of the option -d (delete) and -f (force) options to delete the local branch if not fully merged. You can also use -D instead of -d -f to ignore checking merged status.

✋️ Note: If you are on the feature branch and try to delete it you will get an error "commit delete branch", make sure you are on main/master or some other branch when you execute this command.

Example:
% git branch --delete --force myproject/ticket-1
error: Cannot delete branch 'myproject/ticket-1' checked out at '/myproj'
Example - Git Delete Local Branch

Example - Git Delete Local Branch

Git Delete Branch: Remote

To delete a git branch from the remote origin, you can push the branch with -d or --delete option,

Options to delete Git Branch from Remote (GitHub/BitBucket)

  • % git push origin --delete branch-name
  • % git push origin -d branch-name
Example:
% git push origin -d myproject/ticket-1
Username for 'https://github.com': mygithubuser
Password for 'https://mygithubuser@github.com': 
To https://github.com/mygithubuser/myproj.git
 - [deleted]         myproject/ticket-1
Git Delete Remote Branch

Example: Git - Delete Remote Branch

Related Questions

  • How to remove a Git Local Branch?
  • How to remove a Git Remote Branch?
  • Github - How to delete a branch?

Related Articles:


This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Ventura 13.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap