Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a Git branch locally and remotely?

I want to delete a branch both locally and remotely.

Failed Attempts to Delete a 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 should I do differently to successfully delete the remotes/origin/bugfix branch both locally and remotely?

like image 708
Matthew Rankin Avatar asked Jan 05 '10 01:01

Matthew Rankin


People also ask

How do I delete a local branch remotely?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

Can you delete a branch in a local and in a remote repository?

You'll often need to delete a branch not only locally but also remotely. To do that, you use the following command: git push <remote_name> --delete <branch_name>. The branch still exists locally, though.

How do I delete a local branch in git?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

Does deleting local branch delete remote branch?

In Git, local and remote branches are separate objects. Deleting a local branch doesn't remove the remote branch.


2 Answers

Executive Summary

$ git push -d <remote_name> <branchname> $ git branch -d <branchname> 

Note: In most cases, <remote_name> will be origin.

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d <branch_name> $ git branch -D <branch_name> 
  • The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  • The -D option is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]
  • As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  • You will receive an error if you try to delete the currently selected branch.

Delete Remote Branch

As of Git v1.7.0, you can delete a remote branch using

$ git push <remote_name> --delete <branch_name> 

which might be easier to remember than

$ git push <remote_name> :<branch_name> 

which was added in Git v1.5.0 "to delete a remote branch or a tag."

Starting with Git v2.8.0, you can also use git push with the -d option as an alias for --delete. Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.

Delete Remote Branch [Original Answer from 5-Jan-2010]

From Chapter 3 of Pro Git by Scott Chacon:

Deleting Remote Branches

Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s main branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:

$ git push origin :serverfix To [email protected]:schacon/simplegit.git  - [deleted]         serverfix 

Boom. No more branches on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”

I issued git push origin: bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).

Then you should execute this on other machines

# Fetch changes from all remotes and locally delete  # remote deleted branches/tags etc # --prune will do the job :-; git fetch --all --prune 

to propagate changes.

like image 170
Matthew Rankin Avatar answered Oct 04 '22 01:10

Matthew Rankin


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: Git: Delete a branch (local or remote)

like image 42
Eric Brotto Avatar answered Oct 04 '22 01:10

Eric Brotto