git 忽略文件 .gitignore 以及規則


git提供了文件忽略系統,當對工作區某個目錄或文件設置了忽略后,在執行status查看狀態時,被忽略的文件即使存在也不會顯示出來。

這樣我就可以把那些不需要上傳,不需要保留的文件或目錄忽略掉(比如一些動態生成的log,或者編譯出來的文件等等)。

對於忽略文件,git提供了3種方式(我們組的大神又告訴我了另一種方法)下面來一一介紹一下:

一:.gitignore設置遠程共享忽略文件

忽略文件.gitignore使用:

首先可以從文件名看出.gitignore是一個隱藏文件 一般我們默認會把它建立在倉庫的根目錄(也可以是倉庫下的任意目錄)

如下:

cd 到根目錄;

ls -a 查看所有隱藏文件;

vim .gitignore 創建或編輯.gitignore文件,將需要忽略的文件寫在其中;

忽略文件的規則:

*.a 忽略所有以.a為后綴的文件;

!lib.a 不忽略文件lib.a;

/TODO 只忽略此目錄下TODO文件,子目錄的TODO不被忽略;

build/ 忽略build目錄下的所有文件;

doc/*.txt 只忽略doc/下所有的txt文件,但是不忽略doc/subdir/下的txt文件;

小技巧:

  1. 文件.gitignore可以放在當前倉庫中的任何目錄中;
  2. 忽略只對未跟蹤文件有效,對於已經加入版本庫的文件無效;
  3. 如果不希望將.gitignore添加到庫里,也不希望.gitignore文件帶來任何干擾,可以在忽略文件中忽略自己;

二:本地獨享

上面設置的.gitignore通常都會上傳到遠端的版本庫中,所以它是“共享式”的,而在有的情況下我們需要有一個自己“獨享式”的本地忽略文件。

具體版本庫:

在該版本庫.git/info/exclude來設置文件忽略,或者也可以使用.gitignore文件來忽略自己可以達到同樣效果

全局版本庫:

全局忽略是指忽略本地中所有的版本庫。

通過設置git config:

$ git config --global core.excludefile /dean/.gitignore_global

/dean/.gitignore_global 是一個自定義的忽略文件,這個文件的目錄、名字和里面的內容都按不同需求任意設定。

大神告訴的一個方法:

進入到本機的.config/git中,里面有一個ignore文件。

我們在這個文件中加上想要忽略的就可以起到全局忽略的效果。

可以使用如下命令很方便: vim .config/git/ignore

 

忽略文件規則

git 對於 .gitignore 配置文件是按行從上到下進行規則匹配的,如果前面的規則匹配的范圍更大,則后面的規則將不會生效;

忽略規則是對文件名有效的

A: 空行或#號開始的行,會被忽略;

B: 可以使用通配符:
   *        任意字符;
   ?        單個字符;
   [abc]       多種可能的字符a、b或c;
   [a-z0-9]     表示在某個范圍內進行匹配;
   \          轉義字符;
   !        表示取反(不忽略),寫在某條規則的前面;

C: 路徑分隔符"/";

如果"/"后面的名稱是個目錄,則該目錄以及該目錄下的所有文件都會被忽略;

如果"/"后面的名稱是個文件,則該文件不會被忽略;

   例如: /name
   如果name是個目錄,則目錄name和name下的所有文件都會被忽略;
   如果name是個文件,則該文件不會被忽略;

D: .gitignore文件也可以忽略自己,只要把自己的名字寫進來即可;

E: 一條(行)忽略規則只對某一個目錄下的文件有效,而對該目錄下的子目錄中的文件無效;

F: 一條(行)忽略規則也可以只對單個文件有效(忽略單個指定的文件);

例如:
*.a      #忽略所有以.a為后綴的文件;
!lib.a    #不忽略文件lib.a;
/TODO     #只忽略此目錄下TODO文件,子目錄的TODO不被忽略;
build/    #忽略當前目錄之下的 build 目錄下的所有文件;
build     #忽略 任何目錄 build文件;
/build    #忽略build根目錄下的build文件;
/build/    #忽略build根目錄下的build目錄下的所有文件;
doc/*.txt  #只忽略doc/下所有的txt文件,但是不忽略doc/subdir/下的txt文件;

 

可以使用標准的 glob 模式匹配

所謂的 glob 模式是指 shell 所使用的簡化了的正則表達式。

星號(*)匹配零個或多個任意字符;

[abc] 匹配任何一個列在方括號中的字符(這個例子要么匹配一個 a,要么匹配一個 b,要么匹配一個 c);

問號(?)只匹配一個任意字符;

如果在方括號中使用短划線分隔兩個字符,表示所有在這兩個字符范圍內的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的數字)。

使用兩個星號(*) 表示匹配任意中間目錄,比如a/**/z 可以匹配 a/za/b/z 或 a/b/c/z等。

 

