Skip to content

How to Properly Rename a Directory in a Git Repository

Sometimes, it is necessary to rename a directory in a git repository. Git allows doing it with the help of the git mv command. Below, we will describe how it works.

Renaming a Git directory in a Repository

You should run the git mv command like this:

git mv

bash
git mv <old-name> <new-name>

Sometimes, carrying out a case-sensitive rename may require to take two steps:

git mv

bash
git mv casesensitive tmp
git mv tmp CaseSensitive

Then you can git add and git commit the changes.

This workaround is necessary because some filesystems are case-insensitive; nevertheless, it works perfectly. While the filesystem may not recognize the case change, Git does. That is why it is considered the preferred way of renaming directories in the Git ecosystem.

The git mv Command

The git mv command takes two arguments: a source and a destination. To move a number of files to a single path, you can specify “n” sources, but the last argument is the destination. First, it renames the source, which must exist and be either a file, symlink, or directory. The last argument must be an existing directory to which the given sources should be moved.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.