Search code examples
mercurialdvcs

Mercurial overwrite a tracked file with an untracked file


I have a tracked data file which I would like to overwrite with a newer (currently untracked) version

How do you achieve that in mercurial without messing up the history?

Can I just overwrite the original file, so long as the filename stays the same, and everything should be OK?


Solution

  • Overwriting works just fine. Only problem is that mercurial considers that you have made changes to existing file.

    If the new file is really unrelated to original file the best way to do this is to completely remove that file (hg rm filename) and commit the removal and then create the file with the same name and add it to repo (hg add filename) so that its clear to the user that the original file was deleted and then a new file was created.

    hg rm filename
    hg commit -m "Removed filename"
    echo "New file contents" > filename
    hg add filename
    hg commit -m "New version of filename"
    

    When you say

    hg log filename
    

    the entire history of the file is anyway preserved and its clear to the user that the old file was deleted and new file was added.