The git mv
command in Git is used for both moving and renaming files within a repository. This command is particularly useful when you want to change the name of a file or relocate it to another directory while maintaining Git’s tracking of the file history.
Here are some examples demonstrating how to use git mv
effectively:
Example 1: Renaming a file
Suppose you have a file named oldfile.txt
and you want to rename it to newfile.txt
:
git mv oldfile.txt newfile.txt
This command renames the file from oldfile.txt
to newfile.txt
in the Git repository.
Verification: Check the repository to ensure oldfile.txt
is no longer present and newfile.txt
exists with the file content intact.
Example 2: Moving a file to a different directory
Move file.txt
from the current directory to folder/
:
git mv file.txt folder/
This moves file.txt
into the folder/
directory while maintaining its version history.
Verification: Navigate to the folder/
directory and confirm file.txt
exists there.
Example 3: Renaming and moving a file simultaneously
Rename oldfile.txt
and move it to folder/newfile.txt
:
git mv oldfile.txt folder/newfile.txt
This renames oldfile.txt
to newfile.txt
and moves it into the folder/
directory.
Verification: Check that oldfile.txt
is removed, and folder/newfile.txt
exists with the correct content.
Example 4: Moving multiple files
Move multiple files file1.txt
and file2.txt
to folder/
:
git mv file1.txt file2.txt folder/
This moves both files into the folder/
directory while preserving their history.
Verification: Confirm both files are no longer in the root directory and exist within folder/
.
Example 5: Force moving and overwriting
Force move file.txt
to folder/
and overwrite existing files:
git mv -f file.txt folder/
This forcefully moves file.txt
into folder/
, replacing any existing files with the same name.
Verification: Check folder/
to ensure the correct version of file.txt
exists.
Example 6: Verbose output for tracking changes
Move file.txt
to folder/
and display verbose output:
git mv -v file.txt folder/
This command provides detailed information about the move operation, including any changes made.
Verification: Review the verbose output to confirm the move was executed correctly.
Example 7: Moving files with whitespace in names
Move a file named my file.txt
to folder/
:
git mv "my file.txt" folder/
This demonstrates how to handle files with spaces in their names during move operations.
Verification: Ensure my file.txt
is moved correctly and accessible in folder/
.
Example 8: Undoing a move
If you moved a file by mistake, you can undo the move:
git mv folder/file.txt .
This moves folder/file.txt
back to the current directory.
Verification: Check that folder/file.txt
is now in the current directory.
Example 9: Moving and maintaining file permissions
Move executable.sh
to scripts/
while preserving executable permissions:
git mv --chmod=+x executable.sh scripts/
This ensures that executable.sh
remains executable after the move.
Verification: Confirm executable.sh
in scripts/
retains executable permissions.
Example 10: Moving directories
Move an entire directory src/
to lib/
:
git mv src/ lib/
This moves the src/
directory and all its contents into the lib/
directory.
Verification: Navigate to lib/
and ensure all files and subdirectories from src/
are present.
Discussion about this post