简体   繁体   English

如何重命名 Git 存储库?

[英]How do I rename a Git repository?

git mv renames a file or directory in a repository. git mv重命名存储库中的文件或目录。 How do I rename the Git repository itself?如何重命名 Git 存储库本身?

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.重命名 Git 存储库的含义有多种可能的解释:显示的名称、存储库目录或远程存储库名称。 Each requires different steps to rename.每个都需要不同的步骤来重命名。

Displayed Name显示名称

Rename the displayed name (for example, shown by gitweb ):重命名显示的名称(例如,由gitweb显示):

  1. Edit .git/description to contain the repository's name.编辑.git/description以包含存储库的名称。
  2. Save the file.保存文件。

Repository Directory存储库目录

Git does not reference the name of the directory containing the repository, as used by git clone master child , so we can simply rename it: Git 不引用包含存储库的目录的名称,正如git clone master child使用的那样,因此我们可以简单地重命名它:

  1. Open a command prompt (or file manager window).打开命令提示符(或文件管理器窗口)。
  2. Change to the directory that contains the repository directory (ie, 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).重命名目录(例如,使用命令行中的mv或 GUI 中的F2热键)。

Remote Repository远程存储库

Rename a remote repository as follows:重命名远程存储库,如下所示:

  1. Go to the remote host (for example, https://github.com/User/project ).转到远程主机(例如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 (ie, open a command prompt and change to the repository's directory).转到您的本地存储库目录(即,打开命令提示符并切换到存储库的目录)。
  4. Determine the new URL (for example, git@github.com:User/project-new.git )确定新的 URL(例如git@github.com:User/project-new.git
  5. Set the new URL using Git:使用 Git 设置新 URL:

     git remote set-url origin git@github.com:User/project-new.git

A Git repository doesn't have a name. Git 存储库没有名称。 You can just rename the directory containing your worktree if you want.如果需要,您可以重命名包含工作树的目录。

With Github As Your Remote使用 Github 作为您的遥控器

Renaming the Remote Repo on Github重命名 Github 上的远程仓库

Regarding the remote repository, if you are using Github or Github Enterprise as the server location for saving/distributing your repository remotely, you can simply rename the repository directly in the repo settings.关于远程存储库,如果您使用 Github 或 Github Enterprise 作为远程保存/分发存储库的服务器位置,您可以直接在存储库设置中简单地重命名存储库。

From the main repo page, the settings tab is on the right, and the repo name is the first item on the page:从主 repo 页面,设置选项卡在右侧,repo 名称是页面上的第一项:

在此处输入图像描述

Github will redirect requests to the new URL Github 会将请求重定向到新的 URL

One very nice feature in Github when you rename a repo, is that Github will save the old repo name and all the related URLs and redirect traffic to the new URLs .当您重命名存储库时,Github 中的一个非常好的功能是, Github 将保存旧的存储库名称和所有相关的 URL,并将流量重定向到新的 URL Since your username/org and repo name are a part of the URL, a rename will change the URL.由于您的用户名/组织和存储库名称是 URL 的一部分,因此重命名将更改 URL。

Since Github saves the old repo name and redirects requests to the new URLs, if anyone uses links based on the old repo name when trying to access issues, wiki, stars, or followers they will still arrive at the new location on the Github website.由于 Github 保存旧的 repo 名称并将请求重定向到新的 URL,如果有人在尝试访问问题、wiki、stars 或关注者时使用基于旧 repo 名称的链接,他们仍然会到达 Github 网站上的新位置。 Github also redirects lower level Git commands like git clone , git fetch , etc. Github 还重定向较低级别的 Git 命令,如git clonegit fetch等。

More information is in the Github Help for Renaming a Repo更多信息在Github Help for Renameing a Repo 中

Renaming the Local Repo Name重命名本地仓库名称

As others have mentioned, the local "name" of your repo is typically considered to be the root folder/directory name, and you can change that, move, or copy the folder to any location and it will not affect the repo at all.正如其他人所提到的,您的存储库的本地“名称”通常被认为是根文件夹/目录名称,您可以更改、移动或将文件夹复制到任何位置,它根本不会影响存储库。

Git is designed to only worry about files inside the root folder. Git 被设计为只关心根文件夹内的文件。

PRJ0.git重命名为PROJ1.git ,然后编辑位于项目的.git/config文件中的 URL 变量。

In a new repository, for instance, after a $ git init , the .git directory will contain the file .git/description.例如,在一个新的存储库中,在$ git init之后,.git 目录将包含文件 .git/description。

Which looks like this:看起来像这样:

Unnamed repository; edit this file 'description' to name the repository.

Editing this on the local repository will not change it on the remote.在本地存储库上编辑它不会在远程更改它。

To rename any repository of your GitHub account:要重命名 GitHub 帐户的任何存储库:

  1. Go to that particular repository which you want to rename转到您要重命名的特定存储库
  2. Navigate to the settings tab导航到设置选项卡
  3. There, in the repository name section, type the new name you want to put and click Rename在那里,在存储库名称部分,键入您要放置的新名称,然后单击重命名

If you are using GitLab or GitHub, then you can modify those files graphically.如果您使用的是 GitLab 或 GitHub,则可以以图形方式修改这些文件。

Using GitLab使用 GitLab

Go to your project Settings .转到您的项目设置 There you can modify the name of the project and most importantly you can rename your repository (that's when you start getting in the danger section).在那里你可以修改项目的名称,最重要的是你可以重命名你的存储库(那是你开始进入危险部分的时候)。

Once this is done, local clients configurations must be updated using完成此操作后,必须使用更新本地客户端配置

git remote set-url origin sshuser@gitlab-url:GROUP/new-project-name.git

If you meant renaming your repository, go to your repository and click "admin", then rename.如果您的意思是重命名您的存储库,请转到您的存储库并单击“管理员”,然后重命名。

Once you see the red box warning you about some sky-fallingness and other things, go read this question .一旦你看到红色框警告你一些天空坠落和其他事情, 去阅读这个问题

If you are in Eclipse and have installed Egit then you can rename the repository that contains a project by doing the following:如果您在 Eclipse 中并且已经安装了 Egit,那么您可以通过执行以下操作重命名包含项目的存储库:

1) In Eclipse : Close all projects that are in the repository. 1)在 Eclipse中:关闭存储库中的所有项目。

2) In the file system : Locate the directory/folder that contains the repository. 2)在文件系统中:找到包含存储库的目录/文件夹。

3) In the file system : Rename the directory/folder that contains the repository. 3)在文件系统中:重命名包含存储库的目录/文件夹。

4) In the file system : Open the directory/folder that contains the repository and rename the project directory/folder of any project you intend to rename so that it will match the new name of the project. 4)在文件系统中:打开包含存储库的目录/文件夹,并重命名您要重命名的任何项目的项目目录/文件夹,使其与项目的新名称匹配。 (This is not required but it gives consistency between the project name in Eclipse and the project directory/folder in the repository.) (这不是必需的,但它使 Eclipse 中的项目名称与存储库中的项目目录/文件夹保持一致。)

5) In Eclipse : Delete all projects that are in the repository but be sure to NOT check the 'Delete the contents from the file system' checkbox. 5)在 Eclipse中:删除存储库中的所有项目,但一定不要选中“从文件系统中删除内容”复选框。 (The project should no longer contain the correct location of the contents of the file system so the data could not be deleted in any case but it is better to be safe than sorry.) (该项目不应再包含文件系统内容的正确位置,因此无论如何都无法删除数据,但安全总比抱歉好。)

6) In Eclipse : From the Menu select the File|Import... option. 6)在 Eclipse中:从菜单中选择 File|Import... 选项。

7) In Eclipse : In dialog box open the 'Git' folder, select 'Projects from Git' and click 'Next'. 7)在 Eclipse中:在对话框中打开“Git”文件夹,选择“Projects from Git”并单击“下一步”。

8) In Eclipse : In dialog box select 'Local' and click 'Next'. 8)在 Eclipse中:在对话框中选择“本地”并单击“下一步”。

9) In Eclipse : In dialog box click the 'Add...' button. 9)在 Eclipse中:在对话框中单击“添加...”按钮。

10) In Eclipse : In dialog box make sure the check box next to the repository is checked and click 'Finish'. 10)在 Eclipse中:在对话框中确保选中存储库旁边的复选框,然后单击“完成”。

11) In Eclipse : In dialog box select the repository and click 'Next'. 11)在 Eclipse中:在对话框中选择存储库并单击“下一步”。

12) In Eclipse : In dialog box select the 'Import existing projects' radio button, select the "Working Directory" and click 'Next'. 12)在 Eclipse中:在对话框中选择“导入现有项目”单选按钮,选择“工作目录”并单击“下一步”。

13) In Eclipse : In dialog box check the check box next to the projects you want to work on and click 'Finish'. 13)在 Eclipse中:在对话框中选中您要处理的项目旁边的复选框,然后单击“完成”。

14) In Eclipse : Rename any the projects that are in the repository if so desired. 14)在 Eclipse中:如果需要,重命名存储库中的任何项目。 (For consistency between Eclipse and the file system give them the same name as the project directory/folder inside the repository directory/folder.) (为了 Eclipse 和文件系统之间的一致性,给它们与存储库目录/文件夹中的项目目录/文件夹相同的名称。)

  • On the server side, just rename the repository with the mv command as usual:在服务器端,只需像往常一样使用mv命令重命名存储库:

     mv oldName.git newName.git
  • Then on the client side, change the value of the [remote "origin"] URL into the new one:然后在客户端,将[remote "origin"] URL 的值更改为新的:

     url=example.com/newName.git

It worked for me.它对我有用。

Git itself has no provision to specify the repository name. Git 本身没有规定指定存储库名称。 The root directory's name is the single source of truth pertaining to the repository name.根目录的名称是与存储库名称有关的唯一事实来源。

The .git/description though is used only by some applications, like GitWeb . .git/description虽然仅由某些应用程序使用,例如GitWeb

For Amazon AWS codecommit users,对于Amazon AWS codecommit用户,

aws codecommit update-repository-name --old-name MyDemoRepo --new-name MyRenamedDemoRepo

Reference: here参考: 这里

To be simple, just remove that Eclipse Project (not choose content on disk), then re-import the project again.简单来说,只需删除那个 Eclipse 项目(不要选择磁盘上的内容),然后再次重新导入项目。

Eclipse will identify the project connected to Git and put it in Git perspective view. Eclipse 将识别连接到 Git 的项目并将其放在 Git 透视图中。

It works like a charm.它就像一个魅力。

The main name change is here (img 1), but also change readme.md (img 2)主要改名在这里(img 1),还要改readme.md(img 2)

在此处输入图像描述

在此处输入图像描述

  1. Go to the remote host (eg, https://github.com/<User>/<Project>/ ).转到远程主机(例如, https ://github.com/<User>/<Project>/)。
  2. Click tab Settings .单击选项卡设置
  3. Rename under Repository name (and press button Rename ).存储库名称下重命名(并按下按钮Rename )。

Open git repository on browser, got to "Setttings", you can see rename button.在浏览器上打开 git 存储库,进入“设置”,您可以看到重命名按钮。

Input new "Repository Name" and click "Rename" button.输入新的“存储库名称”,然后单击“重命名”按钮。

Have you try changing your project name in package.json and execute command git init to reinitialize the existing Git, instead?您是否尝试更改package.json中的项目名称并执行命令git init来重新初始化现有的 Git?

Your existing Git history will still exist.你现有的 Git 历史仍然存在。

This is an extremely simple solution, though some may consider it "inelegant" or a "hack", and it belies my git inexpertise.这是一个非常简单的解决方案,尽管有些人可能认为它“不优雅”或“黑客”,它掩盖了我的 git 不专业。

  1. Verify that your local repo has everything committed and pushed (to remote origin).验证您的本地存储库是否已提交并推送所有内容(到远程源)。
  2. Go to the website of the remote host (for example, https://github.com/User/project-original-name ).访问远程主机的网站(例如https://github.com/User/project-original-name )。 Follow the host's instructions to rename the repo (will differ from host to host, but usually Settings is a good starting point).按照主机的说明重命名 repo(因主机而异,但通常设置是一个很好的起点)。 (For the purposes of this guide, suppose you renamed your repo to "project-new-name".) (出于本指南的目的,假设您将 repo 重命名为“project-new-name”。)
  3. Locally remove your whole repo (eg, rm -r project-original-name ).在本地删除您的整个 repo(例如, rm -r project-original-name )。
  4. Do a "fresh checkout": git clone https://github.com/User/project-new-name做一个“新鲜结帐”: git clone https://github.com/User/project-new-name

NOTE: If another user of the repo doesn't follow these instructions, and just does a pull in the future, I have no idea what effect this will have.注意:如果 repo 的另一个用户不遵循这些说明,并且只是在将来进行拉动,我不知道这会产生什么影响。

@ Parison @帕里森

Server Side: mv oldName.git newName.git
Client Side: ./.git/config change [remote "origin"] | url to newName.git

It's ambiguous what you mean by "renaming a git repository itself", but one interpretation of that is changing the URL of a remote git repository. “重命名 git 存储库本身”的意思是模棱两可的,但一种解释是更改远程 git 存储库的 URL。

git remote set-url origin url

https://www.commands.dev/workflows/change_url_of_remote_git_repository https://www.commands.dev/workflows/change_url_of_remote_git_repository

I bookmarked ^ page, it tells me the command and allows me play around with the parameters.我为 ^ 页面添加了书签,它告诉我命令并允许我使用参数。 Super useful, IMO.超级有用,海事组织。

This worked for me on Windows 10, via the command line:这在 Windows 10 上对我有用,通过命令行:

git checkout <oldname>
git branch -m <newname>

From How To Rename a Local and Remote Git Branch如何重命名本地和远程 Git 分支

This was a local-only repository (not on any remotes).这是一个仅限本地的存储库(不在任何远程)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM