Git mv Command
In this page:
Renaming a File
git mv old-name.txt new-name.txt renames a tracked file on disk and automatically stages that rename in Git, doing in one command what would otherwise take a manual mv plus separate git add and git rm calls.
Example: Renaming a File
git mv old-name.txt new-name.txt
Moving a File to a Directory
The same command relocates files too -- git mv file.txt src/file.txt moves a tracked file into a different directory while keeping Git's history connected to it, rather than treating it as a delete-and-recreate.
Example: Moving a File to a Directory
git mv file.txt src/file.txt
Moving Multiple Files
You can move several files into one destination folder at once by listing them all before the target directory, e.g. git mv a.txt b.txt docs/, which stages every move together.
Example: Moving Multiple Files
git mv a.txt b.txt docs/
Forcing a Move or Rename
If a file with the destination name already exists, Git refuses the move to protect it from being silently overwritten; the -f flag forces the move anyway, replacing whatever was already there.
Example: Forcing a Move or Rename
git mv -f source.txt existing.txt
Dry-Running a Move
git mv -n old.txt new.txt performs a dry run showing what the rename would do, without actually touching any files -- a quick safety check before renaming something important.
Example: Dry-Running a Move
git mv -n old.txt new.txt
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)