阅读(864) (0)

GitHub 使用命令行将文件移到新位置

2020-08-19 15:42:43 更新

您可以使用命令行在仓库中移动文件,从旧位置删除文件,然后将其添加到新位置。

许多文件可在 GitHub 上直接移动,但有些文件(如图像)需要从命令行移动。

此过程假设您已经:

  1. 在计算机上,将文件移至克隆仓库时在计算机上本地创建的目录中的新位置。

  1. 打开 Git Bash。

  1. 使用 git status 检查新旧文件位置。

   $ git status
   > # On branch your-branch
   > # Changes not staged for commit:
   > #   (use "git add/rm ..." to update what will be committed)
   > #   (use "git checkout -- ..." to discard changes in working directory)
   > #
   > #     deleted:    /old-folder/image.png
   > #
   > # Untracked files:
   > #   (use "git add ..." to include in what will be committed)
   > #
   > #     /new-folder/image.png
   > #
   > # no changes added to commit (use "git add" and/or "git commit -a")

  1. 将要提交的文件暂存到本地仓库。 这将从旧位置删除或 git rm 文件,并且添加或 git add 文件到新位置。

   $ git add .
   # Adds the file to your local repository and stages it for commit.
   # 要取消暂存文件,请使用 'git reset HEAD YOUR-FILE'。

  1. 使用 git status 检查为提交暂存的更改。

   $ git status
   > # On branch your-branch
   > # Changes to be committed:
   > #   (use "git reset HEAD ..." to unstage)
   > #
   > #    renamed:    /old-folder/image.png -> /new-folder/image.png
   # Displays the changes staged for commit

  1. 提交暂存在本地仓库中的文件。

   $ git commit -m "Move file to new directory"
   # Commits the tracked changes and prepares them to be pushed to a remote repository.
   # 要删除此提交并修改文件,请使用 'git reset --soft HEAD~1' 并再次提交和添加文件。

  1. 推送更改(本地仓库中)到 GitHub。

   $ git push origin your-branch
   # 将本地仓库中的更改推送到指定为源的远程仓库

延伸阅读