Hg: How to move files (to subfolder) without losing history?
Asked Answered
S

4

49

How to move files (to subfolder) without losing their history?

In my Mercurial repository (I mean the folder with the .hg in it) I have MyProject/ folder with all project files. Now I need to create src/ folder inside and move all files to it (from MyProject/ to MyProject/src/). How can I do it without losing all history?

Sulky answered 26/2, 2014 at 15:2 Comment(2)
Where do you want repository root to be after move? In MyProject/ or MyProject/src/?Descry
@black_wizard It's not about repository root. It should remain where it is. MyProject is NOT repository root. It is a folder in the repository. I just want to create one more subfolder inside this folder and move files.Sulky
S
40
hg mv 

does do the right thing, but hg log does not list entries past the move unless you give it the -f option. See this question for more info

Why 'hg mv' (mercurial) doesn't move a file's history by default?

After you do this, you likely want to add the -f option to hg log to the hgrc file for the repo.

.hg/hgrc

[defaults] 
log = -f 
Solidary answered 26/2, 2014 at 23:30 Comment(1)
Is it possible to hg mv all files from the directory? Or only one by one? I tried "hg mv MyProject/ MyProject/src/" but it is not exactly what I want. It moves the MyProject folder to the src, so MyProject/src/MyProject/ instead of just moving all files from the MyProject/ to the src/Sulky
P
50

Since you have a "tortoisehg" tag, I figured I'd explain the way I do this using the GUI.

Usually, I just rename/move files in my IDE, or from windows explorer, then when I go to commit, THG will show a bunch of (?) unknown files and (R) removed files. Just right click on any of the files and choose "Detect Renames...", then click the "Find Renames" button.

You might have to adjust the "Min Similarity" slider until you get all the files you want and only the files you want, but it's usually very straightforward.

Pneumonic answered 28/2, 2014 at 21:20 Comment(1)
That just saved me a lot of time.Binoculars
S
40
hg mv 

does do the right thing, but hg log does not list entries past the move unless you give it the -f option. See this question for more info

Why 'hg mv' (mercurial) doesn't move a file's history by default?

After you do this, you likely want to add the -f option to hg log to the hgrc file for the repo.

.hg/hgrc

[defaults] 
log = -f 
Solidary answered 26/2, 2014 at 23:30 Comment(1)
Is it possible to hg mv all files from the directory? Or only one by one? I tried "hg mv MyProject/ MyProject/src/" but it is not exactly what I want. It moves the MyProject folder to the src, so MyProject/src/MyProject/ instead of just moving all files from the MyProject/ to the src/Sulky
V
13

In Windows with Tortoise HG installed, there is a windows shell extension that handles this very nicely.

In Windows Explorer, simply right-click and drag the file(s) you wish to move into the destination folder. You are then presented with a pop-up that give you these choices:

HG Move versioned item(s) here

HG Copy versioned item(s) here

Vivacity answered 27/3, 2015 at 20:18 Comment(2)
If you, like me, are running TortoiseHG on a virtualised Windows on a MacBook, this won't work - in this case however you can use Rename..., which also lets you change directories, not just the filename itself.Evonevonne
This doesn't work for me. It moves/copies the file but the history is lost.Immesh
B
5

Use hg mv to move your files and then use hg log -f (follow) to see history including renames.

Bine answered 26/2, 2014 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.