Is it possible to reopen a closed branch in Mercurial? - mercurial

I understand that it is possible to close a named branch in Mercurial, so that it will not appear in the hg branches list:
hg commit --close-branch -m 'close badbranch, this approach never worked'
Is it possible to later re-open the branch if need be?

You can just hg update to the closed branch then do another hg commit and it will automatically reopen.
The closed flag is just used to filter out closed branches from hg branches and hg heads unless you use the --closed option - it doesn't prevent you from using the branches.

You can reopen a branch by using the "-f" flag when 'creating' the branch.
No, this command will create a new branch with the same name.
Just forget that it's closed. Switch to the branch, make the changes and commit. It will be automatically reopened. When you're done you can close it again.

try with following:
hg pull && hg update branch_name
Now make a small change to one of the file and then commit it
hg commit -m "minor change"
then push it
hg push -b .
Now you should be able to work normally.

Try this.
Switch to the closed branch before executing. ( hg up closed_branch )
hg st
touch a
add a
hg commit -m 'reopening the closed branch'
This will reopen the closed branch.

Related

Rename hg branch and transplant selective changes

I am working on BRANCH_A. I want to move the last 50 commits on this branch to a new branch BRANCH_B and revert BRANCH_A to the previous commit before these. What is the best method to achieve the same? Is it a good way?
Supposing that you just want BRANCH_A to be at the state of "HEAD - 50 commits" and you do not care if those commits appear in the history, then here is a simple solution:
For having all the commits on a new branch BRANCH_B you can simply create this branch while at the HEAD of BRANCH_A:
$ hg branch BRANCH_B
For restoring a previous state on BRANCH_A you can switch back to the HEAD of BRANCH_A, and commit a new changeset that will undo all the ones you do not want, this is done with hg backout:
$ hg update BRANCH_A
$ hg log -r 'branch(BRANCH_A) and head()~50 and not merge()'
$ hg backout -r 'branch(BRANCH_A) and head()~50 and not merge()'
$ hg commit -m"removed from .. to .."
You might have conflicts if changes happen in similar zones.
If you do want to rewrite history, you can either use the rebase or convert extensions to change names, remove commits etc. It all depends if you are working only locally or if you need to push to a server which is used by other people as well (in which case it is not advised to rewrite history).
Without editing history, simply:
Close the tip of BRANCH_A.
Create a named BRANCH_B off the closed tip of BRANCH_A.
Check in new commits to BRANCH_A starting from the node before the 50 commits.

Why I can switch to a branch after closing it?

I discovered today that I can switch back to a branch even after I closed it. Why?
hg init abc
cd abc
echo 'readme1' > README1.txt
hg ci -Am "1st commit"
hg branch other-branch
echo 'readme2' > README2.txt
hg ci -Am "2nd commit"
hg update default
hg merge other-branch
hg ci -m "Merged other-branch into default"
hg update other-branch
hg ci -m "Closing other branch" --close-branch
hg update default
now I think I'm not supposed to do this
hg update other-branch
but it works ok
It confuses me and makes me feel somewhat uneasy.
UPDATE: sorry forgot to indicate that I'm using HG v.1.6
Like Amber said, when you close a branch, it just records that it is closed. As a consequence when you do "hg branches" you will just see "default", and not "other-branch".
However, as soon as you switch to this branch and commit something new, it automatically reopen it (and thus appears again in the list of "hg branches"). You can also re-close it when you are done.
I find this feature actually desirable: Imagine you have created a "stable" branch in order to stabilize some code for a delivery, only allowing bug fixes on this branch. Now after the delivery, you can close the stable branch and develop new features again on default, switching to the next iteration and preparing the next delivery (assuming you are using scrum for instance). Now when three days after, your delivery customer finds a problem and demand to receive a fixed delivery, not wanting to wait for the next one, then you can easily switch to the stable branch, reproduce the problem, fix it (reopening the branch), redeploy, and finally close the branch again. This seems like a plausible scenario and a good Mercurial behavior to me.
Just my 0.02€ :-)
Cheers,
Christophe.
= The illiterate of the 21st century will not be those who cannot read or write; they will be those who cannot learn, unlearn, and relearn. --Alvin Toffler =
Closing a branch mostly just makes it not show up in certain lists:
https://www.mercurial-scm.org/wiki/PruningDeadBranches#Closing_branches

Closing Hg Branches

When using hg branch FeatureBranchName and publishing it to a central repo that is shared amongst developers, is there a way to eventually close the FeatureBranchName when its development has officially been merged with the default branch?
It would also be helpful if the FeatureBranchName was not visible when performing a hg branches command.
hg commit --close-branch
should be enough to mark a branch close. (see hg commit)
--close-branch
mark a branch as closed, hiding it from the branch list.
See also this thread:
My expectation is that I close a branch because this line of development has come to a dead end, and I don't want to be bothered with it any more.
Therefore, when a branch has been closed I shouldn't see it (in branches, heads, log, for instance) unless I explicitly ask to see closed branches.
I should note that I expect a closed branch to remain in the repository;
it may be useful in the future, and the commit --close-branch message
should at least explain why the branch was closed.
Pruning branches is another thing altogether.
Note: that "closing branch" business is one aspect seen as missing in Git, when compared to Mercurial:
Branches in git are, we’re always told, ephemeral things to be used and thrown away, and so far as I know git doesn’t have a way to indicate to your colleagues that you’re done with a branch;
the only way to do this is to delete it, or to hope they see the final merge commit and understand that the branch is closed to further development.
[In Mercurial] When you’re done with a branch, however, you cannot delete it from the repository; instead, you issue a commit which closes the branch, and Mercurial notes that the branch is closed. It’ll remain a permanent part of your repository history.
I wrote a simple script that completes the branch close, commands found at PruningDeadBranches.
## Script ##
#!/bin/bash
#script to close the not required branch in mercurial
hg up -C $1
if [ $? -eq 0 ]; then
echo "$1 is up"
else
echo "branch not found, please recheck your argument"
exit 1
fi
# if we are here then the branch is up, so we do the following
hg commit --close-branch -m 'this branch no longer required'
echo "$1 is closed"
hg up -C default
echo "default is up"
How to
Move to the local copy of the repository, and run this script by giving an argument. For example:
$./the_script_above.sh bad_branch_name_to_close
What does it do
This does the following:
If the branch exists, it updates to the given branch or else exists with
an error message.
It closes the branch.
Updates to the default branch.
Stops.

Undoing branch creation in Mercurial

How can I undo the creation of a branch in Mercurial? For example, if I issue the command
hg branch newbranch
How can I delete this branch if I decide I entered the wrong name? I'm guessing this must be pretty simple to do, but I have yet to figure it out. Thanks!
If you haven't committed yet, you can simply do a clean reset as per the manual (http://www.selenic.com/mercurial/hg.1.html#commands):
hg branch -C
This will reset the working directory's branch name to the parent of the branch that you just created.
if you haven't committed anything to it, it wasn't really created. so just issue another hg branch newname.
If its already commited:
hg clone -b branch1 [-b branch2 [-b ..]] oldrepo newrepo, i.e. every branch except newbranch, will result in new repo without the newbranch.
If mq extension is enabled then hg strip
Look into editing history before making permanent changes in repository.
Assuming you have not pushed to a remote repository, enable the mq extension and strip the branch off.

Trying to merge back from branch to main and close branch in Mercurial

I've got a named branch (same repository) that was created in order to to spike something. I've now decided that I want to move all the changesets created in the branch back into the main (default) and then close the branch.
I've tried a number of different things, including what was outlined in this post (How to repeatedly merge branches in Mercurial) but I just can't get it working :(
Can anyone provide any pointers?
Thanks.
Merge the feature branch into default
hg up default
hg merge feature-branch-name
hg ci -m 'merged feature-branch-name into default'
Close the branch you don't want to use anymore
hg up feature-branch-name
hg ci --close-branch -m 'close feature-branch-name branch'
hg up default
Note that the close command doesn't have any disruptive effects on the repository history
It flags your branch as closed so that it won't appear in hg branches and hg heads commands output
I've managed to solve my problem using the link I mentioned in my question. The steps described in the link had actually merged my changes across however I didn't realise as I was looking in the TortoiseHg UI and couldn't see the changes there. When I performed hg outgoing via the command line it appears that the merge had worked correctly.