Subversion Tip of the Day – Moving Files

Many of you are beginning to use Subversion for your source control repository these days.  I am also sure that you have found SVNTortoise to be a welcome addition to Subversion and use it also.  Moving files and directories at the command line is a pretty straight-forward affair, but doing so with Tortoise is a little counterintuitive (but never-the-less simple).

Here’s the easiest way that I’ve found to move a file or subdirectory around using Tortoise…

 

Before I begin, here is folder/file structure for my demonstration

folders are named based on initial hierarchy

image

documents are likely named…the following are documents in folder1.1

image

and here are the contents of folder1

image

 

Moving a file/directory

Many times (especially early in projects) we may find that we’ve decided that a file should be in another directory.  How to move a file was not readily apparent to me and during the error phase of my trial and error I often ended up exporting the file and adding it to the new location.  This is bad because I lost history on the file at it’s new location. 

Moving the Wrong Way:

Here’s the revision graph for doc1.1.1.txt before a bad move:

image

Now I’ll drag and drop it to folder 2 by using cut and paste:

image

image

This is the revision graph after committing the move:

image

As you can see the revision history is lost for this item because Subversion was unaware of the move.

Moving the right way:

To move files the right way using Tortoise, highlight the file(s) you want to move and drag and drop to the new location using the RIGHT mouse button.

Revision graph of doc1.1.3.txt before the move:

image

When you drop the file(s) to the new location you will be presented with a popup menu

image

Here you can select a SVN move, copy, or export.  I selected ‘SVN Move versioned files here’.

image

image

Notice the ‘Add’ symbol for the moved file.

The revision graph for the file hasn’t changed (because the contents are unaltered)

image

The revision graph for the parent folder shows the file move

image

So, as you can see, the history is preserved.  The same process is used to move multiple files and directories to new locations.  You can also use ‘Rename’ to move individual files, but I’m not too fond of the approach (it’s also not good if done at the folder level).  I do wish that there was a hotkey for this functionality or at least inclusion of the move command into the normal SVN menu.  The TortoiseSVN documentation does cover the subject briefly but without the pretty pictures.  I sort of wish that I had RTFM before screwing up the history on some of my repositories, but where’s the fun in that? 

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Joshua Lockwood

I code stuff and people pay me for it.
This entry was posted in Subversion, Tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

13 Responses to Subversion Tip of the Day – Moving Files

  1. Ranjeet says:

    Pretty useful, cleared my many doubts and fears in these simple steps for moving the project files work many hours of project team work

  2. Fred says:

    Thanks for this. I was looking in the normal TortoiseSVN context menu and was at a complete loss as to how I was meant to move files without losing the history. I’ll have to read through the documentation some time, but for now this sorted my problem.

  3. jlockwood says:

    I’m glad this was of use to you. Figuring it out was a bit annoying.

  4. Asif says:

    Thanks for the tip……I was surprised not to find a move option in the normal right-click context menu of TortoiseSVN……this helped a lot.

    The method is easy to use once you have figured it out……but I think it’s quite non-intuitive.

  5. ART by LENA says:

    Thank you very much for your time! That hidden SVN menu was definitely not obvious…..

  6. Venkata Ramesh says:

    greatefull

  7. Venkata Ramesh says:

    very very usefull. It save so much time for me. Thank you

  8. I tried today using the same technique to RENAME the files, keeping in the same folder – and SVN lost the connection. The result was “delete old, add new”.

  9. Arvind says:

    I am not sure I understand completely. How can you see the History when it is in Add mode after move. Do you need to commit after you see add?

  10. jlockwood says:

    @Arvind I haven’t used Tortiose in a while, but I still use svn daily (mostly at command line). So, assuming I’m on track here:

    You are correct, you can’t see the Add in the history until commit. Before continuing, I’d like to add that svn mv, svn rename, and svn ren are the same command. Here’s a simple model of the rename at the command line:

    # added a new file to svn directory
    svn add test_file.jmx
    >> A test_file.jmx

    # committed it
    svn commit -m “test check-in” test_file.jmx
    >> Adding test_file.jmx
    >> Transmitting file data .
    >> Committed revision 30394.

    # now it can be found in repository
    svn log test_file.jmx
    ————————————————————————
    >> r30394 | jlockwood | 2009-09-23 12:59:03 -0500 (Wed, 23 Sep 2009) | 3 >> lines
    >>
    >> test commit

    # now I rename it
    svn ren test_file.jmx my_test_file.jmx
    >> A my_test_file.jmx
    >> D test_file.jmx

    # now I’ll commit each part separately (instead of typical ‘svn commit’ for complete rename)
    svn commit -m “commit after rename” test_file.jmx
    >> Deleting test_file.jmx
    >>
    >> Committed revision 30410.

    svn commit -m “commit after rename” my_test_file.jmx
    >> Adding my_test_file.jmx
    >> Transmitting file data .
    >> Committed revision 30411.

    >> svn log my_test_file.jmx
    >> ————————————————————————
    >> r30411 | jlockwood | 2009-09-23 17:28:04 -0500 (Wed, 23 Sep 2009) | 1 line
    >>
    >> commit after rename
    >> ————————————————————————
    >> r30394 | jlockwood | 2009-09-23 12:59:03 -0500 (Wed, 23 Sep 2009) | 3 lines
    >>
    >> test commit

    * so you can see by the “test commit” message that the revision history of the same “file” is preserved, even though the filename has changed. It is true that you will see an Add/Delete pair for a rename or move command, but the important thing is that as files are moved and/or renamed they do not lose continuity in regard to their revision history.

  11. Rob says:

    I was always wondering how to do this with TortoiseSVN and now I know. Thanks!

  12. O. Ward says:

    I tried it with TortoiseSVN 1.6.3, Build 16613. It didn’t work. The history was lost after the SVN move.

  13. popeto says:

    Another approach is to actually right click and view the repository. There you CAN drag a folder/file from one location to another, and subversion retains the history. When you sync, you will see the items added/deleted to reflect the new location.

    If you are not seeing the history after either approach, and are using tortoise, make sure you have the “Stop on copy/rename” checkbox at the bottom of the Log Messages window unchecked.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>