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.
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.
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.
In Git, local and remote branches are separate objects. Deleting a local branch doesn't remove the remote branch.
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.
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.
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:
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
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 currentgit 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With