Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I safely delete a remote git branch?

To delete a local branch in git I use git branch -d, but how do I safely remove a remote branch?

I would like to delete it only when the remote branch is merged to my current branch.

like image 436
Sławosz Avatar asked Feb 01 '12 09:02

Sławosz


People also ask

Is it safe to delete a remote branch in git?

Branches can be safely removed without risk of losing any changes. Consider a scenario in which a branch patch-1 is about to be merged with the master branch through a pull request. Before the merge, master and patch-1 both point to separate commits in git's commit history.

How do you clean a remote branch?

In order to clean up remote-tracking branches while fetching, use the “git fetch” command with the “–prune” option. Alternatively, you can simply use the “-p” shortcut instead of typing “prune” every time.

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.

How to delete a local branch in Git?

Local branches are branches on your local machine and do not affect any remote branches. The command to delete a local branch in Git is: git branch is the command to delete a branch locally. -d is a flag, an option to the command, and it's an alias for --delete.

When should I use the delete command in Git?

Use it only when you are absolutely sure you want to delete a local branch. If you didn't merge it into another local branch or push it to a remote branch in the codebase, you will risk losing any changes you've made. Remote branches are separate from local branches. They are repositories hosted on a remote server that can be accessed there.

What is a remote branch in Git?

Remote branches are separate from local branches. They are repositories hosted on a remote server that can be accessed there. This is in comparisson to local branches, which are repositories on your local system. The command to delete a remote branch is:

How to find all branches that are already merged in Git?

While that post copes with local branches, you could find remote branches that are merged or not using git branch -r --mergedto detect all remote branches that are already merged into the current git branch -r --unmergedto do the opposite


2 Answers

The answer is partly covered here: How can I know in git if a branch has been already merged into master?

While that post copes with local branches, you could find remote branches that are merged or not using

  • git branch -r --merged to detect all remote branches that are already merged into the current
  • git branch -r --unmerged to do the opposite

  • git branch -r --no-merged is correct for the new version of Git and I'm not sure whether git branch -r --unmerged is applicable for old git.

Once you found that a specific remote branch is already merged (i.e. it appears when typing git branch -r --merged), you could delete it as Michael Krelin answers using

git push <remote> :<remotebranchname>

See also the documentation of git branch for the --merged and --unmerged flags.

like image 107
eckes Avatar answered Oct 23 '22 06:10

eckes


Just to point out that for unmerged branches it seems the option now is --no-merged as explained on http://git-scm.com/docs/git-branch

like image 35
Carlos Avatar answered Oct 23 '22 05:10

Carlos