Git – How to Rename a Git Repository

git

git mv renames a file or directory in a repository. How do I rename the Git repository itself?

Best 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):

  1. Edit .git/description to contain the repository's name.
  2. 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:

  1. Open a command prompt (or file manager window).
  2. Change to the directory that contains the repository directory (i.e., do not go into the repository directory itself).
  3. 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:

  1. Go to the remote host (for example, https://github.com/User/project).

  2. Follow the host's instructions to rename the project (will differ from host to host, but usually Settings is a good starting point).

  3. Go to your local repository directory (i.e., open a command prompt and change to the repository's directory).

  4. Determine the new URL (for example, [email protected]:User/project-new.git)

  5. Set the new URL using Git:

    git remote set-url origin [email protected]:User/project-new.git
    
Related Question