How to remove accidental branch in TortoiseHg? - mercurial

(I am a relative newcomer to TortoiseHg, so bear with me :-) I use TortoiseHg on two machines to talk to my remote source repository. I made changes on one machine, committed them, and attempted to push them to the remote repository BUT I forgot to first do a pull to get the latest code first. The push gave me a few lines of output, suggesting I may have forgotten to pull first (true!) and mentioned something like "abort: push creates new remote branches...".
So I did a pull, which added several nodes to the head of my graph in the repository explorer. The problem is that the push I tried to do is now showing as a branch in the repository explorer. Looking from the server side (codeplex), it shows no sign of my attempted push, indicating this accidental branch is still local on my machine.
How could I remove this accidental branch? I tried selecting that node in the graph then doing "revert" but it did not seem to do anything. I am wondering if it would be simplest to just discard my directory tree on my local machine and do a completely new, clean pull from the server...?

First make sure you have committed all your local changes. Then merge the branches by calling hg merge and commit the result.
This should bring you back to a single branch, reuniting the two heads.

I had a branch I didn't want, but found I could not merge the branch (or it was very difficult for me to figure out how to merge) because it contained a conflict based on a file name case change.
Ultimately, I decided to hg commit --close-branch. Since the branch had existed only on my local repo, and not the repo I had cloned from, the subsequent hg push didn't even bother to push the closed branch out to the master repo! Very convenient. At that point, all I had to do to eliminate it entirely was delete my local repo and re-clone from the "master".

Do a "Merge With" and check "Discard all changes from merge target (other) revision". Of course you should make sure that the target shown as the merge target is really the one you want to throw away before you click the Merge button.

In the Repository explorer, choose the first rev of your local changes, then right click on the tip of the branch you just pulled and choose "Rebase on top of selected" or "Modify history->Rebase on top of selected" depending upon your client version. This will "re-base" your revs on the pulled ones.
Additionally, to help avoid it in the future...
In Repository Explorer, choose Tools->Settings. In the top left drop-down, choose "User global settings", so this applies to all repositories. Then choose Synchronize on the left. In "After Pull Operation" choose "rebase". This will cause your local revisions to be "rebased" upon the revisions you just pulled, rather than leaving them in a different branch.
This is how I do it and is probably what you typically want.
For more info, see the rebase project and the rebase extension.

This is how to do it with the command-line tool. I guess it can be easily mapped to TortoiseHg (although I'm not sure, since I never use it...) Anyway, since it should be done just once in a while, I think there is no problem on using the terminal here.
An example setup
Suppose your remote repository is something like this:
# changeset: 3:a4c18a1fba12
| tag: tip
| summary: commit 4
|
o changeset: 2:91c5dbfba15c
| summary: commit 3
|
o changeset: 1:4c77cb7952ea
| summary: commit 2
|
o changeset: 0:085dae46f27e
summary: commit 1
Locally, you did not have commit 4, so you committed something directly over commit 3:
# changeset: 3:39526003350f
| tag: tip
| summary: commit 4 made locally
|
o changeset: 2:91c5dbfba15c
| summary: commit 3
|
o changeset: 1:4c77cb7952ea
| summary: commit 2
|
o changeset: 0:085dae46f27e
summary: commit 1
So you try to push it, and get this message:
$ hg push
pushing to ssh://hg#bitbucket.org/brandizzi/mercurial-test-repo
searching for changes
remote has heads on branch 'default' that are not known locally: a4c18a1fba12
abort: push creates new remote head 39526003350f!
(pull and merge or see "hg help push" for details about pushing new heads)
As requested, you pull it:
$ hg pull
pulling from ssh://hg#bitbucket.org/brandizzi/mercurial-test-repo
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files (+1 heads)
(run 'hg heads' to see heads, 'hg merge' to merge)
You have it now...
o changeset: 4:a4c18a1fba12
| summary: commit 4
|
| # changeset: 3:39526003350f
|/ summary: commit 4 made locally
|
o changeset: 2:91c5dbfba15c
| summary: commit 3
|
o changeset: 1:4c77cb7952ea
| summary: commit 2
|
o changeset: 0:085dae46f27e
summary: commit 1
... but you would rather not merge as requested. You want to have this instead:
o changeset: 4:a4c18a1fba12
| summary: commit 4 made locally
|
o changeset: 3:a4c18a1fba12
| summary: commit 4
|
o changeset: 2:91c5dbfba15c
| summary: commit 3
|
o changeset: 1:4c77cb7952ea
| summary: commit 2
|
o changeset: 0:085dae46f27e
summary: commit 1
And then you want to push it to the remote repo.
How do you get that?
Solving it
To get that, you cannot have pushed the "commit 4 made locally". Also, there is no way to put it after the new remote commits. Said that, we can get what we asked.
Said that, you just need to rebase your local commit onto the new remote commit:
$ hg rebase --source 3 --dest 4
If you are lucky, that will be enough.
Handling conflicts
If you are unlucky, you may have some conflicts:
$ hg rebase --source 3 --dest 4
rebasing 3:39526003350f "commit 4 made locally"
merging test.txt
warning: conflicts while merging test.txt! (edit, then use 'hg resolve --mark')
unresolved conflicts (see hg resolve, then hg rebase --continue)
Then, just resolve the conflicts (by manually editing them):
$ hg st
M test.txt
$ nano test.txt # Edit and save
...mark the file as resolved...
$ hg resolve --mark
(no more unresolved files)
continue: hg rebase --continue
...and proceed with the rebase:
$ hg rebase --continue
rebasing 3:39526003350f "commit 4 made locally"
saved backup bundle to /home/adam/software/mercurial-test-repo/.hg/strip-backup/39526003350f-64863882-backup.hg
Here is your new history:
# changeset: 4:ca31fe8a15f0
| summary: commit 4 made locally
|
o changeset: 3:a4c18a1fba12
| summary: commit 4
|
o changeset: 2:91c5dbfba15c
| summary: commit 3
|
o changeset: 1:4c77cb7952ea
| summary: commit 2
|
o changeset: 0:085dae46f27e
summary: commit 1
Now, push it:
$ hg push
pushing to ssh://hg#bitbucket.org/brandizzi/mercurial-test-repo
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files
Those days, it is not as complex as it used to be, right? :)

