11

Is it possible to delete a remote branch with hg-git?

I can delete the tag locally (hg bm -d old-branch), but it's not obvious how to tell the git server to do the same.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 3
    To do this in regular Git (not hg-git), the command is non-intuitive: `git push origin :branchToDelete`. The funny-looking thing with the colon is called a "refspec" and it specifies source and destination separated by a colon. So basically this command is saying "push 'nothing' onto branchToDelete", which effectively deletes it. I don't know if this is helpful in the hg-git case, but it might point you in the right direction.... – Ethan Brown Mar 20 '12 at 19:09
  • Thanks, but hg-git doesn't let you do that :( – David Wolever Mar 21 '12 at 01:10
  • 1
    Was a solution ever found for this? – cdeszaq Aug 01 '12 at 18:03
  • I'm pretty sure pushing a deleted bookmark with `hg bm -d old-branch; hg push -B old-branch` worked at one time, but it doesn't now and instead fails with `abort: revision 000000000000 cannot be pushed since it doesn't have a ref`. – Jason R. Coombs Feb 04 '15 at 09:27

2 Answers2

6

This was discussed on the Hg-Git mailing list on Sept. 13, 2012. The conclusion was that deleting remote Git branches is not something that Hg-Git currently supports, and the workaround is to use a separate Git client to delete the remote branch.

I've been working on improving Hg-Git's integration with bookmarks, so it's possible that this capability may be present in a future version of Hg-Git.

Related discussion: https://groups.google.com/d/topic/hg-git/Zj_-JkYsFaA/discussion

davidmc24
  • 2,232
  • 1
  • 19
  • 17
3

Here's an alias for your hgrc

If you're on a Posix system, add this alias to your hgrc file:

# Example invocation:
#    hg git-delete-remote default/bad-branch
gitremotedelete = ! \
    remote=`echo $HG_ARGS | sed 's_/.*__' | sed 's_.* __'`;
    branch=`echo $HG_ARGS | sed 's_.*/__'`;
    remote_url=$($HG paths $remote | sed 's_^git+ssh://__');
    git_dir=$($HG root)/.hg/git;
    # Delete the remote branch
    git --git-dir=$git_dir push $remote_url :$branch;
    # Delete local ref to remote branch
    git --git-dir=$git_dir branch -rd $remote/$branch;
    # Delete local branch
    git --git-dir=$git_dir branch -D $branch
    echo "Don't forget to run "'`'"hg bookmark -d $branch"'`'

And invoke it like so, if you want to delete the 'default/bad-branch' remotely:

# hg gitremotedelete <name of remote>/<branch to delete>
hg gitremotedelete default/bad-branch

Doing it manually

If you want to know what the above does: here's how we'd do the same thing as an interactive session. Hopefully this is translateable to Windows.

We want to delete the Git branch on some remote. Let's find out the url of the default remote; if you need some other remote, alter as needed.

hg paths default
# git+ssh://git@example.com:some_user/some_repo.git

What is the repo root?

hg root
# /home/You/current_repo

Change to the Git directory under the repo root

cd /home/You/current_repo/.hg/git

Speak the magic words that get Git to remove a remote branch, passing the remote repo URL in the style that Git expects

git push git@example.com:some_user/some_repo.git :branch-to-delete

Also delete the remote-tracking branch, because Git does not automatically delete it. Hg-Git generates the remote-ref tags on the fly from the remote-tracking references, so deleting the remote-tracking branch in Git is necessary and sufficient to no longer see the tag in Mercurial.

git branch -rd default/branch-to-delete

Finally, delete Git's (local ref of the) local branch:

git branch -d $branch
Esteis
  • 4,669
  • 2
  • 29
  • 45