在 Git 中清理未暂存已删除文件的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10914496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:52:16  来源:igfitidea点击:

Easy way to clean up un-staged deleted files in Git

git

提问by Allyl Isocyanate

Often I move files in a git repository using my IDE or via the command line (not via git mv).

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

As a result I and end up with several unstaged files to be deleted on my next commit as below:

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

# 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

I typically will select all the deleted files and edit them in a text editor to produce like:

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

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

Which I then throw back into the console.

然后我把它扔回控制台。

Is there a way to do this without having to copy/paste?

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

回答by larsks

If I understand your question correctly, you want to commit your deletes...that is, you want to perform the equivalent of git rmfor allof the files that show up as deleted. git cleanwon't do this.

如果我没有理解你的问题,你想提交您的删除......也就是要执行相当于git rm所有该显示为删除的文件。 git clean不会这样做。

You can run git add -u:

你可以运行git add -u

Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

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

This will pick up all changes to tracked files, including deletes. So if you start with this:

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

# 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

Running git add -uwill get you to this:

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

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

And a commit at this point will do the right thing.

在这一点上提交将做正确的事情。

回答by Jeffrey Vandenborne

I usually do a commit of the files I made changes to.

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

If i'd have deleted redundant files, I would do the following after this:

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

git commit -a -m 'deleted redundant files'

回答by KurzedMetal

git add -uwill update the status of tracked files.

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