git 排除已經加入版本控制的文件、目錄

[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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章