Git
Repository Management
Directory Renaming
Coding Practices
Version Control

How to rename a directory in a Git repository

Codemia Knowledge Hub ·

Run git mv old_directory_name new_directory_name and commit. git mv renames the directory and stages the change in one step.

bash
git mv old_directory_name new_directory_name
bash
git commit -m "Rename directory from old_directory_name to new_directory_name"

How Git records a rename

Git tracks content, not files or directories, and it does not store renames explicitly. A rename is recorded as the old paths being removed and the new paths being added; Git then detects the rename by comparing content. So git mv is a convenience rather than a requirement: renaming the folder with your file manager or mv and staging both sides produces the same commit (see the manual steps below).

Checking the staged rename

bash
git status

The output should show the files as renamed and staged for commit. Commit soon after renaming; leaving a rename uncommitted can cause confusion and conflicts in a shared repository.

Untracked files in the directory

git mv only stages tracked files. Untracked files in the directory are not added for you, so if you want them in the repository, add them after the move:

bash
git add new_directory_name/

Renaming a directory with many files can look slow because Git updates the index entry for every file, but Git handles this efficiently internally.

Renaming without git mv

If git mv fails (for example, because of conflicting changes, or because the directory was already moved or deleted in your working tree), do it by hand: move the directory, add the new path and remove the old one from the index.

bash
mv old_directory_name new_directory_name
git add new_directory_name
git rm -r old_directory_name

Use git rm -r so every file under the old directory is removed from the index recursively.

ActionCommandDescription
Rename Directorygit mv old_dir new_dirCorrectly renames a directory and stages the change.
Commit Changegit commit -m "message"Commits the staged changes to the repository.
Verify Statusgit statusChecks the current status and shows the rename as staged.
Handle Untracked Filesgit add new_dir/Adds untracked files in the new directory to Git.
Manual Rename Handlinggit add new_dir/ git rm -r old_dir/Manually handles the add and removal when git mv is not usable.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions