How to rename files in Git?

To rename files in Git, you have to use git mv command. This command takes two arguments:

  1. The present name of the file.
  2. The new name that you want to keep.

Let's say you want to rename sales.csv to transaction.csv then run the following command:

$git mv sales.csv transaction.csv

You will find that the above operation has moved the file to a staging area.

$git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        renamed:    medals.html -> trophies.html

To confirm the renaming, you have to commit the changes.

$git commit -m "Rename sales to transaction."