Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git 忽略规则及 .gitignore 规则不生效的解决办法 #50

Open
ONE-SUNDAY opened this issue Jul 11, 2017 · 2 comments
Open

Git 忽略规则及 .gitignore 规则不生效的解决办法 #50

ONE-SUNDAY opened this issue Jul 11, 2017 · 2 comments

Comments

@ONE-SUNDAY
Copy link

在 Git 中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件)。这个文件每一行保存了一个匹配的规则例如:

# 此为注释 – 将被 Git 忽略
*.a       # 忽略所有 .a 结尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

规则很简单,不做过多解释,但是有时候在项目开发过程中,突然心血来潮想把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效,原因是 .gitignore 只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改 .gitignore 是无效的。那么解决方法就是先把本地缓存删除(改变成未 track 状态),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

转自 梧桐树下 » Git忽略规则及.gitignore规则不生效的解决办法

@deepkolos
Copy link

原来正确的姿势是这样的啊, 之前是删除文件 commit, 再添加到gitignore 再commit来弄的. 学习了~

@sir-ran
Copy link

sir-ran commented Feb 2, 2021

涨自知识了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants