mv
git mv
is a command used to move or rename files and directories in a Git repository. It is a convenient way to perform these operations because it stages the changes for you, so you don't need to run git add
after moving or renaming the files.
Basic Usage
Move a File
To move a file from one location to another, use the following command:
git mv old_file_path new_file_pathThis will move the file from
old_file_path
tonew_file_path
and stage the change.Rename a File
To rename a file, use the same command with the new file name:
git mv old_file_name new_file_nameThis will rename the file from
old_file_name
tonew_file_name
and stage the change.Move a Directory
To move a directory and all its contents, use the following command:
git mv old_directory_path new_directory_pathThis will move the directory from
old_directory_path
tonew_directory_path
and stage the change.
Examples
Move a File
git mv src/old_file.txt src/new_file.txtThis command moves
old_file.txt
from thesrc
directory tonew_file.txt
in the same directory.Rename a File
git mv README.md README.txtThis command renames
README.md
toREADME.txt
.Move a Directory
git mv docs/old_directory docs/new_directoryThis command moves the
old_directory
from thedocs
directory tonew_directory
in the same directory.
Using git mv
ensures that the changes are tracked correctly by Git and staged for the next commit.