[英]how to make git ignore ignores in .gitignore
我有一個包含.gitignore
文件的Git存儲庫,該文件列出了一些要忽略的文件(在構建期間創建)。
$ cat .gitignore
foo
$ ls
bar
$ git status
On branch master
nothing to commit, working directory clean
$ touch foo
$ ls
bar foo
$ git status
On branch master
nothing to commit, working directory clean
$
在該存儲庫的特定克隆中,我確實希望看到.gitignore
列出的文件。
這可以通過git status
的--ignored
選項來完成:
$ git status
On branch master
nothing to commit, working directory clean
$ git status --ignored
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
foo
nothing to commit, working directory clean
$
尼斯。 我的問題是:如何為給定的存儲庫創建永久存儲器,以便我不必記住添加--ignored
標志?
$ git status
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
foo
nothing to commit, working directory clean
$
我確定有一些git config
允許我配置我的克隆以忽略.gitignore
的內容...
另外,我不希望所有存儲庫都有此選項; 僅適用於少數人。
Git的(至少2.1及以上) 不允許您配置git status
也默認列出忽略的文件。
但是,Git允許你做的是通過status.showUntrackedFiles
選項修改git status
相對於未跟蹤文件的status.showUntrackedFiles
。
也許未來的Git版本將具有“ status.showIgnoredFiles
”選項......
正如Kleskowy在他的回答中所建議的那樣,明顯且可能最好的做法是在您的存儲庫中定義一個本地別名:
git config alias.istatus "status --ignored"
然后,在相關的存儲庫中運行git istatus
而不是git status
也會列出被忽略的文件。
git
周圍定義一個小包裝器 警告 :以下方法適用於用戶級別,而不適用於存儲庫級別。
或者,如果您不反對在git
周圍使用一個小包裝器,則可以定義以下內容,在運行git status
時會自動使用--ignored
標志:
git() {
if [[ ($1 == "status") ]]; then
command git status --ignored "${@:2}";
else
command git "$@";
fi;
}
將這些行放在.<shell>rc
文件中並獲取后者。
以下是我的一個存儲庫中該包裝器的測試(在bash
)的結果:
請注意,即使未明確使用--ignored
標志,也會列出被忽略的文件。
除非有人能想出更好的方法,否則你所要求的實際上是對Git的功能要求; 因此,我不確定你的問題在這里有它的位置......
所提議的解決方案都沒有達到我真正想要的效果。
但是,@ Jubobs對包裝函數的建議終於讓我知道如何實際做到這一點。
標記更改的給定存儲庫,最好使用git方式 。 這可以使用git config
和一些用戶定義的變量來完成
為什么停止向單個命令( status
)添加單個選項( --ignored
)? 如果我們可以為每個git命令傳遞默認參數會很好
所以我最終得到了以下小片段,在初創公司采購:
git() {
local GIT=$(which git)
case "x$1" in
x|xconfig)
${GIT} "$@"
;;
*)
${GIT} $1 $(${GIT} config --get $1.options) "${@:2}"
;;
esac
}
所以在我的特殊存儲庫中,我只是這樣做:
$ git config --add status.options "--ignored"
之后,在此repo中運行以下命令將向我顯示甚至被忽略的文件,而其他存儲庫將像往常一樣運行。
$ git status
您可以通過創建名為${cmd}.options
的配置選項將您的個人選項添加到任何git命令 。 例如,如果您更喜歡裝飾您的git log
輸出,只需執行以下操作:
$ git config log.options "--decorate"
出於安全原因,腳本會排除config
子命令。 這應該可以防止自己在膝蓋上射擊:
$ git config add config.options --fuzz
最后,它使用一些bashisms (即${@:2}
)來允許包含空格的參數; 如果你的外殼不是bash
,請小心......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.