Search code examples
gitdirectoryrenamegit-branch

Right way to rename a directory with git


I have a project with its base directory named "MyProject". The repo is git managed. The main branch has the MyProject folder and some folders inside. I open a new branch "feature1" and in this I have a directory "labeling" and some code inside.

I have also checkout other branches from the main branch (feature2) and these also have a directory "labeling" and some code.

Turns out I want to change the name of the folder

What is the correct way to do this, so that it doesn't make problems for me later?


I am concerned that if I change the name of the folder to "Newfeature", and I commit it and push it, and later even have this branch merged with main, what is gonna happen when I rebase the other branch (that still has the "labeling" folder) over main and try to push it


Solution

  • If you want to change the name of a file or directory inside the repository, then git mv is your friend - it moves and/or renames the file/directory while preserving history. That should also survive later rebases and parallel work, though there's still a chance of conflict if you have competing renames (pro tip: get the team to agree on the new name first, and then have one person make the change)

    If you want to change the name of the directory containing the repository, or a directory higher up in the path, then Git won't care at all.