← Back to Git Course | Chapter 2: Basic Commands | Lesson 15 of 18

Git mv Command

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

bash
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

bash
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

bash
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

bash
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

bash
git mv -n old.txt new.txt

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.