Related

Continue mercurial branch at new location

I have a branch dev that has been merged into a release branch to form a release commit.
I am hoping to reopen dev from after that merge commit so that my repository has a visual "fan-in fan-out" style. By that I mean that all commits and branches converge into my release commit and then work for the next release diverges out of it. This visually makes it very easy to see what is contained in each release as it keeps them isolated between our tagged release commits.
The effect is that dev is no longer a contiguous branch and I am wondering if this is possible in mercurial.
When I try to hg branch dev again after the release, mercurial complains that it already exists. Even if I commit with say --close-branch after I merge it.
So currently my workaround is to:
(1) either use a different dev-VERSION for each release which is more fiddly and harder to script or
(2) run dev in parallel to release and keep merging from dev into release which isn't so bad, just a little messier for our particular release process.
I know that this might seem like a non-standard mercurial release process. It would suit our particular project and team structure really well though.
Thanks for any help!
EDIT:
To make what I mean a bit clearer, imagine I had merged a feature-01 branch into dev and then merged dev into release in order to create release-1.1. Then I want to make a new feature-02 branch off that merges into dev with a visualisation like:
# changeset: 6:4bcc59fe6ded
|\ branch: dev
| | summary: Merge feature-02
| |
| o changeset: 5:8e117ffe64d0
| | branch: feature-02
| | summary: Add d
\ |
o changeset: 4:c39dae3ff2fa <---- all commits converge to here then diverge back out
/| branch: release
| | summary: Release v1.1
| |
o | changeset: 3:c89af2b3e8db
| | branch: dev
summary: Merge feature-01
This is an example bash script where I'm trying to get something like this:
# Create test
rm -rf test
mkdir test
cd test
# Set up v1.0
hg init
hg branch release
touch a
hg commit -m 'Release v1.0'
# Set up dev branch
hg branch dev
touch b
hg add b
hg commit -m 'Created dev'
# Feature branch
hg branch feature-01
touch c
hg add c
hg commit -m 'Add c'
hg update dev
hg merge feature-01
hg commit -m 'Merge feature-01'
# Release all the things
hg update release
hg merge dev
hg commit -m 'Release v1.1'
# Start a new feature branch
hg update dev # <------ crucial line
hg branch feature-02
touch d
hg add d
hg commit -m 'Add d'
hg update dev
hg merge feature-02
hg commit -m 'Merge feature-02'
If I try to continue feature-02 on the dev branch, it ends up having commit 3:e7ba9d1d9bfe as it's parent when I would like it to have the release commit 4:bbc951a4b339 as its parent but still be on the dev branch.
hg log -G gives roughly this output:
# changeset: 6:46539f99dadd
|\ branch: dev
| | tag: tip
| | summary: Merge feature-02
| |
| o changeset: 5:885b0b930fed
|/ branch: feature-02
| summary: Add d
|
| o changeset: 4:bbc951a4b339 <--- not the parent of the new changes
|/| branch: release
| | summary: Release v1.1
| |
...
I could change my process so that the first feature branch after a new release is branched directly off release, but that makes the process harder to script and just delays the problem until I have to merge it into dev:
So if I use the same script as above but change it slightly:
...
# Start a new feature branch
#hg update dev
hg update release # <--- changed to release
hg branch feature-02
...
Then my graph ends up being:
# changeset: 6:4bcc59fe6ded <---- has the previous `dev` as a parent
|\ branch: dev
| | summary: Merge feature-02
| |
| o changeset: 5:8e117ffe64d0
| | branch: feature-02
| | summary: Add d
| |
| o changeset: 4:c39dae3ff2fa
|/| branch: release
| | summary: Release v1.1
| |
o | changeset: 3:c89af2b3e8db
|\ \ branch: dev
|| | summary: Merge feature-01
...
If I try to rebranch dev:
...
# Start a new feature branch
#hg update dev
#hg update release
hg branch dev # <--- changed
hg branch feature-02
...
I get an error: abort: a branch of the same name already exists.
I hope that helps make it clearer! Thanks for any help!
After doing your merge, just do hg update dev and start making commits. You don't need to close and reopen the branch. You don't need to create a new branch.

