[英]Ignoring directories in Git repositories on Windows
如何在 Windows 上使用 msysgit 忽略 Git 中的目錄或文件夾?
默認情況下,Windows 資源管理器將顯示.gitignore
,而實際上文件名是.gitignore.txt
。
Git 不會使用.gitignore.txt
而且您不能將文件重命名為.gitignore
,因為 Windows 資源管理器認為它是一個沒有名稱的 gitignore 類型的文件。
非命令行解決方案:
You can rename a file to ".gitignore.", and it will create ".gitignore"
似乎忽略文件和目錄有兩種主要方法:
.gitignore
.gitignore
文件放置到存儲庫的根目錄中,除了.git
文件夾(在 Windows 中,確保看到真實的文件擴展名,然后生成.gitignore.
(最后的點是空文件擴展名))~/.gitignore_global
並運行git config --global core.excludesfile ~/.gitignore_global
將其添加到您的 Git 配置中注意:之前跟蹤的文件可以通過運行git rm --cached filename
取消跟蹤
存儲庫排除- 對於不需要共享的本地文件,您只需將文件模式或目錄添加到文件.git/info/exclude
。 這些規則未提交,因此其他用戶看不到它們。 更多信息在這里。
要在忽略文件列表中設置例外,請參閱此問題。
要忽略整個目錄,請在此處放置一個帶有“*”的 .gitignore。
例如,
示例系統
/root/
.gitignore
/dirA/
someFile1.txt
someFile2.txt
/dirB/
.gitignore
someFile3.txt
someFile4.txt
目標
頂級( /root/.gitignore )
忽略的目錄( /root/dirB/.gitignore )
Git 在文件系統的每一步都監視 gitignore,所以如果在每個級別都有一些東西要忽略,那就把它放進去! (例如,在這里放置為“ someFile3.txt ”,或者在根 .gitignore 中放置為“ dirB/someFile3.txt ”,如果你願意的話)
所以在這里,我選擇 dirB/.gitignore 作為“*”來忽略所有內容,包括其中的所有文件和子目錄。
完成☺️
要指示 Git 忽略某些文件或文件夾,您必須創建.gitignore
文件。
但在 Windows 資源管理器中,您必須為文件提供名稱。 您不能僅使用擴展名創建文件。 訣竅是創建一個空文本文件並轉到命令提示符並將文件名更改為.gitignore
:
ren "New Text Document.txt" .gitignore
現在使用您喜歡的文本編輯器打開文件並添加您希望忽略的文件/文件夾名稱。 您還可以使用這樣的通配符: *.txt
。
我在Windows 資源管理器中使用.
一開始。
一種解決方法是進入命令外殼並使用“編輯”創建一個新文件。
如果你想維護一個文件夾而不是其中的文件,只需在文件夾中放置一個“.gitignore”文件,以“*”作為內容。 該文件將使 Git 忽略存儲庫中的所有內容。 但是.gitignore
將包含在您的存儲庫中。
$ git add path/to/folder/.gitignore
如果您添加一個空文件夾,您會收到此消息(.gitignore 是一個隱藏文件)
The following paths are ignored by one of your .gitignore files:
path/to/folder/.gitignore
Use -f if you really want to add them.
fatal: no files added
因此,使用“-f”強制添加:
$ git add path/to/folder/.gitignore -f
在 Windows 中,有一個額外的斜線。 排除.gitignore中的單個目錄
dir_to_exclude/
可能會工作,但不包括所有目錄
/
當您的目錄中有帶空格的文件名(如my file.txt
)時會導致問題:Git Bash 使用反斜杠(如my\ file.txt
)轉義這些空格,並且適用於 Windows 的 Git 不區分/
和\
。
要排除所有目錄,最好使用:
**/
兩個連續的星號表示目錄內容。
您可以使用以下內容創建“.gitignore”文件:
*
!.gitignore
這個對我有用。
萬一您需要排除子文件夾,您可以使用**
通配符排除任何級別的子目錄。
**/build/output/Debug/
在您的\.git\info
項目目錄中還有一個排除文件,它實際上與.gitignore
相同(我認為)。 您可以在其中添加要忽略的文件和目錄。
當一切都失敗時,嘗試編輯文件
/.git/info/排除
並將您想要的目錄添加到文件末尾,如下所示:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/
我將文件夾“assets”和“compiled”添加到要忽略的文件和目錄列表中。
在 Unix 上:
touch .gitignore
在 Windows 上:
echo > .gitignore
在終端中執行的這些命令將在當前位置創建一個.gitignore
文件。
然后只需將信息添加到這個.gitignore
文件(例如使用 Notepad++)應該忽略哪些文件或文件夾。 保存您的更改。 而已 :)
更多信息: .gitignore
我在讓 Git 在 Windows 上獲取.gitignore
文件時遇到了一些問題。 $GIT_DIR/info/exclude
文件似乎總是有效。
然而,這種方法的缺點是$GIT_DIR
目錄中的文件不包含在簽入中,因此不共享。
ps
$GIT_DIR
通常是名為.git
的隱藏文件夾
我認為問題是你的工作樹是這樣的:
a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore
...使用您描述的.gitignore
。 這將為您提供git status
輸出,例如:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# a-cache/
# b-cache/
...如果index.html
文件尚未添加到存儲庫中。 (Git 發現緩存目錄中有未被忽略的文件,但它只報告目錄。)要解決此問題,請確保您已添加並提交index.html
文件:
git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"
...然后您的git status
將如下所示:
$ 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
。我只是懶得處理這個測試用例。)
在 Windows 和 Mac 上,如果要忽略當前目錄中名為 Flower_Data_Folder 的文件夾,可以執行以下操作:
echo Flower_Data_Folder >> .gitignore
如果它是一個名為data.txt
的文件:
echo data.txt >> .gitignore
如果是像“Data/passwords.txt”這樣的路徑
echo "Data/passwords.txt" >> .gitignore.
我有類似的問題。 我使用與 Linux 人員共享存儲庫的 Windows 工具鏈工作,他們很樂意在給定文件夾中創建具有相同(大小寫除外)名稱的文件。
效果是我可以克隆存儲庫並立即擁有數十個“修改過的”文件,如果我簽入這些文件,將會造成嚴重破壞。
我將 Windows 設置為區分大小寫,將 Git 設置為不忽略大小寫,但它仍然失敗(顯然在 Win32 API 調用中)。
如果我 gitignore 文件,那么我必須記住不要跟蹤 .gitignore 文件。
但我在這里找到了一個很好的答案:
http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html
只需在您的項目文件夾中創建.gitignore
文件然后在其中添加文件夾的名稱,例如:
frontend/node_modules
這對某些人來說可能非常明顯,但我確實從其他答案中理解了這一點。
在目錄中創建 .gitignore 文件本身並沒有任何作用。 您必須將 .gitignore 作為文本文件打開並寫入您希望它忽略的文件/目錄,每個文件/目錄各占一行。
所以 cd 到 Git 存儲庫目錄
touch .gitignore
nano .gitignore
然后寫下您想要忽略的文件和/或目錄的名稱及其擴展名(如果相關)。
此外,.gitignore 是某些操作系統(例如 Mac)上的隱藏文件,因此您需要ls -a
才能看到它,而不僅僅是 ls。
暫時忽略 git 中已經存在的目錄/文件:
我在一個多項目 gradle 項目中有很多項目,它們可能需要很長時間才能刪除它們,它們幾乎相同但不同。 有時我想通過完全刪除它們來從 gradle 構建中刪除它們。 git 畢竟可以讓他們回來。 但是我也不希望它們出現在 git status 中。 所以我使用以下簡單的程序;
git ls-files --deleted -z | git update-index --assume-unchanged -z --stdin
git ls-files --deleted -z | git update-index --no-assume-unchanged -z --stdin
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.