How do I delete a Git branch locally and remotely?

How do I delete a Git branch locally and remotely?

Failed Attempts to Delete a Remote Branch:

$ git branch -d remotes/origin/bugfixerror: branch 'remotes/origin/bugfix' not found.$ git branch -d origin/bugfixerror: branch 'origin/bugfix' not found.$ git branch -rd origin/bugfixDeleted remote branch origin/bugfix (was 2a14ef7).$ git pushEverything up-to-date$ git pullFrom github.com:gituser/gitproject* [new branch] bugfix -> origin/bugfixAlready up-to-date.

How do I properly delete the remotes/origin/bugfix branch both locally and remotely?


Executive Summary

git push -d <remote_name> <branchname> # Delete remotegit branch -d <branchname> # Delete local

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 serverfix branch from the server, you run the following:

$ git push origin :serverfixTo git@github.com: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 ran 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).

Fetch changes

Finally, execute the following on other machines to propagate changes:

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

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 {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 -d {remote_branch}.

Reference: Git: Delete a branch (local or remote).


Git is a powerful version control system that allows developers to manage and track changes to their code. One common task is managing branches, which are essentially independent lines of development. Over time, some branches become obsolete, and it's important to know how to properly delete them, both locally on your machine and remotely on the server. This ensures a clean and manageable repository, reducing clutter and potential confusion. Knowing how to confidently remove branches is a crucial skill for any developer working with Git, contributing to a more organized and efficient workflow. By mastering these techniques, you can keep your repositories streamlined and focus on the active, relevant code.

Safely Deleting a Git Branch Locally

Deleting a branch locally in Git is a straightforward process, but it's important to understand the implications. When you delete a local branch, you're removing the pointer to that specific line of development from your local repository. This doesn't affect the remote repository or any other developer's local copy until you explicitly push the deletion. It's generally a good practice to ensure that you've merged the branch's changes into another branch (typically main or develop) before deleting it. This prevents accidental data loss and ensures that all your work is preserved. Regularly cleaning up local branches helps to keep your workspace tidy and reduces the chance of accidentally working on an outdated branch.

Here’s how you can delete a branch locally:

  1. First, make sure you are not on the branch you intend to delete. Switch to another branch, such as main:
    git checkout main
  2. Then, use the -d flag (for "delete") if the branch has been merged, or the -D flag (for "delete forcefully") if it hasn't:
    git branch -d branch_name

    or

    git branch -D branch_name
  3. If the branch has not been merged and you use -d, Git will prevent you from deleting it, unless you use -D.

Forcefully Removing a Local Branch

Sometimes, you might need to delete a local branch that hasn't been fully merged. This could be because the changes in the branch are no longer needed, or you want to start fresh. Using the -D flag allows you to bypass Git's safety check and forcefully delete the branch. However, exercise caution when using this option, as it can lead to data loss if the changes in the branch are important and haven't been backed up or merged elsewhere. Always double-check that you no longer need the code in the branch before using the force delete option. It's also a good idea to communicate with your team members before deleting any branches, especially if they might be relying on them.

To forcefully delete a local branch, use the following command:

git branch -D branch_name

For example:

git branch -D feature/my-new-feature

This command immediately removes the feature/my-new-feature branch from your local repository, regardless of whether it has been merged.

Deleting a Git Branch Remotely

Deleting a branch remotely removes it from the central repository that your team collaborates on. This action impacts all team members who are connected to the remote repository, so it's crucial to communicate before removing any remote branches. Removing a remote branch helps maintain a clean and organized central repository, preventing confusion and ensuring everyone is working with the most up-to-date and relevant code. It's also important to ensure that the branch is no longer needed before deleting it, as other developers might be relying on it. Coordination and communication are key when deleting remote branches to avoid disrupting the team's workflow.

Here are a few ways to delete a branch remotely:

  1. Using git push with the --delete flag:
    git push origin --delete branch_name
  2. Using git push with a colon before the branch name:
    git push origin :branch_name

Both commands achieve the same result: they remove the specified branch from the remote repository named origin.

Here's a summary table comparing local and remote branch deletion:

Action Command Impact
Delete Local Branch (Merged) git branch -d branch_name Removes the branch locally if merged.
Delete Local Branch (Forcefully) git branch -D branch_name Removes the branch locally, even if not merged (use with caution).
Delete Remote Branch git push origin --delete branch_name Removes the branch from the remote repository.
Delete Remote Branch (Alternative) git push origin :branch_name Removes the branch from the remote repository.

Before deleting any branch, consider if you might need to revert any changes. How do I undo the most recent local commits in Git?

"Deleting branches is a necessary part of Git workflow, but it should be done with care and consideration to avoid data loss and disruption."

Deleting branches is a common and necessary task when using Git for version control. Knowing how to manage branches effectively, both locally and remotely, ensures a clean and efficient workflow. Whether you’re cleaning up old feature branches or removing obsolete release branches, understanding the commands and their implications is crucial. Remember to always communicate with your team before deleting remote branches to avoid any unexpected issues. And always double-check that you have backed up or merged any important changes before deleting a branch, especially when using the force delete option. Use these techniques with caution and consideration, and you’ll maintain a well-organized and manageable Git repository. To further enhance your Git skills, explore resources like the official Git documentation. Don't forget to regularly review and clean up your branches to keep your project tidy and efficient.


Delete a Git Branch Locally & Remotely

Delete a Git Branch Locally & Remotely from Youtube.com

Previous Post Next Post

Formulario de contacto