Undoing a mercurial rebase

I've done a rebase on my local repository, and it's gone wrong. How do I come back from it?
Before the rebase
# 225:c5eb9b47e19a Task #1 modified due to comment by peers
|
| o 224:7a1964c6b694 WIP on Task #2 depending on Task #1
| |
| o 223:eb5a2ce5ef36 Task #1 sent for peer review
|/
o 222:d18063b01959 Remote tip
The unfortunate rebase
Coming from a git world, I'm used to rebase things. That may have been my mistake.
$ hg rebase -r224
merging some_file
3 files to edit
saved backup bundle to /path/to/repo/.hg/strip-backup/7a1964c6b694-backup.hg
After the rebase
# 224:c5eb9b47e19a Task #1 modified due to comment by peers
|
| o 223:eb5a2ce5ef36 Task #1 sent for peer review
|/
o 222:d18063b01959 Remote tip
$ hg diff
# nothing
$ hg st -amdr
# nothing
$ hg update --clean 7a1964c6b694
abort: unknown revision '7a1964c6b694'!
I'v lost my work commited as WIP on Task #2 depending on Task #1: it isn't in c5eb9b47e19a (I must have messed while merging), and the original commit is gone (is it?).
How do I cancel / revert my rebase?
Get "WIP on Task #2 depending on Task #1" back
hg up 223
hg unbundle /path/to/repo/.hg/strip-backup/7a1964c6b694-backup.hg
Get opposite of "WIP on Task #2 depending on Task #1"
hg revert --all -r 223
hg commit -m opposite of WIP on Task #2 depending on Task #1
Apply opposite of "WIP on Task #2 depending on Task #1" to undo the merge
hg update 224
hg rebase -r 225

Mercurial extension or hook to prevent backout of merge changesets

