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

Git mv Command

git mv is like renaming a folder label and telling the teacher about the new name at the same moment, so nobody thinks the old one vanished.
Syntax
bash
git mv old_name new_name
git mv file_name directory/

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Renaming a file in the file manager and then running git add only, when git mv old new does both steps at once (though Git can detect the rename either way).
  2. Running git mv on an untracked file, which fails with not under version control.
  3. Moving a file into a folder that does not exist, as in git mv file.txt src/file.txt, where the src directory must be created first.

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.