我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
使用 git rm --cached 命令不会回答原始问题:
你怎样要强迫吉特完全忘记(一个文件)?
事实上,这个解决方案会导致文件在运行 git pull 时在存储库的每个其他例子中被删除!
强迫 Git 忘记文件的正确方式是由 GitHub 在这里记录的。
我推荐阅读文档,但基本上:
git fetch --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch full/path/to/file' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
只需用文件的完整路径取代完整/路径/到/文件,请确保您已将文件添加到.gitignore 文件中。
您还需要(暂时)允许您的存储库不快推进,因为您正在改变您的 Git 历史。
其他回答
您想要跟踪很多文件,或者您更新了您的.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 .
特别是对于基于 IDE 的文件,我使用以下文件:
例如,对于 slnx.sqlite 文件,我刚刚完全解除它如下:
git rm {PATH_OF_THE_FILE}/slnx.sqlite -f
git commit -m "remove slnx.sqlite"
请记住,其中一些文件存储一些本地用户设置和项目偏好(类似于您打开的文件)。所以每次您浏览或在您的 IDE 中进行一些更改,该文件会被更改,因此它会检查并显示为未订购的更改。
此方法需要使用 /.git/info/exclude 文件(偏好)或预先存在的.gitignore 在所有有文件要被忽视/忘记的任务中。
执行Git的所有方法都忽略了后事实行为,有效地重写历史,从而对任何公共/共享/合作的存储库产生了显著的分裂,这些存储库可能在这个过程之后被撤回。
#Commit up-to-date .gitignore (if not already existing)
#This command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"
#Apply standard Git ignore behavior only to the current index, not the working directory (--cached)
#If this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#This command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#Commit to prevent working directory data loss!
#This commit will be automatically deleted by the --prune-empty flag in the following command
#This command must be run on each branch
git commit -m "ignored index"
#Apply standard git ignore behavior RETROACTIVELY to all commits from all branches (--all)
#This step WILL delete ignored files from working directory UNLESS they have been dereferenced from the index by the commit above
#This step will also delete any "empty" commits. If deliberate "empty" commits should be kept, remove --prune-empty and instead run git reset HEAD^ immediately after this command
git filter-branch --tree-filter 'git ls-files -z --ignored --exclude-standard | xargs -0 git rm -f --ignore-unmatch' --prune-empty --tag-name-filter cat -- --all
#List all still-existing files that are now ignored properly
#If this command returns nothing, it's time to restore from backup and start over
#This command must be run on each branch
git ls-files --other --ignored --exclude-standard
git push origin --force --all
git push origin --force --tags
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
其他从已修改的远程存储库中提取的开发人员应该进行备份,然后:
#fetch modified remote
git fetch --all
#"Pull" changes WITHOUT deleting newly-ignored files from working directory
#This will overwrite local tracked files with remote - ensure any local modifications are backed-up/stashed
git reset FETCH_HEAD
脚印
告诉你的合作伙伴放弃,不要合并,他们创建的任何分支从你的旧(封闭)存储库历史。 一个合并承诺可以重新引入一些或所有的封闭的历史,你刚刚走到清理问题。
为文件 / 文件夹执行下列步骤:
删除文件:
需要添加该文件到.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
推荐文章
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域
- 我可以在GitHub上对要点进行拉请求吗?
- Hg:如何做一个像git的rebase
- 如何丢弃远程更改并将文件标记为“已解决”?
- 如何查看远程标签?