Enroll in Selenium Training

Coming at this step in Git, we came across how to add the files in git staging area through git bash, how to write the content in a file in those files, how to commit the file.

These operations are enough for the basics, but, as we are progressing, our unnecessary files are increasing in the repository. Also, till now there was no option to rename the file. Once you have created a file in Git, its name remained the same all through the project. Focussing on the same issue, we will learn some operations in Git which cover Alter Files in Git. Compacting it in the form of points, we will learn:

  • Removing files from Git Repository.
  • Renaming files in Git Repository.

How to Alter Files in Git?

The reader should be clear about the Bash and Git Bash as a separate entities. In this tutorial, we will use the same command using both Bash and Git Bash to see how Git reacts for both. This is very important to know and master Git.

How to Remove Files from Git Repository

Files are constantly added and removed from a git repository. Clearing up space is a good practice. There can be a lot of reasons for which you would be required to clear the memory and you will be required to remove the files and you should know how to do it. In this section, we will explore two cases when a file can be removed:

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

Both of these are normal operations in Git and we will see how Git reacts for each of them.

How to remove a file using Git in Git Repository

As a good practice, before starting anything new in Git we must check the status of our repository. It should be a clean repository (no changes pending in the staging area to commit).

Starting off, check the status of the repository by typing git status

Git Status Command

Yes, our tree is clean and it is good to go ahead. Now, to delete a file, we must have a file. Create a new file toolsqa.txt through touch command as shown below:

touch toolsqa.txt

Create ToolsQA File

NoteThe touch command is the easiest way to create new, empty files.

Okay, we have our file ready. Add this to the staging area and commit the changes.

Adding and Committing

The three steps shown above are:

  1. Adding the changes to the staging area.
  2. Checking the status of the Git repository.
  3. Committing the changes in the Git repository.

Okay, now we have added the file to the Git repository. Let us remove the file now by typing the following command and press enter:

git rm <file_name>

Alter Files in Git - Removing A File

Once you press enter, your file will be removed and the message rm <file_name> will be displayed.

Alter Files in Git - File Removed Success

Now, let's check the git status and the message it will give.

Alter Files in Git - Git Del From Git

Commit these changes. Now we will look at the same scenario but by removing the file without the help of Git.

How to remove a file externally from Git Repository

Removing a file from outside the Git repository means you are not taking the help of Git to clear out space. You are either using Bash directly for that purpose or maybe some IDE. IDEs are a common way of easily renaming or deleting a file through GUI. It lacks in Git Bash as we know. We made use of the same commands in the Git Status tutorial when we looked at the Git Status behavior on a deleted file.

Please revisit the post to understand the scenario and how Git reacts once the file is deleted without taking the help of Git Bash.

You should notice that when we deleted the file from outside Git (without using the git command), the file was not staged. Here, without adding the file to the staging area the file has been added and ready to commit. This is an important point and should be noted down for interviews.

Renaming Files in Git Repository

Renaming the files is also one of the most used operations on the file. Renaming can be done due to many factors. As a developer, maybe you want to rename the file that your fellow developer created. Maybe you can change the name even though you created the file due to some reason. Whatever the reason be, you will encounter renaming frequently. Being an important part of Git, we need to learn how to rename a file in the Git repository. As we learned in the deletion, the renaming will also be done through Git and outside of Git.

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

How to rename a file through Git

Renaming a file through Git means you will be accessing the Git commands to operate. Using Git bash and a simple bash is sometimes similar apart from mentioning Git specifically before the command. For example, rm toolsqa.txt is a bash command but the same command can be executed through Git as git rm toolsqa.txt.

Since we do not have any file in the Git repository, please create a new file with the same name i.e. toolsqa.txt by following the steps mentioned above. Once done, you have to check the git status. Confirm that it is a clean directory as we did above. Once everything is done, type the following command and press enter:

git mv <original_file_name> <new_file_name>

Renaming File through Git

Check the status of the repository through git status.

Renaming Successful

As you can see, Git has picked up that the file has been renamed. Remember this step as it will play an important role when we will rename the file outside the git. Commit these changes.

How to rename the file outside Git

In this section, we will look at how Git reacts when the file is renamed without telling Git. So, as I mentioned above, use the same command without "git".

Type the following command in the Git Bash and press enter:

mv <original_file_name> <new_file_name>

Renaming File Outside Git

Now, ideally, Git should have known that the file has been renamed if we had renamed it through Git. But, let's see the Git reaction when it encounters a file rename outside Git.

Renaming Files outside Git

It is very important to notice Git reaction here. Git has deleted a file called renaming.txt and added a new file toolsqa.txt. Both of them are not present in the staging area currently. This is different than what we saw above. There, Git mentioned the file has been renamed. Now we need to add these changes to the staging area.

Renaming Outisde Success

Now the Git has recognized the renaming and hence shows you the same. Commit these changes in the Git repository. You will notice something when you commit the changes.

Renamed Files Commit

The highlighted line says, rename renaming.txt -> toolsqa.txt (100%), By 100%, Git means that the previous file and the new file are exactly the same and there is no difference. It is called the confidence level. If you would have made any changes in the new file and then committed the changes, it would have shown less than 100%.

So, after covering these basics we are done with Alter Files in Git, we will go on to the next tutorial about Git Ignore.

Writing Good Commit Messages
Writing Good Commit Messages
Previous Article
Git Status Command in Git
Git Status Command in Git
Next Article

Similar Articles