git mv
renames a file or directory in a repository. How do I rename the Git repository itself?
Question
Answer
There are various possible interpretations of what is meant by renaming a Git repository: the displayed name, the repository directory, or the remote repository name. Each requires different steps to rename.
Displayed Name
Rename the displayed name (for example, shown by gitweb
):
- Edit
.git/description
to contain the repository's name. - Save the file.
Repository Directory
Typically (with exceptions for worktrees and submodules explained below), Git does not reference the name of the directory containing the repository, so we can simply rename or move it:
- Open a command prompt (or file manager window).
- Change to the directory that contains the repository directory (i.e., do not go into the repository directory itself).
- Rename the directory (for example, using
mv
from the command line or the F2 hotkey from a GUI).
Moving a repository that has worktrees
If you have created worktrees using the git worktree
subcommands from the repository that is to be renamed, then each worktree directory will contain a .git
file that contains
gitdir: {full-path-to-parent-repository}/.git/worktrees/{worktree-name}
So if you move the location of the parent repository, for each such worktree you will also need to edit its .git
file to change the parent path.
Moving a directory that is a worktree
Use the git worktree move
command from the parent repository.
Moving a repository that has submodules
Similarly to the worktree case, the submodule directory has a .git
file pointing to its parent. The parent also has a .git/modules/{submodule}/config
file which may contain absolute paths that need to be edited. See also this question.
Renaming a submodule
Use git mv
as discussed in this answer.
Corner cases involving both submodules and worktrees
Don't do that. If you must, read the docs for git worktree repair
and probably also the docs for submodules to understand how they are implemented.
Remote Repository
Rename a remote repository as follows:
Go to the remote host (for example, https://github.com/User/project).
Follow the host's instructions to rename the project (will differ from host to host, but usually Settings is a good starting point).
Go to your local repository directory (i.e., open a command prompt and change to the repository's directory).
Determine the new URL (for example,
[email protected]:User/project-new.git
)Set the new URL using Git:
git remote set-url origin [email protected]:User/project-new.git