Git

What are the Steps to Rename a File in Git?

Git is the versioning control system that is utilized for source code management. It is widely used for tracking modification in the development project source code files and permits multiple developers to work together. Moreover, when users move or rename a file, it cannot be observed whether the file is moved from one place to another or not. However, the new file name will be replaced with the old one, and the old file name will be deleted.

This tutorial will provide the solution for renaming a file in Git.

What are the Steps to Rename a File in Git?

To rename a file in Git, check out the below-provided instructions:

  • Navigate toward the root directory.
  • List the repository content using the “ls” command.
  • Run the “git mv” command along with the file name which needs to be renamed and the new name of the file.

Step 1: Move to the Local Repository

Initially, run the “cd” Git command with the specified root directory path and move toward it:

cd "C:\Users\user\Git\demo1"

 

Step 2: List Content

Execute the “ls” command to list the content of the current working repository:

ls

 

From the below-given output we have selected the “file3.py” file for further processing:

Step 3: Check Status

Now, utilize the “git status” command to check the current state of the working repository:

git status

 

It can be observed that currently we have nothing to commit, and the working area is clean:

Step 4: Rename a File

Execute the “git mv” command along with the file name which needs to rename and then specify a new name for the file according to your desire:

git mv file3.py myfile3.html

 

Step 5: Verify the Renamed File

To ensure the renamed file by running the provided command:

ls

 

The below-given output indicates that the file is renamed successfully:

Step 6: Check Status

Run the “git status” command to view the status of the current working directory:

git status

 

As you can see, the “file3.py” has been renamed successfully:

That’s it! We have described the way to rename a file in Git.

Conclusion

To rename a file in Git, first, navigate to the root directory. Next, list content with the help of the “ls” command. After that, run the “git mv” command along with the file name which needs to be renamed and specify the new file name. This tutorial illustrated the method for renaming the file in Git.

About the author

Hafsa Javed