git如何忽略已经加入版本控制的文件

git移除已经追踪的文件

有时候新增一个文件,会自动追加到git的版本控制当中,但是又不想提交到仓库。可以按照下面的步骤:

git status

查看管理状态:

ml-py git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   work-testing/01-sex-predict/data.tg

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/misc.xml
        .idea/ml-py.iml
        .idea/modules.xml
        .idea/workspace.xml

其中data.tg就是我不想提交的文件,但是现在已经进入到版本控制当中了。

那么可以通过rm删除当前的控制状态:

ml-py git:(master) ✗ git rm --cached work-testing/01-sex-predict/data.tg
rm 'work-testing/01-sex-predict/data.tg'

再次查看就发现已经到了未加入版本控制状态列表里面

➜  ml-py git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/misc.xml
        .idea/ml-py.iml
        .idea/modules.xml
        .idea/workspace.xml
        work-testing/

nothing added to commit but untracked files present (use "git add" to track)

然后把该文件添加到.gitignore里面就可以了。有时候工程初始化并没有.gitignore文件,可以自己创建一个:

touch .gitignore

然后手动编辑即可:

# 敏感数据
*.tg

# 排除工程文件
.idea/

提交后,以后再创建的xxx.tg就不会自动加入到版本控制了。

posted @ 2018-06-13 12:57  xingoo  阅读(1715)  评论(0编辑  收藏  举报