How to Rename Files and Directories in a Git Repository

John Wachira Feb 02, 2024
  1. Rename Files and Directories in a Git Repository Using GitHub
  2. Rename Files and Directories in a Git Repository Using the Command Line
How to Rename Files and Directories in a Git Repository

In this article, we will discuss the renaming process in git. We use Git Rename to change the name of files and folders in our working directories.

The process of renaming involves the git mv command. This command accepts two arguments: target and destination.

We can use GitHub or the command line to rename files and folders. Let’s discuss the two approaches.

Rename Files and Directories in a Git Repository Using GitHub

This is arguably the most straightforward way to rename. Follow these steps to use GitHub;

  1. Open GitHub and Locate the file or folder you would like to rename in your repository.

    File to rename

  2. In our case, we have the repository, Delftscopetech and the file, Sample.txt. Click on the EDIT icon represented by a pen to rename the file.

    Renamed File

  3. Change the name of your file. In our case, from Sample.txt to Tutorial.txt. Scroll down to commit the changes.

    Committed

GitHub will save the renamed file, and you can pull the changes to your local repository.

Rename Files and Directories in a Git Repository Using the Command Line

Renaming with the command line uses the git mv command as shown below.

  1. Open Git bash and go to your repository. In our case, we run the git status -uno command to list our staged files.

    We want to rename the Example.txt file to Project.txt.

    To rename

  2. We will use the git mv command in the following context.

    $ git mv Example.txt Project.txt
    
  3. We run the git status command to check if we have renamed the file successfully.

    Renamed

  4. We can commit the change by running the following.

    $ git commit -m "Updated Name"
    

    Output:

    $ git commit -m "Updated Name"
    [main ba4c699] Updated Name
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 Project.txt
    

We can push the changes to the remote repository by running the git push origin branch_name command.

Follow the same steps to rename a folder.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Rename