我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
這不再是最新 Git 的問題(v2.17.1 在寫作時)。
.gitignore 文件最终忽略了跟踪但删除的文件. 您可以通过运行下列脚本来为自己测试此文件. 最终 git 状态声明应该报告“没有任何承诺”。
# Create an empty repository
mkdir gitignore-test
cd gitignore-test
git init
# Create a file and commit it
echo "hello" > file
git add file
git commit -m initial
# Add the file to gitignore and commit
echo "file" > .gitignore
git add .gitignore
git commit -m gitignore
# Remove the file and commit
git rm file
git commit -m "removed file"
# Reintroduce the file and check status.
# .gitignore is now respected - status reports "nothing to commit".
echo "hello" > file
git status
其他回答
特别是对于基于 IDE 的文件,我使用以下文件:
例如,对于 slnx.sqlite 文件,我刚刚完全解除它如下:
git rm {PATH_OF_THE_FILE}/slnx.sqlite -f
git commit -m "remove slnx.sqlite"
请记住,其中一些文件存储一些本地用户设置和项目偏好(类似于您打开的文件)。所以每次您浏览或在您的 IDE 中进行一些更改,该文件会被更改,因此它会检查并显示为未订购的更改。
我知道这个答案已经迟到了很多年,但是我决定我想在注意到可能有不同的答案后做出贡献,取决于用户想要的结果。
如何强迫Git完全忘记这一点?
这取决于下列哪个是所需的结果:
停止跟踪文件并将其从存储库中删除,或停止跟踪文件,而不会从存储库中删除(可能其他合作伙伴可以下载)
因为(一),解决方案就像上面的马特的答案一样;因为(二),请参见康斯坦丁的答案。
来源: https://stackabuse.com/git-stop-tracking-file-after-adding-to-gitignore/
答案来自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 .
如果你不想使用 CLI 并在 Windows 上工作,一个非常简单的解决方案是使用 TortoiseGit. 它在菜单中有“删除(保持本地)”操作,它工作得很好。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别