J. Pedro Ribeiro

What's the deal with renaming files in Git?

June 11, 2016

If you try renaming a file in your project, you might notice this weird behaviour when checking its status: `$ git status On branch master Your branch is up-to-date with ‘origin/master’.

Changes not staged for commit: (use “git add/rm …” to update what will be committed) (use “git checkout — …” to discard changes in working directory)

deleted: README.md

Untracked files: (use “git add …” to include in what will be committed)

README-NEW.md

no changes added to commit (use “git add” and/or “git commit -a”)`

Why is this happening?

The short answer is: Git relies on names to know if whether a file is tracked or not. When you rename a file, Git believes the original file got deleted and suddenly there’s a new file in which it knows nothing about. Hence the untracked status.

Alternative: rename with Git

If you want to avoid this behaviour, you could simply use the native Git command: git mv `$ git mv README.md README-NEW.md $ git status On branch master Your branch is up-to-date with ‘origin/master’.

Changes to be committed: (use “git reset HEAD …” to unstage)

renamed: README.md -> README-NEW.md`

Nice and easy.


J. Pedro Ribeiro

Hello!
I’m a Brazilian front-end developer living in London. This website features some of my latest projects and my thoughts on anything web related.
You can find me on Twitter, Instagram, and LinkedIn.
Wanna talk? Send me a message.