从 Git 存储库中删除已从磁盘删除的多个文件

发布于 2024-08-04 12:09:27 字数 377 浏览 18 评论 0原文

我有一个 Git 存储库,已使用 rm 删除了四个文件(不是 git rm),我的 Git 状态如下所示

#    deleted:    file1.txt
#    deleted:    file2.txt
#    deleted:    file3.txt
#    deleted:    file4.txt

:我是否需要从 Git 中删除这些文件,而不必像这样手动检查并添加每个文件:

git rm file1 file2 file3 file4

理想情况下,我正在寻找与 git add 相同的方式工作的东西(如果可能的话) 。

I have a Git repo that I have deleted four files from using rm (not git rm), and my Git status looks like this:

#    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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

ま柒月 2024-08-11 12:09:27

对于 Git 1.x

$ git add -u

这告诉 git 自动暂存跟踪的文件——包括删除以前跟踪的文件。

对于 Git 2.0

要暂存整个工作树:

$ git add -u :/

仅暂存当前路径:

$ git add -u .

For Git 1.x

$ git add -u

This tells git to automatically stage tracked files -- including deleting the previously tracked files.

For Git 2.0

To stage your whole working tree:

$ git add -u :/

To stage just the current path:

$ git add -u .
月亮是我掰弯的 2024-08-11 12:09:27
git ls-files --deleted -z | xargs -0 git rm 

可能是你正在寻找的..它对我有用..

git ls-files --deleted -z | xargs -0 git rm 

might be what you are looking for.. it works for me..

相守太难 2024-08-11 12:09:27

您可以使用

git add -u

将删除的文件添加到暂存区,然后提交它们

git commit -m "Deleted files manually"

You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"
未央 2024-08-11 12:09:27

如果您只是运行:

git add -u

git 将更新其索引以了解您删除的文件实际上应该是下一次提交的一部分。然后您可以运行“git commit”来签入该更改。

或者,如果您运行:

git commit -a

它将自动获取这些更改(以及任何其他更改)并提交它们。

更新:如果您只想添加已删除的文件,请尝试:

git ls-files --deleted -z | xargs -0 git rm
git commit

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.

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
凉墨 2024-08-11 12:09:27

您可能正在寻找 -A:

git add -A

这与 git add -u 类似,但也添加新文件。这大致相当于 hg 的 addremove 命令(尽管移动检测是自动的)。

You're probably looking for -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).

我一直都在从未离去 2024-08-11 12:09:27

仅暂存已删除的文件:

for x in $(git status | grep deleted | awk '{print $2}'); do git rm $x; done

或者(xargs 方式):

git status | awk '/deleted/ {print $2}' | xargs git rm

您可以alias 您首选的命令集,方便以后使用。

To stage only the deleted files:

for x in $(git status | grep deleted | awk '{print $2}'); do git rm $x; done

Or (the xargs way):

git status | awk '/deleted/ {print $2}' | xargs git rm

You can alias your preferred command set for convenient later use.

裸钻 2024-08-11 12:09:27
git rm test.txt

在删除实际文件之前或之后。

git rm test.txt

Before or after you deleted the actual file.

原来分手还会想你 2024-08-11 12:09:27

通过使用 git-add 和 '--all' 或 '--update' 选项,你可能会得到比你想要的更多的东西。新的和/或修改的文件也将添加到索引中。当我想从 git 中删除已删除的文件而不触及其他文件时,我有一个 bash 别名设置:

alias grma='git ls-files --deleted -z | xargs -0 git rm'

所有已从文件系统中删除的文件都将添加到已删除的索引中。

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:

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.

别挽留 2024-08-11 12:09:27

这并不重要,但我不同意所选择的答案:

git add -u 

...如果工作树中的相应文件已被删除,将从索引中删除文件,但它也会暂存跟踪文件的修改后的新内容。

git rm $(git ls-files --deleted)

...另一方面,只会保存被跟踪的已删除文件。

所以我认为后者是更好的选择。

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.

So the latter in my view is the better option.

空城缀染半城烟沙 2024-08-11 12:09:27

如果这些是唯一的更改,您只需

git commit -a

提交所有更改即可。这将包括已删除的文件。

If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.

盛夏已如深秋| 2024-08-11 12:09:27
git ls-files --deleted | xargs git rm 

是仅添加已删除文件的最佳选择。

这是一些其他选项。

git add .  => Add all (tracked and modified)/new files in the working tree.

git add -u => Add all modified/removed files which are tracked.

