Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial Patch Creation and Usage

I have come across a problem that I "think" can only be resolved using patches.

I cloned a project from our main repository, made quite a few changes (updates, deletion of files & directory and additions) to it. These changes are not even committed. The problem is, project from the main repository has been deleted/removed and recreated as a new project (name is same, all the directory structures everything is same as before). I cloned that project again from the main repository and would like to transfer all my uncommitted changes to it.

I am still exploring the hg patch to resolve that. It would be helpful if someone could confirm that creating and adding a patch IS the right approach to this, any resources explaining the process would be of great help.

like image 662
Abidi Avatar asked Dec 27 '10 12:12

Abidi


People also ask

What is patch in mercurial?

A patch file is a single file annotating the changes between two versions of one or more text files. By "applying" a patch, you can update a set of text files from an older version to a newer version. Developers often use patch files to make their changes available to other developers.


2 Answers

You're correct — a patch is what you need to transfer the information from one repository to another (unrelated) repository. This will work since the files are the same, as you note.

So, to transfer your uncommitted changes from your old clone, you do

$ hg diff -g > uncommited.patch
$ cd ../new
$ hg patch --no-commit ../old/uncomitted.patch

That will restore the information saved in the patch. This includes information about files that are added or renamed in the old clone.

like image 51
Martin Geisler Avatar answered Sep 30 '22 12:09

Martin Geisler


The following steps can be performed with a standard Mercurial install:

  1. Commit the changes in your local repository. Note the revision number.
  2. Use "hg export -r REV >patch.diff" to create a patch.
  3. Clone the new repository.
  4. Use "hg import patch.diff" to apply the patch to the new repository.

Example

C:\>hg init example
C:\>cd example
C:\example>echo >file1
C:\example>hg ci -Am file1
adding file1

C:\example>hg clone . ..\example2
updating to branch default
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

C:\example>rd /s/q .hg
C:\example>hg init
C:\example>hg ci -Am same-but-different
adding file1

At this point example and example2 have identical contents, but the repositories are unrelated to each other due to deleting and reinitializing the .hg folder.

Now make some changes and commit them in one of the repositories, then export them as a patch:

C:\example>echo >>file1
C:\example>echo >file2
C:\example>hg ci -Am changes
adding file2

C:\example>hg export -r 1 >patch.diff

Below shows that the other repository can't pull the changes, because of the reinitialization. It can, however, apply the patch successfully:

C:\example>cd ..\example2
C:\example2>hg pull
pulling from c:\example
searching for changes
abort: repository is unrelated

C:\example2>hg import ..\example\patch.diff
applying ..\example\patch.diff
like image 32
Mark Tolonen Avatar answered Sep 30 '22 11:09

Mark Tolonen