我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。


当前回答

答案来自Matt Frear是最有效的IMHO. 下面只是一个PowerShell脚本给那些在Windows只删除文件从他们的Git存储库,符合他们的排除列表。

# Get files matching exclusionsfrom .gitignore
# Excluding comments and empty lines
$ignoreFiles =  gc .gitignore | ?{$_ -notmatch  "#"} |  ?{$_ -match  "\S"} | % {
                    $ignore = "*" + $_ + "*"
                    (gci -r -i $ignore).FullName
                }
$ignoreFiles = $ignoreFiles| ?{$_ -match  "\S"}

# Remove each of these file from Git
$ignoreFiles | % { git rm $_}

git add .

其他回答

下面的命令系列将从 Git 索引中删除所有项目(不是工作目录或本地存储库),然后更新 Git 索引,同时遵守 Git 忽略。

首先:

git rm -r --cached .
git add .

然后:

git commit -am "Remove ignored files"

或者作为一个单线:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"

要总结一下:

您的应用程序正在寻找一个被忽略的文件 config-overide.ini 并使用它在承诺的文件 config.ini (或替代,寻找 ~/.config/myapp.ini,或 $MYCONFIGFILE) 承诺文件 config-sample.ini 并忽略文件 config.ini,有脚本或类似的复制文件如有必要。

我喜欢JonBrave的答案,但我有足够的工作目录,承诺 - 一个让我有点害怕,所以这里是我做的事情:

git config --global alias.exclude-ignored '!git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached &&  git ls-files -z --ignored --exclude-standard | xargs -0 git stage &&  git stage .gitignore && git commit -m "new gitignore and remove ignored files from index"'

打破它:

git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git ls-files -z --ignored --exclude-standard | xargs -0 git stage
git stage .gitignore
git commit -m "new gitignore and remove ignored files from index"

删除被忽略的文件从索引阶段.gitignore 和您刚刚删除的文件承诺

对于我来说,文件在历史上仍然可用,我首先需要清除添加删除文件的命令: https://gist.github.com/patik/b8a9dc5cd356f9f6f980

下面的例子结合了最后3个命令

git reset --soft HEAD~3
git commit -m "New message for the combined commit"

推破的承诺 如果承诺已推到远程:

git push origin +name-of-branch

答案来自Matt Frear是最有效的IMHO. 下面只是一个PowerShell脚本给那些在Windows只删除文件从他们的Git存储库,符合他们的排除列表。

# Get files matching exclusionsfrom .gitignore
# Excluding comments and empty lines
$ignoreFiles =  gc .gitignore | ?{$_ -notmatch  "#"} |  ?{$_ -match  "\S"} | % {
                    $ignore = "*" + $_ + "*"
                    (gci -r -i $ignore).FullName
                }
$ignoreFiles = $ignoreFiles| ?{$_ -match  "\S"}

# Remove each of these file from Git
$ignoreFiles | % { git rm $_}

git add .