Last updated: Apr 5, 2024
Reading time·4 min
To rename a directory or a file on the GitHub website:
Backspace <--
.Once you press Backspace
, you will be able to edit the name of the
directory.
You can move your cursor to the beginning of the previous directory and press
Backspace
again to edit its name.
Once you have renamed the directory, add a forward slash /
after its name.
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.
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.
An alternative approach is to:
.
to open the github.dev
editor.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:
F2
.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).
Once you rename the directory, you will see that you have uncommitted changes.
You can also focus the Source Control view with a keyboard shortcut:
Ctrl
+ Shift
+ G
(works on Windows, macOS and Linux)Once you click Commit & Push your changes will immediately get pushed to the main branch on GitHub.
Open the GitHub page of your Git repository, refresh the page and verify that the directory has been renamed.
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.
An alternative approach is to use the git
command line to rename a GitHub
directory or a file.
git ls-files
The command shows information about files in the index and the working tree.
git mv
command to rename the directory.git mv site/blog site/new-blog
git status
command to verify you've made the correct changes.git status
# 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.
You can learn more about the related topics by checking out the following tutorials: