您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
28 如何指定不需要Git管理的...
发布时间:2020-07-19 18:09:14编辑:雪饮阅读()
空目录与版本控制以及忽略版本控制
在git中若某个目录不为空则该目录会被git版本控制,如
这里先建立一个空目录
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ mkdir doc
然后会发现status中是空的
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
nothing to commit, working tree clean
现在向这个目录存储一个文件
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ touch doc/1.txt
然后发现status中就提示了doc这个目录没有被暂存
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
nothing added to commit but untracked files present (use "git add" to track)
那么如果我想要这个目录被忽略,不接受版本控制呢?可以这样做
先建立一个gitignore文件
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ cat .gitignore
doc
然后再看看状态,会发现除了刚新建的gitignore文件外没有提示前面建立的doc未暂存
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
忽略语法
上面提到了gitignore可以控制一个目录被忽略版本控制,其实上面那个严格意义来说是忽略文件被版本控制,因为目录是一种特殊的文件,所以上面的语法若换成doc/照样可以,如
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ cat .gitignore
doc/
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
接下来试试纯doc文件,我们这里先把上面的doc目录删除掉,然后创建纯文件doc看看状态
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ touch doc
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
doc
nothing added to commit but untracked files present (use "git add" to track)
会发现纯文件doc还是能够被版本控制,那么我们要脱离版本控制,则只需要把上面的doc/替换为doc即可,如
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ cat .gitignore
doc
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/we4 (master)
$ ls -la
total 9
drwxr-xr-x 1 xy 197121 0 7月 19 17:59 ./
drwxr-xr-x 1 xy 197121 0 7月 18 12:26 ../
drwxr-xr-x 1 xy 197121 0 7月 19 17:59 .git/
-rw-r--r-- 1 xy 197121 4 7月 19 17:59 .gitignore
-rw-r--r-- 1 xy 197121 0 7月 19 17:47 1.txt
-rw-r--r-- 1 xy 197121 0 7月 19 12:59 2.txt
-rw-r--r-- 1 xy 197121 0 7月 18 12:24 3.txt
-rw-r--r-- 1 xy 197121 0 7月 18 12:24 4.txt
-rw-r--r-- 1 xy 197121 0 7月 19 17:57 doc
关键字词:git,忽略,gitignore