Git

How to Properly Rename a Directory in a Git Repository

Renaming Git sub-repositories and project files in Git is an essential part of project management. As many developers work on the same project and for a better understanding of the project’s file and directory, Git users use relevant names with functions or features. For this purpose, occasionally, Git users are required to rename the file or Git directory.

This blog will illustrate the procedure to rename a directory in a Git repository.

How to Rename a Git Directory in a Repository?

To rename a directory, first, view all the Git directories by executing the β€œls” command. Then, choose the directory you want to rename and utilize the β€œ$ git mv <source-directory-name> < new-directory-name>” command.

For this purpose, look at the provided instructions.

Step 1: Open Git Bash Terminal
First, launch the β€œGit Bash” command line terminal from the Startup menu:

Step 2: Go to Git Repository
Next, go to the required Git repository:

$ cd "C:\Git"

Step 3: View All Files and Directories
View all files and directories of the Git repository through the provided command:

$ ls

Alternatively, the user can utilize the β€œgit ls-file” command to view the directories file in detail:

$ git ls-file

Step 4: Rename Directory
Now, rename the Git directory by utilizing the β€œgit mv” command. Also, specify the old directory name and new directory name along with the command:

$ git mv test features

Using the above command, we will rename the β€œtest” directory as β€œfeatures”:

Again, view all files and directories of the repository to verify if the directory is renamed or not:

$ ls

You can see that we have successfully renamed the directory:

Step 5: Add Changes to Staging Area
Move the untracked changes to the tracking index (staging area) by executing the mentioned command:

$ git add .

Check out the repository state to verify if the changes are added to the staging environment or not:

$ git status

You can see the directory renamed changes are added to the staging index:

Step 6: Commit the Changes
Next, commit the tracked changes using the β€œgit commit” command. This will properly save the directory in the local repository with the new name:

$ git commit -m "rename Git directory"

Check the repository logs to verify if the changes or committed or not:

$ git log

It can be observed that the changes are committed successfully:

We have taught you the procedure to properly rename the Git directory in a Git repository.

Conclusion

To rename the Git directory in a Git repository, first, open the repository and choose the directory you want to rename. After that, utilize the β€œgit mv <source-dir-name> <new-dir-name>” command to rename the directory. Now, add the untracked changes to the tracking index and then, commit these changes using the β€œgit commit” command. This write-up has demonstrated the method for renaming Git directories in the Git repository.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.