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


当前回答

为文件 / 文件夹执行下列步骤:

删除文件:

需要添加该文件到.gitignore. 需要删除该文件使用命令(git rm --cached 文件名)。 需要运行(git add.). 需要(commit -m)“文件删除”。 最后,(git push)。

例如:

我想删除 test.txt 文件. 我偶然推到 GitHub 并想删除它. 命令将如下:

首先,在.gitignore 文件中添加“test.txt”

git rm --cached test.txt
git add .
git commit -m "test.txt removed"
git push

删除文件夹:

需要添加该文件夹到.gitignore 文件夹. 需要删除该文件夹使用命令(git rm -r --cached 文件夹名称)。 需要运行(git add.). 需要(commit -m)“文件夹删除”。 最后,(git push)。

例如:

我想删除.idea 文件夹/目录. 我偶然推到 GitHub 并想删除它。

首先,在.gitignore 文件中添加.idea

git rm -r --cached .idea
git add .
git commit -m ".idea removed"
git push

其他回答

在我的情况下,我需要在.gitignore 文件中插入 ".envrc" 。

然后我用了:

git update-index --skip-worktree .envrc
git rm --cached .envrc

文件已被删除。

然后我再次承诺,说文件已被删除。

但当我使用 git log -p 命令时,文件的内容(这是 Amazon S3 的秘密认证)显示已删除的内容,我不想在 Git 存储库的历史上显示此内容。

然后我用了这个命令:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch .envrc' HEAD

我再也不会看到内容了。

git update-index 为我做工作:

git update-index --assume-unchanged <file>

注意: 这个解决方案实际上是独立的.gitignore 因为 gitignore 仅适用于未追踪的文件。


更新,更好的选择

因为这个答案被发布,一个新的选项已经创建,应该是优先的. 你应该使用 --skip-worktree 这是为修改的跟踪文件,用户不想再承诺,并保持 --assume-unchanged 性能,以防止 git 检查大跟踪文件的状态. 查看 https://stackoverflow.com/a/13631525/717372 更多详细信息...

git update-index --skip-worktree <file>

取消

git update-index --no-skip-worktree <file>

在已承诺的DS_Store的情况下:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

忽略它们:

echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

最后,做一个承诺!

您想要跟踪很多文件,或者您更新了您的.gitignore 文件

来源: Untrack 已添加到基于.gitignore 的 Git 存储库

假设你已经添加 / 订购了一些文件到你的 Git 存储库,然后你将它们添加到你的.gitignore 文件;这些文件将仍然存在于你的存储库指数。

步骤1:承诺所有的变化

在进行之前,请确保您的所有更改都完成,包括您的.gitignore 文件。

步骤2:从存储库中删除一切

要清理您的存储库,请使用:

git rm -r --cached .

如果你想先尝试它做什么,添加 -n 或 - 干燥的旗帜来测试事情。

步骤3:阅读一切

git add .

git commit -m ".gitignore fix"

您的存储库很干净:)

将更改推到您的远程,以便看到更改在那里有效。

答案来自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 .