0

I made some changes to a file and committed it. (In fact there were several commits).

Then I wanted to revert to the earlier version and lose all those changes.

I did something like:

hg update -r nnn where nnn was the reversion number of the changeset I wanted to go back to.

That worked. I was happy.

Then, later, I had to push my local repository to the remote. But when I did hg push I got a message about there being two heads on this branch and one of them not being known to the remote repositiory. It suggested I merge before pushing. (I think).

I googled this and found a page that suggested I do "hg merge". I did that. Now the resultant file is back to where I started. I.e. it contains all the changes I wanted to throw away.

Where did i go wrong?

EDIT:

I have found this post Mercurial — revert back to old version and continue from there

where it says:

If later you commit, you will effectively create a new branch. Then you might continue working only on this branch or eventually merge the existing one into it.

That sounds like my case. Something went wrong at the merging stage it seems. Was I on the wrong branch when I did "hg merge"?

Community
  • 1
  • 1

2 Answers2

2

You're past this point now but if it happens again, and it's just a single file you want to revert then consider:

hg revert --rev REVISION_YOU_LIKED path/to/just/one/file.txt

That doesn't update you whole repository to a different revision, and it doesn't create any commits. It just takes a single file in your working directory and makes it look like it used to. After doing that you can just commit and you're set.

That's not the way to go if you want to undo all the changes you've made to all files, but for reverting a single file use revert and avoid multiple heads and merging entirely.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • The help message on `hg revert -h` shows `hg revert [OPTION]... [-r REV] [NAME]...` - I was not sure what the `[NAME]` here meant - but from this answer I am guessing `[NAME]` refers to a specific file – Mortz Oct 08 '21 at 09:36
1

No, nothing went wrong at the merge stage – Mercurial did exactly what you asked it to...

What merge means is that you take the changes on your current branch, and the changes on the 'other' branch, and you merge them. Since your original changes were in the 'other' branch, Mercurial carefully merged them back into your current branch.

What you needed to do was to discard the 'other' branch. There are various ways of doing that. The Mercurial help pages discuss the various techniques, but there are pointers in other SO questions: see for example Discard a local branch in Mercurial before it is pushed and Remove experimental branch.

(Edit) Afterthought: the reason you got a warning about there being two heads on the branch is because having two heads is often a temporary situation, so pushing them to a remote repository is something you don't want to do accidentally. Resolutions are (i) you did mean to push them, so use --force to create two heads in the remote repository; (ii) ooops!, you meant to merge them before pushing, so do that; or (iii) ooops!, you'd abandoned the 'other' one, so get rid of it. Your case was (iii).

Community
  • 1
  • 1
Norman Gray
  • 11,978
  • 2
  • 33
  • 56
  • Yes. I think I've understood about the merge. It was Case iii. and the link is helpful. Just two questions: did i create two heads in one branch or a new branch? And, how could I have seen the name of the new branch/head? –  Aug 07 '14 at 08:14
  • A 'head' is the last revision in a given branch (note that git and Mercurial have _subtly_ different terminology here); in linear development you have a single branch, and so a single head. So here you had two branches, and therefore two heads. `hg head` would have shown you these; see `hg help` for a list of useful commands, and eg `hg help head`. I define `alias hgl5='hg log -G -l5'`: then `hgl5` shows the last five revisions _and_ makes it clear when there's more than one head. – Norman Gray Aug 07 '14 at 09:38
  • Thanks. The -G for graphical is a nice touch. –  Aug 07 '14 at 13:20