假设我们有一个文件无意中添加到我们的存储库中:
stackoverflow$ echo this file is important > junkfile
stackoverflow$ git add junkfile
stackoverflow$ git ci -m 'added a file'
在某个时候,我们意识到该文件并不重要,因此我们将其添加到我们的.gitignore
文件中:
stackoverflow$ echo junkfile >> .gitignore
stackoverflow$ git ci -m 'ignore junkfile' .gitignore
稍后,我们对该文件进行一些更改:
stackoverflow$ sed -i 's/ is / is not/' junkfile
当然,即使该文件在中列出.gitignore
,我们也已经告诉git我们要跟踪它,因此git status
显示:
stackoverflow$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: junkfile
#
no changes added to commit (use "git add" and/or "git commit -a")
我们需要从存储库中删除此文件(而无需从工作树中删除文件)。我们可以使用以下--cached
标志来做到这一点git rm
:
stackoverflow$ git rm --cached junkfile
rm 'junkfile'
这将分阶段进行delete
操作...
stackoverflow$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: junkfile
#
...所以我们需要提交:
stackoverflow$ git commit -m 'removed junkfile from repository'
[master 19f082a] removed junkfile from repository
1 file changed, 1 deletion(-)
delete mode 100644 junkfile
现在,如果我们运行git status
git,将忽略此文件:
stackoverflow$ git status
# On branch master
nothing to commit, working directory clean
即使它仍在我们的工作树中:
stackoverflow$ cat junkfile
this file is not important
backend/Evaluation/logs/*