What command is used to rename a file in Git, including tracking changes?
Posted by TinaGrn
Last Updated: June 24, 2024
In Git, to rename a file while also tracking the changes, you can use the git mv command. The syntax is as follows:
git mv <old_filename> <new_filename>
This command does two things: it renames the file and stages the change for the next commit. After you run this command, you can commit the change by using:
git commit -m "Renamed <old_filename> to <new_filename>"
Alternatively, you can manually rename the file using regular operating system commands and then stage the change with git add. For example:
mv <old_filename> <new_filename>  # Using Unix/Linux/Mac command
git add <new_filename>
git rm <old_filename>             # This is optional since git tracks the rename.
git commit -m "Renamed <old_filename> to <new_filename>"
Using git mv is the preferred method as it simplifies the process by combining the rename and staging steps into one command.