跳至主要内容

重命名文件

您可以直接在 GitHub 中或使用命令行重命名存储库中的任何文件。

平台导航

在 GitHub 上重命名文件

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

提示

  • 如果你尝试重命名一个你无权访问的仓库中的文件,我们将把项目分叉到你的个人账户,并在你提交更改后帮助你向原始仓库发送拉取请求
  • 通过 Web 界面创建的文件名只能包含字母数字字符和连字符 (-)。要使用其他字符,请在本地创建并提交文件,然后将它们推送到仓库。
  • 某些文件(例如图像)需要你通过命令行重命名。有关更多信息,请参阅“使用命令行重命名文件”。
  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. 在“提交消息”字段中,键入一条简短且有意义的提交消息,描述你对文件所做的更改。你可以在提交消息中将提交归因于多个作者。有关更多信息,请参阅“创建具有多个作者的提交”。

  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