1

I know this is a bad practice but I changed the permission of /home and put a .gitignore there (I have a weird use case, let's keep it at that for now, will discuss more about it in the P.S.).

My .gitignore looks like:

/*
!/.gitignore

!/ismail/
/ismail/*

!/ismail/.zshrc

!/ismail/.searchmonkey/
/ismail/.searchmonkey/*
!/ismail/.searchmonkey/*

!/ismail/.config/
/ismail/.config/*

!/ismail/.config/user-dirs.dirs

!/ismail/.config/alacritty/
/ismail/.config/alacritty/*
!/ismail/.config/alacritty/alacritty.yml

The thing is this .gitignore does not work in /home , but works when I put it in /home/ismail/Desktop/test-git.

The permission of both directories:

/home/ismail/Desktop/test-git:master+%
total 20
drwxrwxr-x 4 ismail ismail 4096 Jun  6 12:26 .
drwxr-xr-x 3 ismail ismail 4096 Jun  6 12:24 ..
-rw-rw-r-- 1 ismail ismail  296 Jun  6 12:39 .gitignore
drwxrwxr-x 4 ismail ismail 4096 Jun  6 12:25 ismail

/home:master*+% ls -la
total 20
drwxrwxr-x  4 ismail ismail 4096 Jun  6 12:40 .
drwxr-xr-x 20 root   root   4096 May 31 22:50 ..
-rw-rw-r--  1 ismail ismail 2284 Jun  6 12:40 .gitignore
drwxrwxr-x 23 ismail ismail 4096 Jun  6 12:57 ismail

P.S. Please feel free to give any suggestion that meet my use case. My use case is, I track dotfiles of two linux distros. There are some common files and there are some distro specific files. So, my bare repos look like dotfiles_bare_repo_common, dotfiles_bare_repo_mint, dotfiles_bare_repo_zorin. So, for a given time, I need to put two .gitignore in /home/ismail (dotfiles_bare_repo_common and dotfiles_bare_repo_mint or dotfiles_bare_repo_common, dotfiles_bare_repo_zorin), which is not an option. So, I decided to move one step up and put common .gitignore in /home and distro specific .gitignore in /home/ismail, to maintain two .gitignore for same directory (.git/info/exclude does not work for me, as I need to push or clone the repos). I have also checked Can you set multiple gitignore files, in the same directory? and Are multiple .gitignores frowned on?. These also does not fit the bill.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87

1 Answers1

1

The thing is this .gitignore does not work in /home , but works when I put it in /home/ismail/Desktop/test-git.

That is because you are using "anchored" rules (my term for "rules starting with /"): the anchor "/" means the pattern is relative to the directory level of the particular .gitignore file itself... inside a Git repository.
And /home is not the inside a Git repository.

There is no native solution, but possible workarounds:

  • I would consider symlinks from each repository to the relevant .gitignore.
  • Or a content filter driver to generate the right .gitignore depending on the repository.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Can you please elaborate, `And /home is not the inside a Git repository.` I did not understand. – Ahmad Ismail Jun 06 '22 at 09:32
  • @AhmadIsmail Unless you did a `git init .` directly in `/home` (which is not a good practice), then `/home` is not part of a Git repository. – VonC Jun 06 '22 at 09:40
  • I have done `git --git-dir=/media/ismail/WDRed/_Working/_dotfiles-backup/ --work-tree=/home`. In both cases though there is not a `.git` directory, they are part of git. In one case my `.gitignore` works, in another case it does not work. – Ahmad Ismail Jun 06 '22 at 09:50
  • I mean the `"anchored" rule` work when I have `git --git-dir=/media/ismail/WDRed/_Working/_dotfiles-backup/ --work-tree=/home/ismail/Desktop/test-git` but does not work when I have `git --git-dir=/media/ismail/WDRed/_Working/_dotfiles-backup/ --work-tree=/home` – Ahmad Ismail Jun 06 '22 at 09:52
  • 1
    @AhmadIsmail I would recommend setting environment variable `GIT_WORK_TREE` and |GIT_DIR` to test if gitignore works better then. Use `git check-ignore -v` – VonC Jun 06 '22 at 10:40
  • `GIT_WORK_TREE` & `GIT_DIR` also does not work. – Ahmad Ismail Jun 06 '22 at 14:50
  • @AhmadIsmail That is expected, considering the `.gitignore` file is outside the designated worktree (like `/home/ismail/Desktop/test-git`). Hence my suggestion for symlink from `/home/ismail/Desktop/test-git/.gitignore` to `/home/.gitignore`. – VonC Jun 06 '22 at 16:17