[yeqiang@localhost tmp]$ mkdir testignore
[yeqiang@localhost tmp]$ cd testignore/
[yeqiang@localhost testignore]$ git init .
Initialized empty Git repository in /tmp/testignore/.git/
[yeqiang@localhost testignore]$ mkdir src bin
[yeqiang@localhost testignore]$ echo a > src/a.c
[yeqiang@localhost testignore]$ echo a.out > bin/a.out
[yeqiang@localhost testignore]$ git add *
[yeqiang@localhost testignore]$ git commit -m "first commit"
[master (root-commit) 65ee91c] first commit
2 files changed, 2 insertions(+)
create mode 100644 bin/a.out
create mode 100644 src/a.c
[yeqiang@localhost testignore]$ git status
On branch master
nothing to commit, working tree clean
[yeqiang@localhost testignore]$ echo "垃圾" > dirt.txt
[yeqiang@localhost testignore]$ git add dirt.txt
[yeqiang@localhost testignore]$ git commit -m "错误提交了垃圾文件"
[master 9c76fbe] 错误提交了垃圾文件
1 file changed, 1 insertion(+)
create mode 100644 dirt.txt
[yeqiang@localhost testignore]$ git status
On branch master
nothing to commit, working tree clean
[yeqiang@localhost testignore]$ vim .gitignore
[yeqiang@localhost testignore]$ git add .gitignore
[yeqiang@localhost testignore]$ git rm --cached -r bin/
rm 'bin/a.out'
[yeqiang@localhost testignore]$ git rm --cached dirt.txt
rm 'dirt.txt'
[yeqiang@localhost testignore]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .gitignore
deleted: bin/a.out
deleted: dirt.txt
[yeqiang@localhost testignore]$ git commit -m "忽略文件、目录"
[master 50c89a4] 忽略文件、目录
3 files changed, 3 insertions(+), 2 deletions(-)
create mode 100644 .gitignore
delete mode 100644 bin/a.out
delete mode 100644 dirt.txt
[yeqiang@localhost testignore]$ git status
On branch master
nothing to commit, working tree clean
[yeqiang@localhost testignore]$ echo "fdfdfdfd" > dirt.txt
[yeqiang@localhost testignore]$ git status
On branch master
nothing to commit, working tree clean
其中.gitignore内容
bin/
dirt.txt