[英]How do you make Git ignore files without using .gitignore?
由於外部奇怪的限制,我無法修改存儲庫的.gitignore
。 除了修改.gitignore
之外,有沒有辦法忽略文件和目錄? 即使它是一個全局解決方案,例如將應用於我所有存儲庫的全局配置。
不要忘記,根據gitignore ,Git 考慮的不同“忽略模式源”中存在優先順序:
$GIT_DIR/info/exclude
讀取的模式。core.excludesfile
指定的文件中讀取的模式。最后兩個可以解決您的問題,但是:
(另見這個問題)
其他兩個解決方案涉及更新索引( git update-index
):
git update-index --assume-unchanged
:請參閱“ Git:僅在本地存儲庫中取消跟蹤文件並將其保留在遠程存儲庫中”。git update-index --assume-unchanged
on directory ”。--no-assume-unchange
來反轉效果:請參閱“ git add
當前受assume-unchanged
保護的文件? ”。 但是,當您簽出另一個分支或git pull
時,“忽略”狀態可能會被重置。 因此,另一種選擇:
git update-index --skip-worktree
; 看:
兩者之間的區別在“ Git - ' assume-unchanged
'和' skip-worktree
'之間的區別”中進行了解釋。
如果您可以修改.git/info/exclude
,則可以將相同的規則放在那里。 但該文件僅在您的本地存儲庫中。
有三種方法可以告訴 GIT 要忽略哪些文件:
.gitignore
文件$GIT_DIR/.git/info/exclude
core.excludesfile
設置指向的文件后兩點可以解決你的問題。
有關詳細信息,請參閱gitignore(5) 。
如果您只想在存儲庫中有一些本地文件並且子目錄位置靈活,您可以將文件放在tracked_dir1/tracked_dir2/untracked_dir/
中,然后添加tracked_dir1/tracked_dir2/untracked_dir/.gitignore
內容如下:
*
IE
$ cat > tracked_dir1/tracked_dir2/untracked_dir/.gitignore
*
<Ctrl+D>
我遇到過類似的情況,所以我添加了我沒有提到的首選解決方案。 在這種情況下, git update-index --assume-unchanged
的問題是您不能對未跟蹤的文件執行此操作。 你說
我無法修改存儲庫的 .gitignore。
我將假設您的意思是您不能將對.gitignore
的任何更改推送到原點。 如果是這種情況,您可以將未跟蹤的文件添加到本地.gitignore
,然后執行git update-index --assume-unchanged.gitignore
以便永遠不會推送對.gitignore
的更改。 現在您忽略了(可能)未跟蹤的文件,並且不影響遠程.gitignore
文件。
忽略對跟蹤文件的本地更改: git update-index --assume-unchanged my-file.php
忽略對跟蹤文件的本地更改: git update-index --no-assume-unchanged my-file.php
來源: git help update-index
--[no-]assume-unchanged
...
This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked
files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to
modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is
changed upstream, you will need to handle the situation manually.
另一種方式:
.gitignore
git update-index --assume-unchanged.gitignore
可以使用全局的gitignore方法,而不是修改項目https://help.github.com/articles/ignoring-files/#create-a-global-gitignore中的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.