Managing scratch files in a git project
You can avoid checking in certain files in a git project directory by using a .gitignore
file.
Then there are files which you don't want to check in on any project directory, such as editor/IDE configuration files. Instead of “polluting” .gitinore
files with such entries, it's better to set up a global .gitignore
file:
$ touch ~/.gitignore
$ git config --global core.excludesfile ~/.gitignore
Here's the contents of mine:
*~
.DS_Store
.idea
*.iml
\#*#
*.hsp
*.sav
*.scpt
/scratch/
.vscode/
coverage.out
.Guardfile
.config.ru
(You can see some of my history there: macOS, IntelliJ, VS Code, Go, etc.)
But notice this entry:
/scratch/
This means that any directory in the project named scratch
will be ignored, along with its contents.
Having such a git scratch directory turns out to be really handy:
- The files are visible to your editor/IDE.
- It's easier to remember where you put such files compared to storing them outside the project directory.
- You can even nest directories in the scratch directory.
- If you finish with the project and delete the project directory, the files are cleaned up too.
A recent example is from a Rust project:
$ cargo expand > scratch/generated.rs
This puts the generated code in a file which my editor will recognise as Rust code, and will display with syntax highlighting. I definitely didn't want to check that file in!
Other files which are suitable for the scratch directory are:
- Hacky tests or fixtures I'm too embarrassed to check in.
- Dependencies I don't control, but which I need to modify, e.g. for debugging.
- TODO lists and other rough notes.
- Output files from static analysis or code coverage.
- Old versions of code files from the project which I want to refer to quickly.
- Old project executables for comparing with the current behaviour.
- Downloaded PDF manuals relating to the project.
I'm sure you'll find many other uses for git scratch directories.