git mv
is a command used to rename a file or move it to a different directory within a Git repository. It accepts two arguments: the source file name and the target file name.
Initial Example
Renaming a file using:
$ git mv <old-file-name> <new-file-name>
MV Options
Option | Description |
---|---|
-f , --force | Forces the renaming or moving of a file even if the destination file already exists. |
-k | Skips move or rename actions that would lead to an error condition. Errors occur when a source doesn’t exist or isn’t controlled by Git, or when it would overwrite an existing file without the -f option. |
-n , --dry-run | Performs a dry run, showing what would happen without actually making any changes. |
-v , --verbose | Reports the names of files as they are moved or renamed. |
Example
1. Renaming a File
This example shows how to rename a file using git mv
.
Before running git mv
:
$ git mv oldfile.txt newfile.txt
2. Moving a File to a Different Directory
You can also move a file to a different directory using git mv
.
$ git mv oldfile.txt new-directory/newfile.txt
3. Combining Rename and Move
In this example, you will rename a file and move it to a different directory simultaneously.
$ git mv oldfile.txt new-directory/renamedfile.txt
By using git mv
, you can efficiently manage file names and their locations within your Git repository, making it easier to organize your project’s structure and maintain a clean history of changes.