Quick Guide — How to Rename a File in Git

Renaming files in Git is easy when you use git mv. This quick guide covers the one-liner, case-only gotchas on macOS/Windows, multi-file moves, and how to verify the rename.

Table of Contents

Renaming files is a common maintenance task—cleaning up naming, fixing typos, or reorganizing a project. Here’s the fastest, least-surprising way to rename files in Git from the command line, plus a few gotchas to save you time.


TL;DR

For case‑only changes on macOS/Windows, rename via a temporary filename first.


Prerequisites

  • Git is installed and the repo is already initialized and up to date.
  • You’re on the branch where you want the rename to happen.


The 10‑second rename

Use git mv so Git tracks the rename automatically (stages the change for commit):

That’s it. git mv performs a file move plus a stage in one step. (Under the hood it’s equivalent to mv + git add -A + git rm.)


Case‑only renames (macOS/Windows gotcha)

Most macOS and Windows file systems are case‑insensitive. A rename that only changes letter case (e.g., Readme.mdREADME.md) may seem to do nothing.

Fix: do a two‑step rename via a temporary name

Tip: If your repo is shared across different OSes, always use the two‑step approach for case-only renames.


Renaming into a new folder (create missing dirs)

If the destination folder doesn’t exist yet:


Rename multiple files

You can combine git mv with shell expansion or loops.

Add a prefix to all .spec.ts files:

Move a whole directory:


Oops — undo before commit

If you haven’t committed yet, you can unstage and restore the old path:


After committing — revert the rename

If the rename is already committed and pushed, make a follow‑up commit to move it back:


Verifying Git sees the rename

R means Git detected a rename; the number is the similarity score.


Common errors & fixes

  • fatal: destination exists — Choose a different destination or remove/rename the existing target first.
  • Pathspec did not match any files — Check the source path; ensure the file is tracked (git ls-files | grep <name>).
  • Case-only rename didn’t stick — Use the two‑step temp filename workaround above.

FAQ

Do I have to use git mv?
No—Git detects renames heuristically. But git mv is clearer in history and avoids forgetting to stage deletions.

Will history be preserved?
Yes. Git tracks content, not file IDs. Use git log --follow <file> to trace history across renames.

What about submodules or LFS?
git mv works the same. For submodules, you’re moving the gitlink entry, not the nested repo content.

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.