← Back to context

Comment by jmholla

3 days ago

Here's a little extra magic so that you don't even need to be in the root of the repository to create such a temporary file (you'll have to change the readlink invocation if you're on MacOS):

    git-ignore-local () {
      root=$(git rev-parse --show-toplevel)
      path=$(readlink -f "$1")
      # ${path#${root}} should suffice if you don't have realpath
      relpath=$(relpath -m --relative-to="$root" "$path")
      echo "$relpath" >> "${root}.git/info/exclude"
    }

Edit: You could also put the function contents as an executable on your PATH called `git-ignore-local` then it becomes something you can invoke as `git ignore-local`.

FYI: you may want to check `git rev-parse --show-cdup`

- in root of the repo, it prints empty string

- if you're 1 level deep it prints `../`

- if you're 2 levels deep it prints `../../`

One minor drawback: inside `.git` subfolder, it always prints empty string too.