Rename a file with git mv

Description: Rename readme.txt to README.md and commit the change.

Hint:

  • git mv old new renames the file and stages the change.

Solution

Terminal output

$ git mv readme.txt README.md
$ git status -s
R readme.txt -> README.md
$ git commit -m "Rename readme to README.md"
[main 6b0ae04] Rename readme to README.md
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme.txt => README.md (100%)
$ git log –oneline –follow — README.md
6b0ae04 (HEAD -> main) Rename readme to README.md
38fa585 Add readme

How it works

  • R in the status means renamed.
  • --follow keeps the history of the file after the rename.

Read Git rm and mv.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.