1. 本地倉庫忽略
本地倉庫的文件忽略規則可以在
.git/info/exclude文件中添加。
這些忽略的文件不會提交到共享庫中,因而不會被協作者所共享。
 
2. 當前工作目錄添加文件忽略
對於每一級工作目錄,創建一個.gitignore文件,向該文件中添加要忽略的文件或目錄。
但在創建並編輯這個文件之前,一定要保證要忽略的文件沒有添加到git索引中。
使用命令 git rm --cached filename將要忽略的文件從索引中刪除。
 
--摘抄.gitignore的格式規范
• 所有空行或者以注釋符號 # 開頭的行都會被 Git 忽略。
• 可以使用標准的 glob 模式匹配。
• 匹配模式最后跟反斜杠(/)說明要忽略的是目錄。
• 要忽略指定模式以外的文件或目錄,可以在模式前加上驚嘆號(!)取反。

所謂的 glob 模式是指 shell 所使用的簡化了的正則表達式。
星號(*)匹配零個或多個任意字符;
[abc] 匹配任何一個列在方括號中的字符(這個例子要么匹配一個 a,要么匹配一個 b,要么匹配一個 c);
問號(?)只匹配一個任意字符;
如果在方括號中使用短划線分隔兩個字符,表示所有在這兩個字符范圍內的都可以匹配(比如[0-9]表示匹配所有 0 到 9 的數字)。
 
2.1 工作目錄的每一層下級目錄都可以有一個.gitignore文件,以說明當前目錄下需要被git忽略的文件或目錄
2.2 .gitignore文件可以被提交到共享庫中被協作者共享 <也可以忽略自身>
 
3. 全局的.gitignore
 
可以通過創建~/.gitignore_global並添加到git全局配置以減少每層目錄的規則重復定義。
使用命令 git config --global core.excludesfile ~/.gitignore_global即可
 
.gitignore_global文件范例
 
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db

規則:.DBStore/*

說明:忽略.gitignore文件所在的目錄之下的任何位置的目錄 .DBStore 下的全部內容;

注意,不管是根目錄下的 /.DBStore/ 目錄,

還是某個子目錄 /lib/.DBStore/ 目錄,都會被忽略;

規則:/upload/*

說明:忽略根目錄下的 /upload/ 目錄的全部內容; 

規則:!.gitignore  

跟蹤 .gitignore 文件

說明:忽略全部內容<如果存在一行 * >,但是不忽略 .gitignore 文件 

如果要忽略的文件已被git管理,需要先移除,命令如下: -r:遞歸
If you already have a file checked in, and you want to ignore it,
Git will not ignore the file if you add a rule later.
In those cases, you must untrack the file first, by running the following command in your terminal:

git rm -r --cached  WebRoot/WEB-INF/classes/**/*

 

 

PATTERN FORMAT

  • A blank line matches no files, so it can serve as a separator for readability.

  • A line starting with # serves as a comment. Put a backslash ("\") in front of the first hash for patterns that begin with a hash.

  • Trailing spaces are ignored unless they are quoted with backslash ("\").

  • An optional prefix "!" which negates the pattern;
    any matching file excluded by a previous pattern will become included again.
    It is not possible to re-include a file if a parent directory of that file is excluded.
    Git doesn’t list excluded directories for performance reasons,
    so any patterns on contained files have no effect, no matter where they are defined.
    Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example,
    "\!important.txt".

  • If the pattern ends with a slash /, it is removed for the purpose of the following description,
    but it would only find a match with a directory.
    In other words, foo/ will match a directory foo and paths underneath it,
    but will not match a regular file or a symbolic link foo 
    (this is consistent with the way how pathspec works in general in Git).

  • If the pattern does not contain a slash /, Git treats it as a shell glob pattern and
    checks for a match against the pathname relative to the location of the .gitignore file 文件所在目錄
    (relative to the toplevel of the work tree if not from a .gitignore file).

  • Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag:
    wildcards in the pattern will not match a / in the pathname.
    For example, "Documentation/*.html" matches "Documentation/git.html"
    but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

  • A leading slash matches the beginning of the pathname.
    For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

  • A leading "**" followed by a slash means match in all directories.
    For example,
    "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo".
    "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

  • A trailing "/**" matches everything inside.
    For example, "abc/**" matches all files inside directory "abc",
    relative to the location of the .gitignore file, with infinite depth.
    "abc/*.*" : relative to the location of the .gitignore file only

  • A slash / followed by two consecutive asterisks then a slash matches zero or more directories.
    For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

  • Other consecutive asterisks are considered invalid.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM