Hone

Lessons · Git · renaming a tracked file

Renaming so history follows

git mv renames the file and stages the rename; status shows R and the file's history stays connected.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Files get renamed as projects grow. Done with git mv, git log --follow still shows the commits from before the rename; done with plain mv it looks like a delete and a new file.

How to think about it

Rename with git mv, commit, and keep the rename in its own commit when you can: a rename mixed with edits is hard to review and hard for Git to detect.

Worked example

git mv a.txt notes.txt
Renames the file and stages the rename.
git status --short
R a.txt -> notes.txt.
git commit -m "Rename a to notes"
History follows: git log --follow notes.txt shows the commits from before.

Your turn

Rename the draft and stage it.

git  draft.md final.md

The trap

Renaming with plain mv shows as one deletion and one untracked file; Git only recognises the rename once both are staged.

Practise renaming a tracked file on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.