As noted in the documentation, the hg backout command can cause problems when used with merge changesets. We have had a couple of cases recently of newer developers backing out merge changesets and causing code that we wanted to keep to be reverted when everything gets merged back together.
To avoid this, I'm trying to think of a good way to prevent this from happening at all. Is there a good general way I could write a hook or just disable the backout command entirely?
(Part of our standard developer setup is to install a custom set of extensions, so I already have a good way to install these types of rules locally for our entire development team -- I just haven't thought of a good way to implement the ruleset.)
What about this:
$ hg --version
Mercurial Distributed SCM (version 2.6.3)
...
$ hg log --graph --template='{rev} {desc}'
# 5 c5
|
o 4 merge
|\
| o 3 c4
| |
o | 2 c3
|/
o 1 c2
|
o 0 c1
$ hg backout 4
abort: cannot backout a merge changeset
So it looks like Mercurial does what you want by default. Maybe you need a more recent Mercurial version.
If you are stuck to an old Mercurial version, here is a hacky hook (for *nix/Bash) which aborts backouts of merges:
[hooks]
pre-backout=REV=`echo $HG_PATS | sed -e "s/[^0-9]//g"`; test `hg log -r "parents($REV)" --template='{node}\n' | wc -l` -eq 1 || { echo 'do not do that'; exit 1; }
It extract the revision number from $HG_PATS and then uses hg log and wc to count the number of parents of the revision in question. If there is more than one parent, it is a merge.
Still, I highly recommend to use a recent Mercurial: check the release notes for exciting features you are missing.
Here's a simple Python-based hook that prevents backout of merges. Credit goes to #Oben for pointing me in the right direction.
def prebackout_prevent_backout_merge( ui, repo, **kwargs ):
'''Don't allow backouts to of merge changesets.'''
# Figure out if a --parent version was given or not.
backout_to_parent = kwargs['opts'].get( 'parent', None )
# If no parent version was given, proceed.
if backout_to_parent is '':
return False
# Otherwise abort the operation.
from mercurial import util
raise util.Abort( 'Backout of a merge changeset is not allowed.' )
You can configure this hook in .hgrc or Mercurial.ini with:
[hooks]
pre-backout.ttd_prevent_backout_merge = python:PATH_TO_HOOK_SCRIPT:prebackout_prevent_backout_merge

Mercurial undo merge that has more commits following it?

Have a scenario where we un-intentionally merged a named branch (ABC) into our default branch.
hg rollback is not an option because there have been a couple commits since.
Is there a way to undo this?
You're going to need the Mq extension. If you don't have it turned on, do so by adding this to your Mercurial.ini or .hgrc file.
[extensions]
hgext.mq=
If you're not familiar with it, the Mq extension let's you manipulate history. The good news is, this will allow us to fix your repo. The bad news is that anyone who has a clone of the messed up repo will have to clone it again, because we'll be changing history.
First, go make another clone of your repo to work in, so we don't mess anything up.
Now, find the revision id of the merge changeset (that merged default and your named branch). Write it down. We'll refer to it as changesetM. Now find the revision id of the next changeset. Write it down. We'll refer to it as changesetN.
Once you have those two revision ids, head over to your command prompt and cd into your repo. Then type out the following, replacing changeset[M|N] with the appropriate revision id.:
$ hg qimport -r changesetN:tip
# This will add all of your changes since the merge to the queue
$ hg qpop -a
# This pops them all out of your history.
$ hg strip changesetM
# This removes the merge changeset.
$ hg update -C default
# Make sure we're on the default branch
$ hg qpush -a
# Take the changesets in the queue and push them back onto your history.
$ hg qfinish -a
# Remove changesets from the queue and finalize them as normal changesets.
Essentially, you are rebasing the new changesets on top of the default branch, removing the merge changeset in the process. Once you're done, you'll need to push the changes to a new repository on the server, and have your colleagues clone fresh copies.
Lastly, if you have any other Mercurial questions, also check out kiln.stackexchange.com.
UPDATE
I forgot to mention: If someone has based changes on something that was only in the other branch, it is possible that hg qpush -a will fail. You'll see a foo.txt.rej and foo.txt.orig file laying around. Unfortunately, you'll have to fix this yourself. To fix it, open the original file, the .orig file, and the .rej file and choose the right changes to merge in, saving them in the original file. Once you've merged it in, use hg qrefresh to update that patch to the new, merged patch. From their, you should be able to run hg qpush -a again and continue. If you run into the same error again on another patch, just follow the same process.
If you haven't publish the repo publicly you can do this
hg clone -r (parent1 of bad merge) -r (parent2 of bad merge) old new
and delete the old repo.
I came across the following scenario today:
# changeset: 1728:5d703e1051d3
|\ parent: 1727:1a5f73b5edb4
| | parent: 1720:65ddd0bde225
| | user: nn
| | date: Wed Feb 27 10:35:00 2013 +0100
| | summary: Merge with SomeBranch
| |
| o changeset: 1727:1a5f73b5edb4
| | user: nn
| | date: Wed Feb 27 10:34:30 2013 +0100
| | summary: lorem ipsum
| |
[some more changesets]
| |
o | changeset: 1720:65ddd0bde225
| | branch: SomeBranch
| | user: nn
| | date: Wed Feb 27 07:44:46 2013 +0100
| | summary: lorem ipsum
Where SomeBranch should not have been merged into default. What we did to solve this, was to use the backout command with the parent option like so:
hg backout --rev=1728 --parent=1727
By this you don't undo the merge itself: Looking at a branch graph (either with graph log or in TortoiseHg) you'll still see SomeBranch going into default at r1728. The result of the merge is however undone, meaning that the changeset containing the backout (r1729 in my case) is identical to r1727.

Getting rid of a dangling changeset after rebase in mercurial

I did a hg update --force <repo> hg pull --update --force <other repo> to get another seemingly unrelated repository into the current one.
Afterwards I merged the first changeset of the other repo (to get renamings "right").
A rebase of the other repository starting from the second changeset left the first changeset (here: revision 5431) dangling in the repository:
o changeset: 5433:68c67c7e0bbb
|
o changeset: 5432:331ee440893a
|\
| |
| o changeset: 5431:1023b4c44f18
|
o changeset: 5430:15aff858ec36
To clean things up I'd like to get rid of revision 5431. How would I do that?
Thanks.
By doing hg strip 5431, strip is in the mq extension.
Note that strip is a destructive operation, use with care!