我正在通过阅读和在我的机器上玩测试回购来学习Git。
有一件事令我困惑。我有2个分支。 master
和testing
我签出testing branch
并删除文件系统上的一些文件。然后我运行git add .
我希望它将这些文件记录为从testing
分支中删除,但是在下图中,您可以看到它们在我运行git commit -m "Deleted some files from testing branch"
我希望删除这些文件并允许我提交更改,然后当我再次检出master
时,它应该显示现有的文件。
有人可以告诉我我做错了什么,以及如何做我想做的事情?
答案 0 :(得分:4)
您的文件删除更改未显示在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
选项2:
要删除文件,而不是使用shell中的del
或rm
命令手动删除文件,您可以直接让git删除并记下索引中使用{{3命令。请注意,即使您已经自己删除了文件(例如您提到的情况),也可以执行此命令。
# Example command to remove two files named
# helpers.php and registry.class.php
git rm helpers.php registry.class.php
使用上述任一选项,您应该看到您的状态命令应该自动指示文件已在暂存区域中删除:
git status
# On branch testing
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: helper.php
# deleted: registry.class.php
#
然后,您应该能够使用commit
命令提交更改。
git commit -m "Deleted some files from testing branch"
答案 1 :(得分:3)
您可以使用-u
标记来暂存它们。
例如:git add -u .