git-branch what does - How do I delete a Git branch both locally and remotely?





15 Answers

Matthew's answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:

To remove a local branch from your machine:

git branch -d {the_local_branch} (use -D instead to force deleting the branch without checking merged status)

To remove a remote branch from the server:

git push origin --delete {the_remote_branch}

Reference: https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote

the command 'git

I want to delete a branch both locally and on my remote project fork on GitHub.

Failed Attempts to Delete Remote Branch

$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.

$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.

$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push
Everything up-to-date

$ git pull
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?




Steps for deleting a branch:

for deleting the remote branch:

git push origin --delete <your_branch> 

for deleting the local branch, you have three ways:

1: git branch -D <branch_name> 

2: git branch --delete --force <branch_name>  //same as -D

3: git branch --delete  <branch_name>         //error on unmerge

Explain: OK, just explain what's going on here!

Simply do git push origin --delete to delete your remote branch ONLY, add the name of the branch at the end and this will delete and push it to remote at the same time...

Also, git branch -D, which simply delete the local branch ONLY!...

-D stands for --delete --force which will delete the branch even it's not merged(force delete), but you can also use -d which stands for --delete which throw an error respective of the branch merge status...

I also create the image below to show the steps:




If you want to delete a branch, first checkout to the branch other than the branch to be deleted.

git checkout other_than_branch_to_be_deleted

Deleting the local branch:

git branch -D branch_to_be_deleted

Deleting the remote branch:

git push origin --delete branch_to_be_deleted



git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>



Another approach is

git push --prune origin

WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,

git push --mirror

will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).




Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.

Relevant blog post: Create and delete branches




To delete your branch locally and remotely

  • Checkout to master branch - git checkout master

  • Delete your remote branch - git push origin --delete <branch-name>

  • Delete your local branch - git branch --delete <branch-name>




You can also do this using git remote prune origin:

$ git remote prune origin
Pruning origin
URL: git@example.com/yourrepo.git
 * [pruned] origin/some-branchs

It prunes and deletes remote-tracking branches from a git branch -r listing.




Deleting Branches

Let's assume our work on branch "contact-form" is done and we've already integrated it into "master". Since we don't need it anymore, we can delete it (locally):

$ git branch -d contact-form

And for deleting the remote branch:

git push origin --delete contact-form



Delete remote branch

git push origin :<branchname>

Delete local branch

git branch -D <branchname>

Delete local branch steps:

  1. checkout to another branch
  2. delete local branch



Now you can do it with the GitHub Desktop app.

After launching the app

  1. Click on the project containing the branch
  2. Switch to the branch you would like to delete
  3. From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.
  4. From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (aka the machine you are currently working on)



git push origin --delete branchName

is easier to remember than

git push origin :branchName



I got sick of googling for this answer, so I took a similar approach to the answer that crizCraig posted earlier.

Added the following to my Bash profile:

function gitdelete(){
    git push origin --delete $1
    git branch -D $1
}

Then every time I'm done with a branch (merged into master, for example) I run the following in my terminal:

gitdelete my-branch-name

...which then deletes my-branch-name from origin as as well as locally.




git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

If you are sure you want to delete it, run

git branch -D bugfix

Now to clean up deleted remote branches run

git remote prune origin



Mashup of all the other answers. Requires Ruby 1.9.3+, tested only on OS X.

Call this file git-remove, make it executable, and put it in your path. Then use, for example, git remove temp.

#!/usr/bin/env ruby
require 'io/console'

if __FILE__ == $0
      branch_name = ARGV[0] if (ARGV[0])
      print "Press Y to force delete local and remote branch #{branch_name}..."
    response = STDIN.getch
    if ['Y', 'y', 'yes'].include?(response)
      puts "\nContinuing."
      `git branch -D #{branch_name}`
      `git branch -D -r origin/#{branch_name}`
      `git push origin --delete #{branch_name}` 
    else
      puts "\nQuitting."
    end
end



Related