GITIgnore 已使用排除本地更改提交的文件

标签 git

<分区>

我克隆了包含三个文件的远程存储库

file1.txt 
file2.txt
file3.txt

在我的机器上对 file3.txt 进行本地更改。我不希望更改进入远程仓库,反之亦然。

我读到使用 .gitignore 无法忽略已经提交和跟踪的文件。

我决定在 .git/info/exclude 文件中使用以下行

排除文件

file3.txt

但在 file3.txt 中所做的更改仍然显示为未暂存文件。我希望这个 file3.txt 在提交和暂存中被忽略,还需要防止 file3.txt 从远程仓库更新

在此先感谢您的帮助

最佳答案

I read that using .gitignore it is not possible to ignore the already commited and tracked file.

是的,使用 .gitignore.git/info/exclude

您只需要先记录该文件的删除即可。

git rm --cached file3.txt

一旦它被删除(仅从缓存中,而不是从硬盘驱动器中),git status 不应显示它(因为它已经在 .gitignore)

参见“git rm --cached x” vs “git reset head — x”?”:“缓存”,也称为“索引”或“暂存区”,仅从索引中删除文件并将其保存在您的工作副本中。

file3.txt 将不再在 repo 中进行版本控制。
一旦该删除被提交并推送,这将传播到其他克隆。


如果你只需要ignore the file locally有一段时间,你可以试试:

git update-index --assume-unchanged -- file3.txt

注意:这根本不涉及 .gitignore

停止本地忽略更改:

git update-index --no-assume-unchanged -- file3.txt

关于GITIgnore 已使用排除本地更改提交的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536626/

相关文章:

git - 如何使用 Git 跟踪给定分支的多个 Remote ?

git - 我什么时候需要做 "git pull",在 "git add, git commit"之前或之后?

git - 可以让 "git mergetool"即使 merge 一个文件失败也自动继续尝试其他文件吗?

bash - 如何在每次提交时运行脚本

git - 将 git 存储库传输到 Web 服务器

git - 如何转到git中的特定提交

Git 日志条目显示两次但具有不同的提交 ID

混帐 : fatal: Ambiguous object name: 'origin/release_2.6'

git将多个提交压缩为多个提交

git - 我如何从 'git log' 命令中退出并处理它并继续执行其他命令?