AlgoMaster Logo

Renaming Files (git mv)

Last Updated: January 3, 2026

5 min read

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 Basics of git mv

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.

How it Works

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.

Command Syntax

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.

Practical Examples

To illustrate the power of git mv, let’s look at some practical examples.

Moving a Single File

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:

  • Moves app.js from the root to the src directory.
  • Stages the move for the next commit.

Running git status after this operation shows the changes:

Renaming a File

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.

Why Use git mv?

Using git mv instead of manually moving or renaming files and then staging the changes with git add has several advantages.

Automatic Staging

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.

Clear Commit History

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.

Avoiding Mistakes

Moving files manually can lead to mistakes, especially if you forget to stage the changes afterward. git mv reduces the likelihood of such errors.

Common Pitfalls

While git mv is straightforward, there are some nuances and potential pitfalls to be aware of.

Moving Non-Tracked Files

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.

Moving a Directory with Untracked Files

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.

Case Sensitivity on Different File Systems

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.

Viewing Changes After git mv

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.

Checking Status

To see what changes are staged, run:

You should see output indicating the file has been renamed or moved.

Inspecting History

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.

Best Practices

To maximize the benefits of git mv, consider these best practices.

Commit After Moving

Always commit your changes after performing a git mv. This ensures that your history reflects the current state of the repository accurately.

Use Descriptive Names

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.

Organize Your Project Structure

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.