Last Updated: January 3, 2026
Git is an incredibly powerful tool for managing code, but it can feel overwhelming at times.
One of the fundamental tasks you will encounter is moving or renaming files within your repository. Understanding how to do this effectively can save you time and prevent confusion later on.
Enter git mv, a command that simplifies this process while keeping your history intact.
The git mv command serves a dual purpose: it allows you to move files from one location to another or rename them in your Git repository.
Unlike simply using the shell command to move or rename files, git mv not only performs the action but also stages the changes for you in one step.
When you execute git mv, Git updates its index to reflect the new location or name of the file. Under the hood, it effectively removes the file from its old location and adds it to the new one. This results in a single commit that captures both the deletion of the original file reference and the addition of the new one.
The basic syntax for git mv is:
<source>: The current path of the file or directory.<destination>: The new path or name for the file or directory.If you are moving a directory that contains multiple files, git mv will recursively move all the files within that directory.
To illustrate the power of git mv, let’s look at some practical examples.
Suppose you have a file named app.js in your project root. You want to move it into a src directory. You would run:
This command does the following:
app.js from the root to the src directory.Running git status after this operation shows the changes:
Let’s say you want to rename app.js to main.js. You can achieve this with:
Again, git status will show:
This single command captures the rename and stages it, making your next commit cleaner and more intuitive.
Using git mv instead of manually moving or renaming files and then staging the changes with git add has several advantages.
One of the most significant benefits is that git mv automatically stages the changes for the next commit. This means you don’t have to remember to run additional commands to track your changes.
When you use git mv, the commit history remains clear. It shows a single rename or move action rather than a deletion and an addition. This clarity is especially useful for code reviews and tracking changes later on.
Moving files manually can lead to mistakes, especially if you forget to stage the changes afterward. git mv reduces the likelihood of such errors.
While git mv is straightforward, there are some nuances and potential pitfalls to be aware of.
If you attempt to move a file that isn’t tracked by Git, you will encounter an error. For example:
You need to ensure that the file is tracked before moving it.
If you have untracked files within a directory and you try to move that directory, Git will allow the move but will not track the untracked files. Be cautious about this behavior to prevent losing track of your files.
Some file systems, particularly on Windows, are case-insensitive. If you attempt to rename a file by changing only its case, such as changing App.js to app.js, you might run into issues. Git may not recognize this as a change, and therefore, it will not track it properly. A common workaround is to rename it to a temporary name first.
Once you have used git mv, it is essential to verify the changes you made. You can use the following commands to review the status and history.
To see what changes are staged, run:
You should see output indicating the file has been renamed or moved.
To view the history of your changes, including moves, you can use:
The --follow flag is particularly useful for tracking the history of a file even after it has been renamed or moved.
To maximize the benefits of git mv, consider these best practices.
Always commit your changes after performing a git mv. This ensures that your history reflects the current state of the repository accurately.
When renaming files, use descriptive names that reflect their purpose. This helps with maintainability and makes it easier for others to understand the structure of your project.
Take the opportunity to use git mv to reorganize your project structure as it grows. A well-organized repository can lead to better collaboration and easier navigation.