我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
在我的情况下,我需要在.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 索引中删除所有项目(不是工作目录或本地存储库),然后更新 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"
什么不为我工作
(在Linux下),我想使用这里的帖子,建议Ls - 文件 - 被忽略 - 排除 - 标准 - xargs git rm -r - 被捕的方法。
git ls-files --ignored --exclude-standard | xargs -d"\n" git rm --cached
git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached
处理此情况(获取未找到文件的错误)。
所以我提供
git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git commit -am "Remove ignored files"
这使用 -z 对 ls 文件的论点,以及 -0 对 xargs 的论点,以便在文件名中安全/正确地输入“不良”字符。
在 git-ls-file(1)的手册页面上,它说:
当 -z 选项未使用时,在 pathnames 中,TAB、LF 和 backslash 字符分别以 \t、 \n 和 \\ 表示。
所以我认为我的解决方案是必要的,如果有这些字符中的任何一个字符。
例如,在使用 CVS 时,这个问题不存在, CVS 将信息存储为基于文件的变更列表。
这两篇文章对我很有帮助:
git assume-unchanged vs skip-worktree 以及如何用 Git 忽略跟踪文件的变化
git update-index --skip-worktree <file>
从此,此文件中的所有本地变更将被忽视,不会去远程。如果文件在远程中被更改,冲突将发生,当 git pull. Stash 不会工作。
git update-index --no-skip-worktree <file>
git stash
git pull
文件内容将被远程内容取代. 将您的更改从安全地点调整到文件,然后再次执行:
git update-index --skip-worktree <file>
這不再是最新 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
我喜欢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 和您刚刚删除的文件承诺
推荐文章
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域
- 我可以在GitHub上对要点进行拉请求吗?
- Hg:如何做一个像git的rebase
- 如何丢弃远程更改并将文件标记为“已解决”?
- 如何查看远程标签?