git mv
Learn the git mv command to move or rename files in a Git repository while keeping them staged in one step. See options and examples.
Definition
The git mv command moves or renames a file or directory that Git already tracks, and stages the change in a single step. It is a convenience wrapper: running it is equivalent to moving the file yourself, telling Git the old path is gone with git rm, and adding the new path with git add — all at once.
Renaming a file
To rename a tracked file, give the old name and the new name:
git mv old-name.txt new-name.txtThe file is renamed on disk and the change is already staged, ready to commit. A following git status shows a clean renamed: entry.
Moving a file into a directory
Give a directory as the destination to move a file without renaming it:
git mv styles.css assets/You can move several files into a directory in one command, as long as the destination is an existing folder.
What git mv saves you
Without git mv, the same rename takes three manual steps:
mv old-name.txt new-name.txt
git rm old-name.txt
git add new-name.txtgit mv collapses these into one and guarantees the staging area stays consistent. Note that Git does not actually store renames — it detects them by comparing file contents when you run git log or git diff. git mv simply makes the working-tree change tidy and pre-staged.
Common options
| Command | Description |
|---|---|
git mv <source> <destination> | Renames or moves a tracked file and stages the change. |
git mv <source> <dir>/ | Moves a file into an existing directory. |
git mv -f <source> <destination> | Forces the move even if the destination already exists. |
git mv -k <source> <destination> | Skips moves that would cause an error instead of failing. |
git mv -n <source> <destination> | Dry run — shows what would happen without doing it. |
When the destination exists
By default Git refuses to overwrite an existing file. If you intend to replace it, add the force flag:
git mv -f draft.md final.mdUse this carefully, since it discards whatever was at the destination.
Practice
What does 'git mv old.txt new.txt' accomplish?