43

我经常使用我的 IDE 或通过命令行(而不是通过 git mv)在 git 存储库中移动文件。

结果,我最终在下一次提交时删除了几个未暂存的文件,如下所示:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.html
#
# 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:    css/bootstrap.css
#   deleted:    css/bootstrap.min.css
#   deleted:    img/glyphicons-halflings-white.png
#   deleted:    img/glyphicons-halflings.png
#   deleted:    js/bootstrap.js
#   deleted:    js/bootstrap.min.js

我通常会选择所有已删除的文件并在文本编辑器中编辑它们以生成如下内容:

git rm  css/bootstrap.css
git rm  css/bootstrap.min.css
git rm  img/glyphicons-halflings-white.png
git rm  img/glyphicons-halflings.png
git rm  js/bootstrap.js
git rm  js/bootstrap.min.js

然后我把它扔回控制台。

有没有办法做到这一点而不必复制/粘贴?

4

3 回答 3

78

如果我正确理解您的问题,您希望提交删除...也就是说,您希望对显示为已删除 git rm所有文件执行等效的操作。git clean不会这样做。

您可以运行git add -u

仅与索引中已跟踪的文件而不是工作树匹配。这意味着它永远不会暂存新文件,但它将暂存已修改的跟踪文件的新内容,并且如果工作树中的相应文件已被删除,它将从索引中删除文件。

这将获取对跟踪文件的所有更改,包括删除。所以如果你从这个开始:

# 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:    file2
#   deleted:    file3
#   deleted:    file4
#   deleted:    file5

跑步git add -u会让你做到这一点:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    file2
#   deleted:    file3
#   deleted:    file4
#   deleted:    file5

此时的提交将做正确的事情。

于 2012-06-06T12:53:22.830 回答
4

我通常会提交我更改的文件。

如果我删除了多余的文件,我会在此之后执行以下操作:

git commit -a -m 'deleted redundant files'
于 2012-06-06T12:56:34.927 回答
3

git add -u将更新跟踪文件的状态。

于 2012-06-06T12:53:33.647 回答