How to Rename Files and Directories in a Git Repository
- Rename Files and Directories in a Git Repository Using GitHub
- Rename Files and Directories in a Git Repository Using the Command Line
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;
-
Open GitHub and Locate the file or folder you would like to rename in your repository.
-
In our case, we have the repository,
Delftscopetech
and the file,Sample.txt
. Click on theEDIT
icon represented by a pen to rename the file. -
Change the name of your file. In our case, from
Sample.txt
toTutorial.txt
. Scroll down to commit the changes.
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.
-
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 toProject.txt
. -
We will use the
git mv
command in the following context.$ git mv Example.txt Project.txt
-
We run the
git status
command to check if we have renamed the file successfully. -
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.
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