跳至主要内容

将文件移动到新位置

您可以在 GitHub 上或通过命令行将文件移动到其他目录。

平台导航

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

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

提示

  • 如果您尝试在没有访问权限的仓库中移动文件,我们会将项目 fork 到您的个人账户,并在您提交更改后帮助您向原始仓库发送拉取请求
  • 某些文件(例如图像)必须通过命令行移动。有关更多信息,请参阅将文件移动到新位置
  • 如果仓库存在受保护分支,则无法在受保护分支上编辑或上传文件。您可以使用 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. 在“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. 在您的电脑上,将文件移动到克隆仓库时在本地创建的目录中的新位置。

  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
    
© . This site is unofficial and not affiliated with GitHub, Inc.