How to Rename or Move Files in Git with git mv
In the last blog, we learned how to restore deleted files from Git’s staging area. Now, let’s explore how to rename or move files in Git.
You may think renaming is simple: just right-click and rename the file (on Windows) or use the mv command in Linux/macOS. While that works, Git doesn’t know that the file was renamed. Instead, it sees the old file as deleted and a new untracked file created.
👉 The better way? Use git mv.
Manual Renaming (Not Best Practice)
Suppose we have a file called wrong.txt. We rename it manually:
mv wrong.txt right.txt
Or on Windows: right-click → Rename → change wrong.txt to right.txt.
Now check status:
git status -s
Output:
D wrong.txt
?? right.txt
Git interprets this as:
wrong.txt→ deletedright.txt→ a new untracked file
So you end up doing multiple steps:
- Stage the deletion (
git add wrong.txt) - Stage the new file (
git add right.txt)
This works but is not the cleanest method.
Renaming with git mv (Best Practice)
Instead of juggling multiple commands, you can rename in one simple step:
git mv wrong.txt right.txt
Now check again:
git status -s
Output:
R wrong.txt -> right.txt
✅ Git knows this is a rename operation. ✅ No need to manually add/delete files separately. ✅ Cleaner commit history.
Moving Files Between Folders
You can also use git mv to move files between directories:
git mv oldfolder/file.txt newfolder/file.txt
Git automatically stages the move so you don’t need extra steps.
Why Use git mv?
- Keeps your Git history clear (records renames instead of delete+add).
- Saves time with fewer commands.
- Ensures Git correctly understands your intent.
🚀 Don’t Miss Out!
Now you know the difference between manual renaming and using git mv. Always prefer git mv for cleaner history and fewer steps.
👉 Subscribe to Learning Ocean – Subscribers get coupon codes, early access to blogs/courses, and exclusive YouTube videos.
👉 Watch this video explanation
Stay curious, keep coding, and let’s master Git together! 🎉