Vaibhav Bajpai
Vaibhav Bajpai

Reputation: 16774

Close an unmerged wasteful branch in mercurial

I decide to start an experiment in a branch

[default] $ hg branch experiment
[experiment] $ [... some commits ...]

Aargh! does not work! I want to throw it away.

[experiment] $ hg commit -m "did not work; closing ..." --close-branch
[experiment] $ hg update default

To get the real tip back -

[default] $ [... some commits ...]
[default] $ hg push

Is this a correct workflow to destroy an experimental branch?

Upvotes: 1

Views: 1015

Answers (4)

Helgi
Helgi

Reputation: 5436

Closing a branch will leave it in the repository, and the closed branch will be pushed with other changesets next time you do a push.

If you don't want this to happen, and your branch is local, just strip it.

On the other hand, if you have already pushed the experimental branch, stripping it won't help, so you can either close it or do a dummy merge (or both).

Upvotes: 1

Tim Henigan
Tim Henigan

Reputation: 62168

The Mercurial wiki covers all the options for Pruning Dead Branches. Briefly, these options include:

  1. Closing the branch (as done in your original post)
  2. Create a new clone that does not include the dead branch
  3. Use a no-op merge
  4. Use the strip command that is bundled with the mq extension

Upvotes: 1

Ry4an Brase
Ry4an Brase

Reputation: 78330

You've got two fine answers on how to undo your branch, but the bigger point is don't use named branches for temporary concepts. Named branches are for long lived entities like 'development' and 'stable'. For features, expiriments, etc. you want either clones, bookmarks, or anonymous branches. All three are contrasted with named branches in this excellent article by Steve Losh:

http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/

You can see similar advice from the Mercurial project here:

https://www.mercurial-scm.org/wiki/StandardBranching

Upvotes: 2

Chris Phillips
Chris Phillips

Reputation: 12377

In my opinion, you should just close the branch and forget about it.

In the long run, there's no harm in a "dead" branch being present in the repository. Any given branch is almost certainly tiny in comparison to the contents of your repository and any additional "noise" created by the additional changesets is going to fade into the past relatively quickly.

However, by not worrying about cleaning up the branch, you achieve two things:

  1. You don't have to deal with any of the potential issues associated with altering history in a DVCS.
  2. (More importantly) You have a permanent record of your attempt.

That second point is key -- you can actually make use of what you learned if the branch is still around: any fellow developers can learn from it; you can go back and try again if you learn something else; you can prevent trying the same thing again by seeing this branch in history.

A lot of developers have a hard time with keeping history that isn't "pristine" in their DVCS, especially when they recently came from a centralized VCS.* Over time, I've come to realize that there's nothing bad or wrong about that "other" history and in fact it can turn out to be remarkably useful if kept around.

*I'm not necessarily implying that you fall into either of these camps, just making an observation.

Upvotes: 0

Related Questions