Version management with Git, GitHub and Co

Remove and rename files with Git – Easy guides made simple

All videos of the tutorial Version management with Git, GitHub and Co

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.

Remove and rename files with Git – Easy tutorials made simple

To add this file, use the git add command:

git add falsch.txt

Now we can commit the file. Do this with the command:

git commit -m "File added"

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:

git rm falsch.txt

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:

git commit -m "File removed"
Remove and rename files with Git - Easy instructions made simple

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.

Remove and rename files with Git – Instructions made easy

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:

touch config.txt
git add config.txt

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:

git rm --cached config.txt
Remove and rename files with Git – Easy instructions made simple

Then make a commit:

git commit -m "Configuration file removed from repository"
Remove and rename files with Git – Easy instructions made simple

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.

Remove and rename files with Git – Easy guides made

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:

git add.gitignore
git commit -m ".gitignore added"

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:

git mv README README.txt

Run git status again to see that the file has been successfully renamed.

Remove and rename files with Git – Easy guides made simple

Finally, perform another commit:

git commit -m "README renamed to README.txt"
Remove and rename files with Git – Easy instructions made simple

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.