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'
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
% 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
Related Questions
- How to remove a Git Local Branch?
- How to remove a Git Remote Branch?
- Github - How to delete a branch?
Related Articles:
- GitHub Doc - Creating and deleting branches within your repository
- GitHub Docs - Deleting and restoring branches in a pull request
- Git Branch Documentation
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- Fix Power BI error Access to the resource is forbidden when connecting SharePoint Online List as data source - SharePoint
- How to reset Eclipse IDE Code Font - Eclipse
- Java 8 Supplier Functional Interface Examples - Java
- Fix: error: non-static type variable T cannot be referenced from a static context - Java
- How to turn off Stage Manager - macOS Ventura - MacOS
- 🎃 Halloween Hashtag 2022 on Social Media 🎃 - Hashtags
- 25: How to rename a file using Python Program - Python-Programs
- Java Stream Word Count Example - Java