首页 > 解决方案 > 如何从 git 的所有分支中删除名为 .css 的文件

问题描述

我正在使用一个 git 存储库,该存储库中以某种方式命名.css了一个文件。这在 Windows 中签出时会导致错误(但在 Linux 中可以正常工作)。

如何从git的所有分支中删除此文件,而不会意外从所有分支中删除所有 css 文件?

标签: gitgit-branch

解决方案


这是您如何为单个分支执行此操作的方法,因为“所有分支”相当模糊,我将自动化/迭代部分留在所有分支上,让您按照自己的喜好进行操作(如果您没有的话,我建议您手动进行许多分支都有这个有问题的文件)

# First checkout the branch - WARNING - Note I use
# --force here to make sure `git status` is clean. 
# I'm relying on it later when doing `git add -A`.
# The --force flag will delete any uncommited local changes,
# so make sure you don't have any of those before running
# the following command
git checkout --force my_branch

# Now delete the `*.css` file. Note the single quotes, they
# are crucial to prevent the shell from doing expansion on the asterisk:
rm -- '*.css'

# Add "all" the changes (The deletion of the file should be the only change):
git add -A

# Now commit
git commit -m 'Removed evil *.css file'

# And push
git push origin my_branch

我没有故意使用git rm(它结合了删除和git add一个命令),因为我无法让它忽略*作为一个字符,它试图变得聪明并删除.css项目中的所有文件。编辑:有关在 git 命令中指定文字星号的方法,请参阅torek 的答案


推荐阅读