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
# 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://firstname.lastname@example.org': To https://github.com/mygithubuser/myproj.git - [deleted] myproject/ticket-1
- How to remove a Git Local Branch?
- How to remove a Git Remote Branch?
- Github - How to delete a branch?
- GitHub Doc - Creating and deleting branches within your repository
- GitHub Docs - Deleting and restoring branches in a pull request
- Git Branch Documentation
Have Questions? Post them here!
- [Fixed] Git Clone email@example.com: Permission denied (publickey) Could not read from remote repository Fatal error
- Github: How to Invite Collaborators
- Git Commit - Author identity unknown, Please tell me who you are email
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- Github: fatal: Authentication failed Support for password was removed on August 13, 2021
- Get List of all local branches git command
- Git Revision Questions Before the Interview
- git fatal: Authentication failed error [fix]
- How to Install Git on Ubuntu Linux
- GitHub: How to Search Code
- How to change Git Default Author and Committer details in Eclipse
- How to check your installed version of Git
- Install GitHub Command Line Tool on Mac
- How to Merge Branch into Master Branch
- Perform an Empty Commit in Git without anything in Staging Area
- Git Fix: fatal: refusing to merge unrelated histories Error
- How to undo last Git Commit on Local Repository?
- git command to remove/unstage files from staging area
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial
- Remove git config at Local, Global or System Levels?
- Fix [oh-my-zsh] Cant update: not a git repository
- Git Config Command - A Deep Dive
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to change directory in Git bash
- Java JDBC Example with Oracle Database Driver Connection - Java
- How to Setup maven on Mac (macOS) - Mac-OS-X
- Mac Studio Display Buzzing sound from the Monitor - Mac-OS-X
- How to Scan iPhone for Virus? Is Antivirus it really required? - HowTos
- Add Custom header and footer to Windows Notepad file - NotepadPlusPlus
- Bootstrap Nav Menu Dropdown on hover - Bootstrap
- Run IntelliJ Java main method without Gradle build - Gradle