简体   繁体   中英

How to merge with mercurial

I created a repo and made some changes into a file. Then I committed and kept working. A couple of changes and I committed again. I had to go back to the first version, so I did:

hg up 5f01300

And the files from the first version came back.

I made some changes and committed again.

hg commit -m 'Updated'

A new head was created and then I tried:

hg push

Mercurial said there were "outstanding uncommitted" stuffs.

I tried to commit again:

hg commit -m 'Changes accepted'

Then I typed:

hg merge

So a lot of errors appeared because there were two heads and I couldn't merge them.

Sometimes I try to merge like I did above and it works perfectly. Usually at the second time I try to repeat the process, it breaks and I can't merge anything.

Can somebody teach how to merge properly?

[SOME ERRORS]

Usually, at the second time, this is what happens:

int main(int argc, char **argv)
{
        printf("hello, world!\n");
<<<<<<< local
        printf("sure am glad I'm not using CVS!\n");
=======
        printf("sure am glad I'm using Mercurial!\n");
>>>>>>> other
        return 0;
}

And I have to do:

hg resolve -m hello.c

Merging is how you combine divergent versions of your files. Whenever you change a file in two different ways, Mercurial will try to merge it when you run hg merge .

When the changes to the files don't overlap, Mercurial can do the merge automatically. However, if you change the same line in two different ways, then you need to help Mercurial merge the two edits.

Consider a file that starts with hello, world! in the first revision and then changes into sure am glad I'm not using CVS! in the second revision:

"hello, world" --- "sure am glad I'm not using CVS!"

You now update back to the first revision and make a different change to the text:

"hello, world" --- "sure am glad I'm not using CVS!"
               \
                \
                 \
                   "sure am glad I'm using Mercurial!"

You now have a divergent history and when you hg merge , Mercurial will tell you that it has detected a conflict in the file. Mercurial will search for a merge tool and if it cannot find any on your system, it will dump the conflicting changes in the file. The file then looks like this:

<<<<<<< local
sure am glad I'm not using CVS!
=======
sure am glad I'm using Mercurial!
>>>>>>> other

The markers you see are called merge markers and they show you the region of the file that couldn't be automatically merged. The top region marked with local is the version you had in your working copy before you typed hg merge . The bottom region marked other is the version that was on the branch you merged with.

Mercurial knows that the file is unresolved and you can see with with hg resolve --list . You now edit the file to somehow combine the two versions. Here's one combination:

sure am glad I'm using Mercurial and not CVS!

I've deleted the merge markers and left behind a version I like. You now mark the file as resolved and commit the merge:

$ hg resolve --mark hello.c
$ hg commit

I hope that helps!


Another comment: You write

[...] I tried: hg push . Mercurial said there were "outstanding uncommitted" stuffs.

Mercurial never talks about uncommitted changes when you try to push. Pushing and pulling are unrelated to your working copy (where your uncommitted changes live). So it is okay to push and pull with a dirty working copy. The error you're thinking of is probably

$ hg merge
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg merge
abort: outstanding uncommitted merges

That is, you cannot start a new merge before you commit or abort the current merge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM