Recent Posts

Showing posts with label remote. Show all posts
Showing posts with label remote. Show all posts

Delete a remote branch GIT

Intro: 3 Branches to Delete

When you're dealing with deleting branches both locally and remotely, keep in mind that there are 3 different branches involved:

  1. The local branch X.
  2. The remote origin branch X.
  3. The local remote-tracking branch origin/X that tracks the remote branch X.


The original poster used

git branch -rd origin/bugfix
which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.

To delete that actual remote branch, you need

git push origin --delete bugfix
The following sections are the detailed steps needed to actually delete these various branches.

Deleting the local branch X

git branch --delete X

# Or shorter
git branch -d X

# If X hasn't been merged yet, you'll need to force delete
git branch -D X

Deleting the remote branch X

As Matthew Rankin points out in his answer, you can use either of the following to delete the remote branch (depending on what version of Git you're using):

# Use an empty refspec to delete the remote branch.
# This will work in versions of Git older than 1.7.0.
git push origin :X

# If you're using Git version 1.7.0 or newer
# you can use the new --delete flag instead.
git push origin --delete X
Note that deleting the remote branch X from the command line this way will also delete the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p, though it wouldn't hurt if you did it anyway.

You can verify that the remote-tracking branch origin/X was also deleted by running the following:

# View just remote-tracking branches
git branch --remotes
git branch -r

# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a

Pruning the obsolete local remote-tracking branch origin/X

If you didn't delete your remote branch X from the command line (like above), then your local repo will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.

A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:

git fetch origin --prune

# Or shorter
git fetch origin -p
Here is the relevant quote from the 1.6.6 release notes (emphasis mine):

"git fetch" learned --all and --multipleoptions, to run fetch from many repositories, and --prune option to remove remote tracking branches that went stale. These make "git remote update" and "git remote prune" less necessary (there is no plan to remove "remote update" nor "remote prune", though).

Alternative to above automatic pruning for obsolete remote-tracking branches

Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p, you can avoid making the extra network operation by just manually removing the branch(es) with the --remote or -r flags:

git branch --delete --remotes origin/X

# Or shorter
git branch -dr origin/X

Create a remote branch GIT

First, you create your branch locally

git checkout -b your_branch
The remote branch is automatically created when you push it to the remote server. So when you feel for it, you can just do

git push <remote-name> <branch-name>
Where <remote-name> is typically origin, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name>
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!

So that a subsequent git pull will know what to do, you might instead want to use:

git push -u <remote-name> <branch-name>
As described below, the -u option sets up an upstream branch:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.