Back to all posts

Quick Tip - Directory Renames with Git & Visual Studio

Posted on Aug 03, 2020

Posted in category:
Visual Studio

Sometimes the most trivial tasks are ones that we sink entire days of effort into and find the solution was one keyword. For me, most recently, this was the concept of renaming a directory inside a Git repository.

My Environment

I should preface this that 99.9% of my work with Git-based repositories is done from within Visual Studio 2019 utilizing the tooling that is available within Visual Studio. For those of you command-line junkies, it could be less of a limitation.

The Goal

I had a huge project where the main source was contained in a folder called Source, and we needed to rename that folder to src to meet automation and other requirements to bring this project in compliance with other projects. We had more than 2.5 years of history of the project within Git, and all initial attempts by the team to rename this folder resulted in Git seeing it as a bunch of "deletes" and "adds."

Don't get me wrong, using the VS Tools we would have had methods to go back, but simple things like "View History" would have been lost. We wanted this to appear as a true move with the history intact.

Going Command Line

After lots of looking, I discovered that this is just one of the infrequent occasions that we had to use the command line. We tried

  • Going directly to the file system and doing a "rename"
  • We tried renaming within the "Folder" view within Visual Studio
  • We even tried a standard command-line rename

All of these had the same impact.

Introducing git mv

The final solution, as I mentioned was straightforward, using a single command-line option with Git.

Our Working Command
git mv source src 

From here, we were able to go back into Visual Studio and do our regular commits, and everything functioned expected. The changes were all recorded as moves, and the history was retained.

I hope this helps someone else!