git add -A => Add all (tracked and modified)/(tracked and removed)/new files in the working tree.

git commit -a -m "commit message" - Add and commit modified/removed files which are tracked.
git ls-files --deleted | xargs git rm 

is the best option to add only deleted files.

Here is some other options.

git add .  => Add all (tracked and modified)/new files in the working tree.

git add -u => Add all modified/removed files which are tracked.

git add -A => Add all (tracked and modified)/(tracked and removed)/new files in the working tree.

git commit -a -m "commit message" - Add and commit modified/removed files which are tracked.
温折酒 2024-08-11 12:09:27

git add -u

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

如果没有给出,则默认为“.”;换句话说,更新当前目录及其子目录中的所有跟踪文件。

git add -u

-u
--update
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.

If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

GRAY°灰色天空 2024-08-11 12:09:27

这个简单的解决方案对我来说效果很好:

git rm $(git ls-files --deleted)

That simple solution works fine for me:

git rm $(git ls-files --deleted)
安人多梦 2024-08-11 12:09:27
git rm $(git ls-files -d)

删除 git ls-files 命令列出的所有文件(-d 仅显示已删除的文件)。不适用于文件名或路径中带有空格的文件,但易于记住

git rm $(git ls-files -d)

Removes all files listed by the git ls-files command (-d show only deleted files). Doesn't work for files with spaces in the filename or path, but easy to remember

可爱咩 2024-08-11 12:09:27

如果您想将其添加到您的 .gitconfig 中,请执行以下操作:

[alias]
  rma = !git ls-files --deleted -z | xargs -0 git rm

然后您所要做的就是运行:

git rma

If you want to add it to your .gitconfig do this:

[alias]
  rma = !git ls-files --deleted -z | xargs -0 git rm

Then all you have to do is run:

git rma
独﹏钓一江月 2024-08-11 12:09:27
git ls-files --deleted -z | xargs -0 git rm --cached

这将删除 git 之前跟踪的所有已删除文件,并处理文件名中包含空格的情况。

根据您的 POSIX 变体,您可能需要使用 xargs -0 -r:这将导致 xargs 在通过管道传递 null 内容时正常退出。

编辑: --cached--deleted 标志串联使用,以防止意外删除尚未删除的文件。

git ls-files --deleted -z | xargs -0 git rm --cached

This will remove all deleted files that were previous tracked by git, as well as handle the case where your filenames have spaces in them.

Depending on your POSIX variant, you may need to use xargs -0 -r: this will cause xargs to gracefully exit when piped null content.

EDIT: --cached and --deleted flags are used in tandem to safeguard against accidentally deleting files that have not already been deleted.

三生殊途 2024-08-11 12:09:27

如前所述,

git add -u

暂存已删除的文件以进行删除,但也修改了文件以进行更新。

可以取消暂存修改的文件

git reset HEAD <path>

如果您希望保持提交的组织性和整洁性,则 。
注意:这也可以取消暂存已删除的文件,因此小心那些通配符。

As mentioned

git add -u

stages the removed files for deletion, BUT ALSO modified files for update.

To unstage the modified files you can do

git reset HEAD <path>

if you like to keep your commits organized and clean.
NOTE: This could also unstage the deleted files, so careful with those wildcards.

晒暮凉 2024-08-11 12:09:27

告诉命令自动暂存已修改和删除的文件,但您没有告诉 Git 的新文件不受影响:

-a
--全部

git add . && git commit -m -a "Your commit"

git add --all && git commit -m "Your commit"

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected:

-a
--all

git add . && git commit -m -a "Your commit"

or

git add --all && git commit -m "Your commit"
此刻的回忆 2024-08-11 12:09:27

即使您有很多文件需要处理,以下内容也将起作用:

git ls-files --deleted | xargs git rm

您可能还想提交评论。

有关详细信息,请参阅:有用的 Git 脚本

The following will work, even if you have a lot of files to process:

git ls-files --deleted | xargs git rm

You'll probably also want to commit with a comment.

For details, see: Useful Git Scripts

梦途 2024-08-11 12:09:27

请使用 -t 查看实际运行的命令

我刚刚调整了 Virender 答案以执行相同的操作:

git ls-files --deleted -z | xargs -t -0 git rm

Please use -t to see which command is actually being ran

I just tweaked Virender answer to do same:

git ls-files --deleted -z | xargs -t -0 git rm
未央 2024-08-11 12:09:27

git-add 的任何标志都不会只暂存已删除的文件;如果您修改的只是删除的文件,那么就可以了,但否则,您需要运行 git-status 并解析输出。

