Use the hg convert
command to make a new repository from a sub directory and retain the change history.
Create a filemap of the tracked files in the subdirectory to include and and rename the files in the new repo.
This is on OS X but should work on other *nix with awk/gawk/equivalent to assist creating the filemap.
UPDATE 2015-09-29: the code now works with a sub directory name that contains space characters.
# cd to the full repo directory cd path/to/full_repo # export tracked files in sub directory by creating a filemap export SUBDIR=name_of_subdir hg locate -I "$SUBDIR" | awk '{print "include \"" $1 "\"";}' > ../filemap.txt echo "rename \"$SUBDIR\" ." >> ../filemap.txt # create new repo using filemap hg convert --filemap ../filemap.txt . ../new_repo_name # cd to new repo and update to latest head revision, the update is required if you want to see your files cd ../new_repo_name hg update
Windows command line version, much the same but needs a temporary file:
rem cd to the full repo directory cd path\to\full_repo rem export tracked files in sub directory by creating a filemap using an intermediary temp file set SUBDIR=name_of_subdir hg locate -I "%SUBDIR%" > ..\filemap.tmp for /f "tokens=*" %A in (..\filemap.tmp) do @echo include %A >> ..\filemap.txt echo rename "%SUBDIR%" . >> ..\filemap.txt del ..\filemap.tmp rem create new repo using filemap hg convert --filemap ..\filemap.txt . ..\new_repo_name rem cd to new repo and update to latest head revision, the update is required if you want to see your files cd ..\new_repo_name hg update