Git Remove File

Git Remove File: In our earlier post on the Git tutorial, we discussed the basic things like how to add files in the staging area with Gitbash, how to write the content in a file, and finally, how to Git commit the file.

We have been able to cover the basics so far, but as we are making progress, the number of unnecessary files in our repository is increasing. Also, there has been no option to rename files up until now.

Post On:Git Remove File
Post Type:GIT Tutorial
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

If you’ve already created a file in Git, don’t worry! You can still change the name of the file by using a few simple commands. In this section, we’ll learn how to alter files in Git so that you can keep your project organized and tidy. In this post, we will try to learn the following:

  • How do you remove files from Git Repository?
  • How do you rename files in Git Repository?

Git Remove: How to Remove Files from Git Repository

There is always a need to clean up space in your git repository as files are constantly being added and removed. There can be many reasons why you would need to clear the memory, so it’s important to know how to remove files.

In this file, remove a section; we will try to find out the possible ways to remove the files :

  • Removing a file from Git Bash.
  • Removing files externally from the Git Repository.

Git Remove: Removing a file from Git Bash.

Before we start anything new in Git, it’s a good practice to check the status of our repository. We want to make sure it’s a clean repository with no changes pending in the staging area to commit. So to do this, we will follow some steps, which are:

  • Check the status of the repository with the help of the git status command.
  • Create an empty file with the touch command
  • Add the file to the staging area
  • commit the changes to the git repository
For All Steps
For All Steps

If you follow us correctly, then until this, we have added the files to the repository successfully. Now, we will try to learn how to remove the file from the repository. To remove the file from the repository, type the below command and press enter.

git rm <file_name>

Remove File
Remove File

You have noticed after pressing enter, it shows your file will be removed, and the message rm <file_name> will be displayed.

File Deleted
File Deleted

After the file got deleted, let’s check the git status

Git Status After File Deleted
Git Status After File Deleted

Now, we can commit these changes to our respective repositories. Suppose we have the same file, but we will delete it this time without using Git.

Git Remove: How to remove a file externally from Git Repository.

If you remove a file without using Git, it means you’re not taking advantage of Git’s ability to clear out space. You’re either using Bash directly or some IDE. IDEs are a common way to rename or delete files through the GUI easily. However, this functionality is lacking in Git Bash. We used the same commands in the Git Status tutorial when we looked at how deleted files behave in Git status.

When you delete a file without using the git command and after that, if you execute the git status command, then you can notice the deleted file is displayed in the untracked files list. But for these files, you can directly commit without adding those files into the staging area.

Note: In this process, you can commit changes without adding the file to the staging area.

Renaming Files in Git Repository

Renaming a file is also one of the most commonly used operations on files. Renaming can be done for many reasons. As a developer, you might want to rename the file that another developer created. You might even need to change the name of a file you created for some reason. Whatever the reason may be, you will encounter renaming frequently when using Git repositories.

Like Delete, we can Renames file in two ways:

  • Renaming a file through Git.
  • Renaming a file externally in the Git repository.

Git Rename: How to rename a file through Git

To rename a file, we must have a file in our repository, but if you don’t have any files, you can create a file by following the above commands, which we mentioned during the file removal process. After renaming the file, you can type the below commands:

git mv <original_file_name> <new_file_name>

In Our Case, we have some time, but we are going to rename the marked file

Beforer Rename
Before Rename

Now it is time to rename the git file by executing the command

After Rename
After Rename

After renaming the file, you can check the status of Git.

After Rename Git Status
After Rename Git Status

The file has been renamed, and Git has registered the change. Be sure to commit these changes, as it will be important later when we rename the file outside of Git.

Git Rename: How to rename the file outside of Git

Here we are, trying to rename the file without telling Git. To do so, we must use the same command without git. Like below:

mv <original_file_name> <new_file_name>

File Rename with out Git
File Rename without Git

During the file rename using Git, we notice that Git is aware of the file change, but now let us see what the reaction of Git is when a file is renamed outside Git. For this, you can use the git status command.

File Rename with out Git - Status
File Rename without Git – Status

It is important to notice the behavior of Git when you rename a file outside Git. When you rename a file outside, git is deleting file2.txt and adds a new file, testing.txt. Also, both files are not in the staging area, so we have to manually add them to the staging area.

After Rename Manually Added Files To Staging
After Rename Manually Added Files To Staging

Since you renamed the file, Git recognizes and displays the change accordingly. Be sure to commit these changes to the Git repository so they take effect. You’ll notice something interesting when you do.

After Rename - Message During Commit
After Rename – Message During Commit

If you notice the above image, the highlight part says rename file2.txt => testing.txt (100%); the term git refers to the confidence level that the previous file and new file are exactly the same with no difference. If any changes were made to the new file and then committed, it would have shown less than 100%.

Categories GIT

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to, or You can join me on Linkedin.

Leave a Comment