根据杰里米的回答,我得到的是:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. 获取文件状态。
  2. 对于已删除的文件,请隔离文件名。
  3. 删除所有以 #s 开头的行,以及其中包含“已删除”一词的状态行;我不记得它到底是什么,而且它已经不存在了,所以你可能需要针对不同的情况进行修改。我认为表达式分组可能是 GNU 特定的功能,因此如果您不使用 gnutils,则可能必须添加多个 grep -v 行。
  4. 将文件传递给 git rm。

现在将其粘贴到 shell 别名中...

None of the flags to git-add will only stage removed files; if all you have modified are deleted files, then you're fine, but otherwise, you need to run git-status and parse the output.

Working off of Jeremy's answer, this is what I got:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. Get status of files.
  2. For deleted files, isolate the name of the file.
  3. Remove all the lines that start with #s, as well as a status line that had the word "deleted" in it; I don't remember what it was, exactly, and it's not there any longer, so you may have to modify this for different situations. I think grouping of expressions might be a GNU-specific feature, so if you're not using gnutils, you may have to add multiple grep -v lines.
  4. Pass the files to git rm.

Sticking this in a shell alias now...

绝不放开 2024-08-11 12:09:27
git commit -m 'commit msg' $(git ls-files --deleted)

在我删除文件后,这对我有用。

git commit -m 'commit msg' $(git ls-files --deleted)

This worked for me after I had already deleted the files.

失眠症患者 2024-08-11 12:09:27

我需要同样的
并使用 git gui“阶段更改”按钮。
它还添加了所有。

在“阶段改变”之后,我做了“提交”......

所以我的工作目录又干净了。

I needed the same
and used git gui "stage changed" button.
it also adds all.

And after "stage changed" I made "commit" ...

so my working directory is clean again.

不乱于心 2024-08-11 12:09:27

您可以使用 git add -u仅暂存已删除的文件。

例如,如果您删除了文件 templates/*.tpl,则使用 git add -u templates/*.tpl

需要 -u 才能引用存储库中存在但工作目录中不再存在的文件。否则,git add 默认是在工作目录中查找文件,如果您指定已删除的文件,它将找不到它们。

You can use git add -u <filenames> to stage the deleted files only.

For example, if you deleted the files templates/*.tpl, then use git add -u templates/*.tpl.

The -u is required in order to refer to files that exist in the repository but no longer exist in the working directory. Otherwise, the default of git add is to look for the files in the working directory, and if you specify files you've deleted there, it won't find them.

怎会甘心 2024-08-11 12:09:27

添加系统别名以将已删除的文件暂存为命令 rm-all

UNIX
alias rm-all='git rm $(git ls-files --deleted)'

WINDOWS
doskey rm-all=bash -c "git rm $(git ls-files --deleted)"

注意

Windows 需要有 bash已安装。

Adding system alias for staging deleted files as command rm-all

UNIX
alias rm-all='git rm $(git ls-files --deleted)'

WINDOWS
doskey rm-all=bash -c "git rm $(git ls-files --deleted)"

Note

Windows needs to have bash installed.

爱本泡沫多脆弱 2024-08-11 12:09:27

(另一种变体)

我想删除所有已从磁盘文件中删除的文件,但从一个特定文件夹中删除,而其他文件夹保持不变。以下对我有用:

git ls-files --deleted  | grep <folder-name> | xargs git rm

(Yet another variation)

I wanted to delete all the already deleted from the disk files but from one specific folder, leaving the other folders untouched. The following worked for me:

git ls-files --deleted  | grep <folder-name> | xargs git rm
GRAY°灰色天空 2024-08-11 12:09:27

类似的东西

git status | sed -s "s/^.*deleted: //" | xargs git rm 

可以做到。

something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.

生来就爱笑 2024-08-11 12:09:27

对于 Visual Studio 项目,

'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' 

当删除的文件路径有空间时很有用

For visual studio project

'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' 

which is useful when the deleted file path has space

Smile简单爱 2024-08-11 12:09:27

从 Repo 中删除所有以 .log 结尾的文件,但不删除本地存储

git rm --cached $(git ls-files | grep "\.log$")

Deleting all files ending with .log from Repo but not local storage

git rm --cached $(git ls-files | grep "\.log$")

请爱~陌生人 2024-08-11 12:09:27

只是简单地

git add . && git commit -m "the message for commit" && git push

Just simply

git add . && git commit -m "the message for commit" && git push
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文