none
How do I preserve GIT Commit History when moving a file between projects

    Question

  • I have a large solution in Visual Studio 2015 using GIT (Bitbucket) for source control. There are seven projects, all in C#. Most of the files have extensive commit histories. However, when I move files to a different project, sometimes the history is preserved and sometimes it is not preserved. How do I ensure that it is kept?
    Saturday, January 16, 2016 1:46 AM

Answers

  • Since I was able to get completed and accurate file change and commit history using SourceTree "log selected" with the "follow" option checked, I decided to go ahead and move the code, commit the changes and push them to origin. 

    Once I did this, all of the history reappeared in Visual Studio.

    So, as far as I am concerned, the problem is now resolved.  thank you for your concern and quick response.

    Best regards,

    Richard


    Thursday, January 21, 2016 4:26 AM

All replies

  • Hi Richard,

    Could you please tell me the detailed steps how do you move files to a different project, which can help us reproduce your problem?

    As far as I know, if your file has more than one commit histories in source control and then you move this file to another project, it will has one commit history that the source control will treat the file as a new file. But if you create a branch for this file, it will preserve the commit histories.

    Best Regards,
    Weiwei

    Monday, January 18, 2016 6:07 AM
    Moderator
  • I have a solution with 6 projects. One of those projects is a dll that contains most of the code. The other projects use the dll to create products. The code is in the MVVM organization, so, there are a lot of files and a lot of folders. The folders are sometimes three levels deep. There are about 2,000 files. the files all have git history accessible by right clicking on them in the Solution Explorer and clicking history.

    I need to move most of the files from the dll project to a different dll project. I am using the Solution Explorer to move them. I have tried moving them using cut/paste and drag and drop. The results seem to the be same. I have tried moving all the files at once, one folder at a time and one file at a time using both the cut and paste and the drag and drop method. The files are correctly moved to the new project and they are moved to the correct directory. Most of the files retain their history but no matter how I do the move, there are always some files that no longer have their git history.  When I look at them using source tree, the files that retained their history were moved by using a rename while the files that lost their history were deleted from the old project and added to the new project, therefore creating a new file and losing the git history.

    So, the question is how do I move them from one project to another and keep the history for all of the files.

    Monday, January 18, 2016 10:45 PM
  • One more piece of information, I am using Visual Studio 2015 Community Edition. I am going to try upgrading the Visual Studio 2015 Professional Edition today to see if that helps.
    Monday, January 18, 2016 10:51 PM
  • Hi Richard,

    According to your description, I create a sample solution which has two projects with Visual Studio 2015 Community. And then I modify two classes in my project one and commit them to let them has several commit histories. Next I copy/past class1 to my project 2 and when I right-click to see its commit histories, the previous histories all missing.

    But if I cut/past the class2 to my project 2, and when I see its commit histories, all previous histories all exist. So if you want to keep the history for all files, please cut/copy your files into another project in Visual Studio. You needn't upgrading the Visual Studio 2015 to Professional edition.

    Best Regards,
    Weiwei

    Tuesday, January 19, 2016 2:38 AM
    Moderator
  • Weiwei,

    Thank you for your information. I have tried both copy/paste and cut/paste. copy/paste never works. cut/paste works for most files but not all. After copy paste of many files, some have history and some do not. The history is still available in SourceTree but not in Visual Studio.

    Dick


    Dick

    Tuesday, January 19, 2016 2:59 AM
  • Hi Dick,

    Since your this problem is occurs with some files and actually the history is still available in SourceTree, please check whether these files are different with that have history.

    Please share one of these files here to let me to reproduce this problem in my side. If it also has this problem in my side, it should be related to these files. If it works fine in my side, there may be something wrong when you operate these files.

    Best Regards,
    Weiwei

    Wednesday, January 20, 2016 3:04 AM
    Moderator
  • The problem is not totally consistent. It seems to be somewhat random. However, I can send two files, one file that usually works OK and one that usually does not. How should I send them to you?

    Regards,

    Dick

    Wednesday, January 20, 2016 6:54 PM
  • Maybe it can be done from command line too. Many articles recommend commands like “git format-path” and “git am”:

    The “git.exe” tool can be found in Visual Studio folders and other places.

    Wednesday, January 20, 2016 8:43 PM
  • I have found that using the "log selected" command in SourceTree with the follow option clicked works reliably. So, that will be my history source henceforth.  Until something is fixed in Visual studio, the history is unreliable once you move a file.
    Wednesday, January 20, 2016 9:29 PM
  • Hi Richard,

    You can upload the files to OneDrive and then share the link here. I will download them and then try in my side.

    Best Regards,
    Weiwei

    Thursday, January 21, 2016 2:36 AM
    Moderator
  • Since I was able to get completed and accurate file change and commit history using SourceTree "log selected" with the "follow" option checked, I decided to go ahead and move the code, commit the changes and push them to origin. 

    Once I did this, all of the history reappeared in Visual Studio.

    So, as far as I am concerned, the problem is now resolved.  thank you for your concern and quick response.

    Best regards,

    Richard


    Thursday, January 21, 2016 4:26 AM
  • Hi Richard,

    I'm glad that you have resolve your problem and thanks for your sharing your solution here. Please mark your reply as answer, which is benefit to other communities who has the same problem.

    If any question about Visual Studio IDE, please feel free to come back to our forum.

    Best Regards,
    Weiwei

    Thursday, January 21, 2016 10:17 AM
    Moderator