George Mauer
George Mauer

Reputation: 122202

Mercurial: Any way to "merge and discard changes"?

I have a graphlog that looks something like this:

(snip)
  | |
  | o    1) Other Dev: Commit
  | | \
  o | |  2) Me: Commit
/ | | |
| | o |  3) Other Dev: Commits with an error
| |/| |
| o |/   4) Me: Merge and commit
|  /|    
|/  |
o   |    5) Me: Realize there were bugs in the commit and take earlier version to merge with
|   o    6) Other Dev: Fixes error
o /      7) Me: committing some changes
|/
o        8) Me: Merge fixed tip

At (8), everything is as it should be with the exception of the dangling extra head at (4). To get rid of it I have to merge (4) -.-> (8) but, since there is nothing in (4) that I need, I can safely discard all of it's changes. I could do this merge manually file-by-file (and usually this isn't that big a deal) but for my own edification - is there a simple one-line way to say "merge (4) with (8) and always take (8)"?

Upvotes: 24

Views: 7640

Answers (1)

Idan K
Idan K

Reputation: 20901

Yes. The internal:local builtin merge tool.

$ hg merge 4 --tool internal:local

Similarly there's internal:other that picks the other version of files as the merged version.

Here's an example to clarify what's going on, start off with a repo with a single file:

$ echo a >> a
$ hg ci -Am.
adding a
$ echo a >> a
$ hg ci -Am.

Branch it and put a conflict in a:

$ hg up 0
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ echo b >> a

Also add another file just in the merged in branch:

$ echo b >> b
$ hg ci -Am.
adding b
created new head

Go back and merge the anonymous head:

$ hg up 1
1 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ hg merge 2 --tool internal:local
1 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

Naturally at this point without the merge tool we'd get a conflict on a. But using the merge tool, we're telling Mercurial to take the version of the first parent on every file that the merged with cset has also touched.

$ hg st
M a
M b
$ cat a
a
a
$ cat b
b

Upvotes: 38

Related Questions