How to rename a Directory or a File on the GitHub website

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. How to rename a Directory or a File on the GitHub website
  2. Rename a directory or a File on the GitHub website using github.dev
  3. Rename a GitHub directory using the Git command line

# How to rename a Directory or a File on the GitHub website

To rename a directory or a file on the GitHub website:

  1. Open the GitHub Repository in your browser.
  2. Navigate to the file, open it and click on the pencil icon to edit it.

click to edit file

  1. You can directly rename the file in the Edit file window.

directly rename file in edit file window

  1. If you end up renaming the file, you have to scroll down and click on the Commit changes button.

click commit changes button

  1. If you need to rename a directory, place your cursor at the beginning of the input field with the file's name and press Backspace <--.

rename directory backspace forward slash

  1. Once you press Backspace, you will be able to edit the name of the directory.

  2. You can move your cursor to the beginning of the previous directory and press Backspace again to edit its name.

  3. Once you have renamed the directory, add a forward slash / after its name.

  4. The forward slash / has to be added between the name of the directory and the name of the file (or between the names of directories).

You can also use the ../ prefix to go one directory up.

rename deeply nested directories

Make sure to place your cursor at the beginning of the input field before pressing Backspace.

When using this approach, you have to edit each file in the directory and rename the parent folder.

When the last file is removed, the old, empty directory is also removed.

However, this approach is not practical if your directory contains many files.

# Rename a directory or a File on the GitHub website using github.dev

An alternative approach is to:

  1. Open the GitHub page of your Git repository.
  2. Press . to open the github.dev editor.

press dot to open github dev editor

  1. The github.dev page will open a VS Code editor where you can rename your directory or files.

I have written a detailed guide on how to rename a file or a directory in VS Code but the easiest way is to:

  1. Select the directory in the left sidebar (Explorer).
  2. Right-click on the directory and select Rename or simply press F2.
  3. Type in the new name of the directory.

The same approach can be used to rename a file.

If you don't see the left sidebar, press Ctrl + B or (Cmd + B on macOS).

right click directory select rename

Once you rename the directory, you will see that you have uncommitted changes.

  1. You can click on the Source Control icon in the Activity Bar on the left.

click source control

You can also focus the Source Control view with a keyboard shortcut:

  • Ctrl + Shift + G (works on Windows, macOS and Linux)
  1. Type in a commit message.
  2. Click on the Commit & Push button.

enter commit message and click commit and push

  1. Once you click Commit & Push your changes will immediately get pushed to the main branch on GitHub.

  2. Open the GitHub page of your Git repository, refresh the page and verify that the directory has been renamed.

directory renamed successfully

This should be your preferred approach when the directory you need to rename contains many files.

Editing the path that points to each individual file manually is not practical for directories that contain more than 2-3 files.

# Rename a GitHub directory using the Git command line

An alternative approach is to use the git command line to rename a GitHub directory or a file.

  1. Open your terminal in your project's root directory and issue the following command.
shell
git ls-files

git ls files

The command shows information about files in the index and the working tree.

  1. Use the git mv command to rename the directory.
shell
git mv site/blog site/new-blog

git rename directory

  1. Run the git status command to verify you've made the correct changes.
shell
git status
  1. Stage and commit the changes.
shell
# stage the changes git add . # commit the changes git commit -m 'renamed directory' # Push to the remote branch git push origin master

Run the git ls-files command to verify everything looks as expected.

You can also open the GitHub page of your Git repository and refresh the page to check whether the directory has been renamed.

I have also written a detailed guide on how to rename a file or a folder in VS Code.

If you need to set VS Code as your default Git editor, difftool and mergetool, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.