从已从磁盘中删除的 Git 存储库中删除多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/492558/
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
Removing multiple files from a Git repo that have already been deleted from disk
提问by Codebeef
I have a Git repo that I have deleted four files from using rm
(notgit rm
), and my Git status looks like this:
我有一个 Git 存储库,我已经删除了使用的四个文件rm
(不是git rm
),我的 Git 状态如下所示:
# deleted: file1.txt
# deleted: file2.txt
# deleted: file3.txt
# deleted: file4.txt
How do I remove these files from Git without having to manually go through and add each file like this:
如何从 Git 中删除这些文件而无需手动添加每个文件,如下所示:
git rm file1 file2 file3 file4
Ideally, I'm looking for something that works in the same way that git add .
does, if that's possible.
理想情况下,git add .
如果可能的话,我正在寻找以相同方式工作的东西。
回答by carl
For Git 1.x
对于 Git 1.x
$ git add -u
This tells git to automatically stage tracked files -- including deleting the previously tracked files.
这告诉 git 自动暂存跟踪的文件——包括删除以前跟踪的文件。
For Git 2.0
对于 Git 2.0
To stage your whole working tree:
要暂存整个工作树:
$ git add -u :/
To stage just the current path:
仅暂存当前路径:
$ git add -u .
回答by Varinder
git ls-files --deleted -z | xargs -0 git rm
might be what you are looking for.. it works for me..
可能是你在找什么..它对我有用..
回答by Cody Caughlan
You can use
您可以使用
git add -u
To add the deleted files to the staging area, then commit them
将删除的文件添加到暂存区,然后提交它们
git commit -m "Deleted files manually"
回答by Emil Sit
If you simply run:
如果你只是运行:
git add -u
git will update its index to know that the files that you've deleted should actually be part of the next commit. Then you can run "git commit" to check in that change.
git 将更新其索引以了解您删除的文件实际上应该是下一次提交的一部分。然后您可以运行“git commit”来检查该更改。
Or, if you run:
或者,如果您运行:
git commit -a
It will automatically take these changes (and any others) and commit them.
它将自动接受这些更改(以及任何其他更改)并提交它们。
Update: If you only want to add deleted files, try:
更新:如果您只想添加已删除的文件,请尝试:
git ls-files --deleted -z | xargs -0 git rm
git commit
回答by Dustin
You're probably looking for -A:
您可能正在寻找 -A:
git add -A
this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove
command (although the move detection is automatic).
这类似于 git add -u,但也会添加新文件。这大致相当于 hg 的addremove
命令(尽管移动检测是自动的)。
回答by Saeb Amini
回答by Peaker
git rm test.txt
Before or after you deleted the actual file.
在删除实际文件之前或之后。
回答by zobie
By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:
通过将 git-add 与 '--all' 或 '--update' 选项一起使用,您可能会得到比您想要的更多的东西。新的和/或修改过的文件也将被添加到索引中。当我想从 git 中删除已删除的文件而不触及其他文件时,我有一个 bash 别名设置:
alias grma='git ls-files --deleted -z | xargs -0 git rm'
All files that have been removed from the file system are added to the index as deleted.
已从文件系统中删除的所有文件都将作为已删除文件添加到索引中。
回答by Stephan Tual
Not that it really matters, but I disagree with the chose answer:
并不是说这真的很重要,但我不同意所选择的答案:
git add -u
... will remove files from the index if the corresponding files in the working tree have been removed, but it will also stage the modified new contents of tracked files.
...如果工作树中的相应文件已被删除,将从索引中删除文件,但它也会暂存跟踪文件的修改后的新内容。
git rm $(git ls-files --deleted)
... on the other hand will only rm the deleted files that were tracked.
...另一方面,只会 rm 被跟踪的已删除文件。
So the latter in my view is the better option.
所以在我看来后者是更好的选择。
回答by SpoonMeiser
If those are the only changes, you can simply do
如果这些是唯一的变化,你可以简单地做
git commit -a
to commit all changes. That will include deleted files.
提交所有更改。这将包括已删除的文件。