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.
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
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:
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.
Use git rm -r so every file under the old directory is removed from the index recursively.
| Action | Command | Description |
| Rename Directory | git mv old_dir new_dir | Correctly renames a directory and stages the change. |
| Commit Change | git commit -m "message" | Commits the staged changes to the repository. |
| Verify Status | git status | Checks the current status and shows the rename as staged. |
| Handle Untracked Files | git add new_dir/ | Adds untracked files in the new directory to Git. |
| Manual Rename Handling | git add new_dir/
git rm -r old_dir/ | Manually handles the add and removal when git mv is not usable. |
Related reading
- In Git, how can I recover a staged file that was reverted prior to committing?
- In Git how can you check which repo in Github you are pushing to from the command line?
- In git, how do I create a single patch for the last 2 revisions?
- In git, is there a simple way of introducing an unrelated branch to a repository?
- In Git, what is the difference between origin/master vs origin master?
- In plain English, what does git reset do?
- In what cases could git pull be harmful?
- Install a specific anaconda package version
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.