跳至主要内容

重命名文件

您可以在 GitHub 上直接或使用命令行重命名仓库中的任意文件。

平台导航

在 GitHub 上重命名文件

重命名文件还可以让您有机会将文件移动到新位置

提示

  • 如果您尝试在没有访问权限的仓库中重命名文件,我们会将项目 fork 到您的个人账户,并在您提交更改后帮助您向原始仓库发送拉取请求
  • 通过网页界面创建的文件名只能包含字母数字字符和连字符 (-)。如需使用其他字符,请在本地创建并提交文件,然后推送到仓库。
  • 某些文件(例如图片)必须通过命令行重命名。欲了解更多信息,请参阅使用命令行重命名文件
  1. 在您的仓库中,浏览至您想要重命名的文件。

  2. 在文件视图的右上角,点击以打开文件编辑器。

    Screenshot of a file. In the header, a button, labeled with a pencil icon, is outlined in dark orange.

  3. 在文件名字段中,将文件名更改为您想要的新文件名。您也可以同时更新文件内容。

    Screenshot showing a repository file open for editing in the web browser. The file name field is active and highlighted with a dark orange outline.

  4. 点击 提交更改...

  5. 在“Commit message”字段中,键入简短且有意义的提交信息,描述您对文件所做的更改。您可以在提交信息中将该提交归因于多个作者。欲了解更多信息,请参阅 创建拥有多位作者的提交

  6. 在提交信息字段下方,决定是将提交添加到当前分支还是新分支。如果您当前的分支是默认分支,建议为提交创建一个新分支,然后发起拉取请求。欲了解更多信息,请参阅 创建拉取请求

    Screenshot of a GitHub pull request showing a radio button to commit directly to the main branch or to create a new branch. New branch is selected.

  7. 点击 提交更改提议更改

使用命令行重命名文件

您可以使用命令行重命名仓库中的任何文件。

许多文件可以直接在 GitHub 上重命名,但某些文件(例如图片)必须通过命令行重命名。

此过程假设您已经

  1. 打开 终端终端Git Bash

  2. 将当前工作目录切换到本地仓库。

  3. 重命名文件,指定旧文件名和您想要的新文件名。这会将更改暂存以便提交。

    git mv OLD-FILENAME NEW-FILENAME
    
  4. 使用git status检查旧的和新的文件名。

    $ git status
    > # On branch YOUR-BRANCH
    > # Changes to be committed:
    > #   (use "git reset HEAD <file>..." to unstage)
    > #
    > #     renamed: OLD-FILENAME -> NEW-FILENAME
    > #
    
  5. 在本地仓库中提交已暂存的文件。

    $ git commit -m "Rename file"
    # Commits the tracked changes and prepares them to be pushed to a remote repository.
    # To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
    
  6. 推送更改至 GitHub.com。

    $ git push origin YOUR_BRANCH
    # Pushes the changes in your local repository up to the remote repository you specified as the origin
    
© . This site is unofficial and not affiliated with GitHub, Inc.