23

Many times it happens that I have few commits on my local Hg repository which I don't want to push and sometimes I want to remove the local branch altogether. But I cannot rollback more than one commit which leaves me no choice than creating a new clone and download the whole repository again. This feels stupid, since if I could just delete my local branch which has not affected the remote repository in anyway, then I wouldn't have to create and setup a new clone. So, is it how it is in Mercurial or is there some way to discard a local branch?

Thanks!

orad
  • 15,272
  • 23
  • 77
  • 113
  • 3
    Another helpful tip, in addition to Remy's answer, is to do work in a clone of your local repository. That way even if you do get into a situation where you haven't much choice but to re-clone, it is local and very fast (rather than cloning from remote). [Also see this Q&A.](http://stackoverflow.com/q/6867054/22211) – Joel B Fant Aug 08 '11 at 23:14
  • 1
    These days I only use [git](https://git-scm.com/). – orad Feb 02 '19 at 16:16

3 Answers3

16

If you enable the mq extension (bundled with Mercurial), you can use hg strip. Be careful, though, as this will modify the history of your repository. The safe method is to clone your repository up to the revision preceding the creation of the branch you want to discard, then to pull the remaining changesets that you want to keep.

Remy Blank
  • 4,236
  • 2
  • 23
  • 24
5

I know its too late but it may be useful for any one:

If your branch is not pushed yet.

  • First rollback changes hg rollback only if you have done commit but not yet pushed
  • Second run hg update --clean
  • Third run hg branch any-existing-branch
  • Fourth run hg pull -u
Jaffar Hussain
  • 308
  • 2
  • 9
3

If you find yourself doing this often perhaps you should be using bookmarks instead of named branches. http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169