我正在学习 Git。在我机器上的测试仓库中有 2 个分支:
master 和testing我确实检查testing branch并删除了文件系统上的一些文件:
helpers.php registry.class.php
... session.class.php simpleCrud.php
Run Code Online (Sandbox Code Playgroud)
然后我跑 git add .
testing分支中删除这些已删除的文件并允许我提交该更改git status(见下图)显示它们不可用于git commit -m "Deleted some files from testing branch"
您的文件删除更改没有显示在 git 索引中的原因是因为您在没有通知 git 的情况下自行删除了文件。
现在有两种方法可以解决这个问题。
选项1:
使用该git add -u <FILES>命令使索引中跟踪的文件反映工作树中的更改。Git 会检测您的工作树中的文件删除,并更新索引以对应此状态。
# Example command to update the git index to
# reflect the state of the files in the work-tree
# for the two files named helpers.php and registry.class.php
git add -u helpers.php registry.class.php
Run Code Online (Sandbox Code Playgroud)
选项 2:
要删除文件,而不是使用shell 中的del或rm命令手动删除它,您可以直接要求 git 删除并使用git rm命令记下索引中的更改。请注意,即使您自己已经删除了文件(如您提到的情况),也可以执行此命令。
# Example command to remove two files named
# helpers.php and registry.class.php
git rm helpers.php registry.class.php
Run Code Online (Sandbox Code Playgroud)
使用上述任一选项,您应该会看到您的状态命令应自动指示文件已在暂存区中删除:
git status
# On branch testing
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: helper.php
# deleted: registry.class.php
#
Run Code Online (Sandbox Code Playgroud)
然后您应该能够使用该commit命令提交更改。
git commit -m "Deleted some files from testing branch"
Run Code Online (Sandbox Code Playgroud)