Using .gitignore as an Allowlist

Issue

I’d like to use git to backup my ~/.config folder, and I would like to just upload specific folders. My goal is to make my ~/.config folder into a Git Repo and adding a ~/.config/.gitignore that allows me to just do git add . && git commit, and just having a hand selected set of folders committed to the Repo.

Now my Problem is, that when I try to make a .gitignore that ignores everything and then excludes the selected Directorys from that, git just ignores everything and does not care about the Directories I’m trying to be not ignored.

Here’s my .gitignore:

*
!.gitignore
!Jetbrains/**
!Google/**
!autostart/**
!git/**
!nvim/**
!obsidian/**
!polybar/**

Solution

According to the .gitignore rules, unanchored patterns are applied at every level, and an ignored directory will never even be scanned. So:

/*/
!/Jetbrains
!/Google
!/autostart

and so forth. "Ignore all toplevel directories" followed by "except these", and none of those patterns matches anything else.

Answered By – jthill

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published