When developing software and dealing with version control systems like Git, it is often necessary to change or even remove files. This guide will show you how to effectively remove or rename files from your repository using the commands git rm and git mv.
Key insights
- With git rm you remove files from the repository without deleting them from the file system.
- The command git mv helps you rename files while simultaneously saving changes in the repository.
Step-by-Step Guide
First, we want to create an empty file and add it to our Git repository. Start by creating a new file. You can do this using the touch command.
Then run the git status command. This gives you an overview of the current status of your Git repository. You should see that the file falsch.txt has been added.

To add this file, use the git add command:
Now we can commit the file. Do this with the command:
If you now run git log, you should see that the file has been successfully added.
However, you have noticed an error: The file should not be named falsch.txt. In this case, you can either rename it or delete it. If you want to delete the file, use the command git rm:
As we check the status again, you should find that the file is marked as deleted but has not yet been removed from the repository. Now use git commit to save the change:

With these steps, you have successfully deleted falsch.txt and noted the change in the repository. Now you will see in the status that the file is no longer present.

Another example is if you have a file that contains sensitive data, like a configuration file with credentials. Create such a file named config.txt and also add it to your Git repository. Again, use the commands:
Before you send the changes, you want to ensure that this sensitive file does not end up in your Git repository. This often happens when you add multiple files at once, and one of them should not belong to the repository. In such a case, you can use git rm, but there is a specific procedure if the file should be kept locally.
Use the following command to remove the file from the repository without deleting it from the file system:

Then make a commit:

Now you see that the file is shown as “untracked” in the status. This means it has been removed from the repository but remains in your file system.

A clever move is to create a.gitignore file to ensure that this file is not accidentally added to the repository. Open or create the.gitignore file and add the line:
config.txt
After doing this, add.gitignore to Git tracking and commit your changes:
Another useful feature within Git is renaming files. Suppose you want to rename a file named README to README.txt. The command for this is:
Run git status again to see that the file has been successfully renamed.

Finally, perform another commit:

Now you have successfully completed all steps and can ensure that the repository is in a clean state.
Summary – Removing and Renaming Files with Git
In this guide, you learned how to remove and rename files with Git. The commands git rm and git mv are essential to keeping your repository clean and organized.
Frequently Asked Questions
What happens with git rm?With git rm you remove a file from your Git repository.
Does the file remain on the file system after git rm?By default, the file is removed from the file system and the repository unless you use --cached.
How can I rename a file?Use the command git mv followed by the old and new file name.
What is the purpose of a.gitignore file?A.gitignore file allows you to specify which files should be ignored by Git.
How can I check the status of my repository?You can check the status of your Git repository with the command git status.