It’s a common occurrence to rename or move a file to a different directory in a code repository. In Git, renaming a file is analogous to moving it to a new file name. In either case, Git will initially interpret the change as two distinct changes:
To stage a moved or renamed file, you have to stage both of these changes. You could do this manually by running git add
on both file names, or you can use the git mv
command.
The git mv
command renames or moves files and also stages the change in one step. It takes two arguments: a source and a destination.
For example, to rename a file named todo.txt
to done.txt
, you can run git mv
with the original name and new name as arguments.
git
mv
todo.txt done.txt
This will perform three tasks in one command:
todo.txt
to done.txt
in the current directory.todo.txt
as a deleted file.done.txt
as a newly created file.After both changes related to a moved or renamed file are staged, Git will identify that it’s the same file under a different path. From that point, it will appear with the label renamed
in the output of git status
.
If you would like to learn more about the Git staging area, read our How to use the Git staging area, stage changes, and unstage files tutorial.