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
other people will keep a pointer to branch X (as origin/X
) in their repository until they run git remote prune origin
. it's left as a stale branch
when fetching/pulling they will get a second point to (newly created) branch Y (as origin/Y
)
branching and merging is not affected by this.
so, as long as all commits are still reachable from your branch, renaming is fine.
Best Answer
So, for this example:
Useful docs here: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#renaming-a-remote-repository