[英]Using .gitignore to ignore everything but specific directories
我的問題是我的 git 存儲庫中有一堆 WordPress 網站,其中我想有選擇地只提交我的themes
文件夾的內容,而忽略在 WordPress 中找到的其余冗余文件。
我之前使用過 .gitignore 文件來忽略文件類型,但是可以反過來使用它 - 即忽略除某個文件夾路徑之外的所有內容嗎?
根(git repo)
- / wordpress
- - / (WordPress 站點 1)/wp-content/themes
- - / (WordPress 站點 2)/wp-content/themes
- - / (WordPress 站點 3)/wp-content/themes
謝謝-
更新:
根據答案,我做了以下操作,但它不起作用。 有任何想法嗎?
# Ignore everything:
*
# Except for wordpress themes:
!*/wp-content/themes/*
我還嘗試了以下變體:
!*/wp-content/themes*
!*wp-content/themes/*
!wp-content/themes/*
!/wordpress/*/wp-content/themes*
!wordpress/*/wp-content/themes*
這些都沒有讀取我的themes
文件夾。
我是這樣做的 - 你基本上必須沿着路徑走,你不能在任何方向上通配一個以上的級別:
# Ignore everything:
*
# Except for the themes directories:
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*
請注意您必須如何明確允許您想要包含的每個級別的內容。 所以如果我在主題下面有 5 個子目錄,我仍然需要把它拼出來。
這只是它對我的工作方式。 如果有人願意提供更明智的解釋。
此外,這些答案很有幫助:
How-do-negated-patterns-work-in-gitignore
How-do-gitignore-exclusion-rules-actual-work
注意:我嘗試使用雙通配符“globs”,但據此該功能取決於系統,並且在我的 mac 上不起作用:
不工作:
!**/wp-content/themes/
!**/wp-content/themes/**
我嘗試了上述方法,但效果不佳。 更好的方法如下: https : //gist.github.com/444295
# This is a template .gitignore file for git-managed WordPress projects.
#
# Fact: you don't want WordPress core files, or your server-specific
# configuration files etc., in your project's repository. You just don't.
#
# Solution: stick this file up your repository root (which it assumes is
# also the WordPress root directory) and add exceptions for any plugins,
# themes, and other directories that should be under version control.
#
# See the comments below for more info on how to add exceptions for your
# content. Or see git's documentation for more info on .gitignore files:
# http://kernel.org/pub/software/scm/git/docs/gitignore.html
# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
# Ignore everything in the "wp-content" directory, except the "plugins"
# and "themes" directories.
wp-content/*
!wp-content/plugins/
!wp-content/themes/
# Ignore everything in the "plugins" directory, except the plugins you
# specify (see the commented-out examples for hints on how to do this.)
wp-content/plugins/*
# !wp-content/plugins/my-single-file-plugin.php
# !wp-content/plugins/my-directory-plugin/
# Ignore everything in the "themes" directory, except the themes you
# specify (see the commented-out example for a hint on how to do this.)
wp-content/themes/*
# !wp-content/themes/my-theme/
修改第一行
*
將其更改為
/*
試試這些答案:
# Ignore everything * # But not these files... !.gitignore !script.pl !template.latex # etc... # ...even if they are in subdirectories !*/
這將忽略根文件和根目錄,然后取消忽略根 bin 目錄:
/* /*/ !/bin/
這樣你就可以得到所有的 bin 目錄,包括子目錄和它們的文件。
如果您在模式前加上感嘆號 ( !
),它將否定排除它的任何先前模式。 因此,大概您可以忽略所有內容,然后通過使用此模式只允許您想要的內容。
假設你有一個這樣的目錄結構:
– sites
– -all
– – – -files
– – – – – -private
– – – – – – – -newspilot
– -default
– – – -files
– – – – – -private
– – – – – – – -newspilot
當您只想允許 sites/all/files/private/newspilot和sites/default/files/private/newspilot 時,您可以首先嘗試以下操作:
sites/*/files/*
!sites/*/files/private/newspilot
這是錯誤的! gitignore 的棘手之處在於,您必須首先允許包含父目錄(“私有”) ,然后才能允許其子目錄(“newspilot”)包含在提交中。
sites/*/files/*
!sites/*/files/private
sites/*/files/private/*
!sites/*/files/private/newspilot
http://www.christianengvall.se/gitignore-exclude-folder-but-include-a-subfolder/
我需要忽略所有內容,但不是一個帶有子目錄的文件夾。
對我來說,這有效
# Ignore everything
/*
# Not these directories
!folder/
# Not these files
!.gitignore
!.env
!file.txt
即使在多次回到這個問題之后,我也總是被困在某個地方。 我想出了一個詳細的過程,一步一步來:
首先只需使用git add
添加實際內容。
它將顯示添加到索引中的相關文件,而所有其他文件仍未被跟蹤。 這有助於逐步構建.gitignore
。
$ git add wp-content/themes/my-theme/*
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Untracked files:
wp-admin/
wp-content/plugins/
wp-content/themes/twentyeleven/
wp-content/themes/twentytwelve/
...
wp-includes/
...
在您的目錄中添加一個臨時DUMMY.TXT
文件:
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Untracked files:
wp-admin/
wp-content/plugins/
wp-content/themes/twentyeleven/
wp-content/themes/twentytwelve/
...
wp-content/themes/my-theme/DUMMY.TXT <<<
...
wp-includes/
...
我們現在的目標是構建規則,使這個DUMMY.TXT
成為唯一一個在我們完成后仍然顯示為 Untracked 的規則。
開始添加規則:
.gitignore
/*
第一個就是忽略一切。 未跟蹤的文件應該全部消失,只有索引的文件應該顯示:
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
在路徑wp-content
添加第一個目錄
/*
!/wp-content
現在 Untracked 文件將再次出現,但只有wp-content
的內容
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Untracked files:
wp-content/plugins/
wp-content/themes/twentyeleven/
wp-content/themes/twentytwelve/
..
忽略第一個目錄中的所有/wp-content/*
並取消忽略!/wp-content/themes
/*
!/wp-content
/wp-content/*
!/wp-content/themes
現在未跟蹤的文件將進一步縮小到只有wp-content/themes
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Untracked files:
wp-content/themes/twentyeleven/
wp-content/themes/twentytwelve/
..
重復該過程,直到該虛擬文件成為唯一一個仍顯示為 Untracked 的文件:
/*
!/wp-content
/wp-content/*
!/wp-content/themes
/wp-content/themes/*
!/wp-content/themes/my-theme
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Untracked files:
wp-content/themes/my-theme/DUMMY.TXT
在多次需要此之后,我終於找到了解決方案 [1]:
/*/
/*.*
!.git*
!/wordpress
逐行解釋:
.gitignore
本身(以及可能的.gitattributes
)。[1]限制(我知道):
/*
會破壞整個方案)。wordpress
)的目錄不能有.
在名稱中(例如wordpress.1
不起作用)。 如果它有一個.
,然后刪除第 2 行並找到另一種排除根目錄下所有文件的方法。git version 2.17.1.windows.2
Windows 上測試另一個簡單的解決方案:
您想忽略所有 Wordpress 文件,但不想忽略您的主題(例如)。
.gitignore ,內容是:
# All wordpress + content of revert ignored directories
wordpress/*
wordpress/wp-content/*
wordpress/wp-content/themes/*
# Revert ignoring directories
!wordpress/
!wordpress/wp-content/
!wordpress/wp-content/themes/
!wordpress/wp-content/themes/my_theme
在這個例子中,如果.gitignore在wordpress 根目錄中,你可以刪除wordpress
您可以對要“特別”排除在 gitignored 文件夾/文件之外的每個文件夾和內容執行完全相同的操作
結論 :
請務必取消忽略您要取消忽略的路徑的所有目錄
但
請務必忽略您要忽略的未忽略目錄的所有內容
Yarin 的回答對我有用,這是我的版本(我不需要/wordpress
子目錄):
*
!.gitignore
!/wp-content/
!/wp-content/themes/
!/wp-content/themes/*
!/wp-content/themes/*/*
!/wp-content/themes/*/*/*
!/wp-content/themes/*/*/*/*
!/wp-content/themes/*/*/*/*/*
# I don't want to track this one, so it's included here, after the negated patterns.
wp-content/themes/index.php
對於那些正在尋找更清潔解決方案的人,請嘗試以下操作。
正如此答案的評論中所述,您必須遞歸地使用此方法。
在這個例子中,你在./
有一個網站設置,你的.git
文件夾和.gitignore
文件所在的位置和 WordPress 安裝設置在./wordpress
。 要正確忽略./wordpress
目錄下的所有內容,除了主題目錄本身( wordpress/wp-content/themes/my-theme
),您必須遞歸地忽略並允許每個目錄,直到您希望允許的目錄:
wordpress/*
wordpress/wp-content/*
wordpress/wp-content/themes/*
!wordpress/wp-content
!wordpress/wp-content/themes
!wordpress/wp-content/themes/my-theme
使用通配符忽略並允許(或忽略“除了”)目錄本身的原因使 Git 在忽略目錄中的所有內容之前先查看目錄內部。 然后我們告訴 Git 忽略“除了”我們指定的目錄之外的所有內容。 這是相同的語法,但按照 Git 如何看待它的順序:
wordpress/* # Ignore everything inside here
!wordpress/wp-content # Apart from this directory
wordpress/wp-content/* # Ignore everything inside here
!wordpress/wp-content/themes # Apart from this directory
wordpress/wp-content/themes/* # Ignore everything inside here
!wordpress/wp-content/themes/my-theme # Apart from this directory
希望這有助於人們更好地理解遞歸方法的必要性。
遲到了,但這是我用於未知深度的方法(公認的解決方案需要已知深度)
/*
!.gitignore
!wp-content/
!*/
這樣 wp-content 下的所有內容都不會被忽略。
這是我的解決方案,它假設結帳到wp-content
# Ignore everything except directories
*
!*/
# except everything in the child theme and its required plugin
!/themes/mytheme-child/**
!/plugins/my-plugin/**
# and this file
!.gitignore
和測試:
git version 2.20.1 (Apple Git-117)
$ git check-ignore -v .foo foo foo/ themes/foo themes/foo/bar themes/mytheme-child \
themes/mytheme-child/foo plugins/foo plugins/my-plugin plugins/my-plugin/foo .gitignore
.gitignore:2:* .foo
.gitignore:2:* foo
.gitignore:2:* foo/
.gitignore:2:* themes/foo
.gitignore:2:* themes/foo/bar
.gitignore:2:* themes/mytheme-child
.gitignore:6:!/themes/mytheme-child/** themes/mytheme-child/foo
.gitignore:2:* plugins/foo
.gitignore:2:* plugins/my-plugin
.gitignore:7:!/plugins/my-plugin/** plugins/my-plugin/foo
.gitignore:10:!.gitignore .gitignore
所以它正確地忽略了我不想要的一切,也沒有我想保留的東西。
根據https://docs.gitlab.com/ee/workflow/repository_mirroring.html,Gitlab配置了用於受保護分支的存儲庫鏡像
當代碼被推送到受保護的分支時,它將被鏡像到/opt/checkout/repo.git/
repo 中的登台和生產服務器。 下面的post-receive
掛鈎(在/opt/checkout/repo.git/hooks/post-receive
)然后將代碼檢出到工作目錄中。
#!/bin/bash
BRANCH=staging
GIT_DIR=/opt/checkout/repo.git
GIT_WORK_TREE=/opt/bitnami/apps/wordpress/htdocs/wp-content
# on push, checkout the code to the working tree
git checkout -f "${BRANCH}"
# ensure permissions are correct
sudo chown -R bitnami:daemon "${GIT_WORK_TREE}"
sudo chmod -R go-w "${GIT_WORK_TREE}"
你可以試試這個:
!**/themes/*
哪里:
這是我如何做到的:
# Ignore everything at root:
/*
# Except for directories:
!wordpress/(WordPress Site 1)/wp-content/themes/
!wordpress/(WordPress Site 1)/wp-content/themes/
!wordpress/(WordPress Site 1)/wp-content/themes/
# Except files:
!README
#Except files of type:
!*.txt
這對我有用。 允許您忽略除特定文件或文件夾之外的所有內容
macOS 山脈
我在同一條船上試圖在一個 repo 下管理一堆 Wordpress 網站。 我的目錄結構如下所示:
root (git repo)
(WordPress Site 1)/app/public/wp-content/themes
(WordPress Site 2)/app/public/wp-content/themes
(WordPress Site 3)/app/public/wp-content/themes
我只想跟蹤每個站點的app/public
文件夾中的項目。 我嘗試了此頁面中的示例以及此處的一些建議,並最終嘗試了此操作:
/(WordPress Site 1)/*
!(WordPress Site 1)/app
(WordPress Site 1)/app/*
!(WordPress Site 1)/app/public
這有效,但我必須忽略我試圖避免的每個站點的相同路徑。
然后我只是用*
替換了網站的名稱,這對我有用。 所以這就是我最終使用的:
/*/*
!*/app
*/app/*
!*/app/public
這有效地忽略了站點文件夾中的所有內容,同時為我在根目錄中創建的任何站點捕獲app/public
文件夾中的所有內容。
請注意,它不會忽略根目錄中的文件,只會忽略目錄:)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.