Comment by jakub_g

4 days ago

I'd like to emphasize the `.git/info/exclude`, which is a "repo-local gitignore", i.e. only for you and only for this repo.

Useful when you want to create a temporary file to help you e.g. with a bug investigation, and make sure it stays untouched while you switch branches, and to avoid accidentally committing it.

I have a shell alias like this:

    git-ignore-local () {
      echo "$1" >> .git/info/exclude
    }

and use it like `git-ignore-local myfile.ext`

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.

Wow TIL thankyou! I've got a bunch of small things like this in my current project that always complicate my PRs, this will solve that handily.