跳至主要内容

将文件移动到新位置

你可以将文件移动到 GitHub 上的不同目录,也可以使用命令行。

平台导航

除了更改文件位置外,你还可以更新文件内容,或在同一提交中为其重新命名

在 GitHub 上将文件移动到新位置

提示

  • 如果您尝试移动您无权访问的仓库中的文件,我们将项目分叉到您的个人帐户,并在您提交更改后帮助您发送拉取请求到原始仓库。
  • 某些文件(例如图像)要求您通过命令行移动它们。有关更多信息,请参阅“将文件移动到新位置”。
  • 如果仓库有任何受保护的分支,则无法使用 GitHub 编辑或上传受保护分支中的文件。您可以使用 GitHub Desktop 将更改移动到新分支并提交它们。有关更多信息,请参阅“关于受保护分支”和“在 GitHub Desktop 中提交和审查项目更改”。
  1. 在您的仓库中,浏览到您要移动的文件。

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

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

    注意

    您可以选择使用github.dev 代码编辑器,而不是使用默认文件编辑器编辑和提交文件,方法是选择下拉菜单并单击**github.dev**。您也可以通过单击**GitHub Desktop** 克隆仓库并在本地通过 GitHub Desktop 编辑文件。

    Screenshot of a file. In the header, a downwards-facing triangle icon is outlined in dark orange.

  3. 在文件名字段中,使用以下准则更改文件名

    • 要将文件移动到子文件夹中,键入所需的文件夹名称,后跟/。您的新文件夹名称将成为导航面包屑中的新项目。
    • 要将文件移动到高于文件当前位置的目录中,请将光标置于文件名前端的字段开头,然后键入../以向上跳转一个完整目录级别,或键入backspace键以编辑父文件夹的名称。
  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. 在您的计算机上,将文件移动到克隆仓库时在本地计算机上创建的目录中的新位置。

  2. 打开终端终端Git Bash

  3. 使用git status检查旧文件和新文件的位置。

    $ git status
    > # On branch YOUR-BRANCH
    > # Changes not staged for commit:
    > #   (use "git add/rm <file>..." to update what will be committed)
    > #   (use "git checkout -- <file>..." to discard changes in working directory)
    > #
    > #     deleted:    /OLD-FOLDER/IMAGE.PNG
    > #
    > # Untracked files:
    > #   (use "git add <file>..." to include in what will be committed)
    > #
    > #     /NEW-FOLDER/IMAGE.PNG
    > #
    > # no changes added to commit (use "git add" and/or "git commit -a")
    
  4. 将文件暂存到本地仓库进行提交。这将删除旧位置的文件(或git rm),并将文件添加到新位置(或git add)。

    $ git add .
    # Adds the file to your local repository and stages it for commit.
    # To unstage a file, use 'git reset HEAD YOUR-FILE'.
    
  5. 使用git status检查已暂存以进行提交的更改。

    $ git status
    > # On branch YOUR-BRANCH
    > # Changes to be committed:
    > #   (use "git reset HEAD <file>..." to unstage)
    > #
    > #    renamed:    /old-folder/image.png -> /new-folder/image.png
    # Displays the changes staged for commit
    
  6. 提交已暂存到本地仓库的文件。

    $ git commit -m "Move file to new directory"
    # 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.
    
  7. 将本地仓库中的更改推送到 GitHub.com

    $ git push origin YOUR_BRANCH
    # Pushes the changes in your local repository up to the remote repository you specified as the origin