When working on large projects, we often generate files that we don't want to commit but need to keep around - build artifacts, local notes, or temporary configurations. Here's how I manage these files using a personal directory pattern and a handy script to organize untracked files.

The .username Directory Pattern

I maintain a .$(whoami) directory in my projects (where whoami expands to my username) for storing personal, project-related files that shouldn't be tracked by git. This directory is automatically ignored thanks to a global gitignore rule in my home directory's .gitconfig:

[core]
  excludesfile = ~/.gitignore

And in ~/.gitignore:

.phil/

This pattern ensures that any .phil directory is ignored. You will of course might want to change the name to your username.

The Untracked Files Organization Script

Here's a bash script that helps maintain cleanliness in git repositories by moving untracked files into a dated directory within your personal space:

#!/bin/bash

# Create directory with username and current date
DEST_DIR=".$(whoami)/untracked/$(date -I)"
mkdir -p ${DEST_DIR}

# Get git repo root directory
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -ne 0 ]; then
    echo "Error: Not in a git repository"
    exit 1
fi

# Get list of untracked files
while IFS= read -r file; do
    if [ -n "$file" ]; then
	# Create the directory structure
	dir_path=$(dirname "$DEST_DIR/${file}")
	mkdir -p "$dir_path"

	# Move the file
	mv "$file" "$DEST_DIR/${file}"
	echo "Moved: $file"
    fi
done < <(git ls-files --others --exclude-standard)

How It Works

  1. Creates a dated directory within your personal space (.username/untracked/YYYY-MM-DD)
  2. Identifies all untracked files in your git repository (excluding those in .gitignore)
  3. Preserves the directory structure while moving files to the new location
  4. Provides feedback about moved files

Key Features

  • Date-based Organization: Files are stored in dated directories, making it easy to track when they were set aside
  • Path Preservation: Maintains the original directory structure relative to the repository root
  • Gitignore-aware: Respects your .gitignore rules
  • User-specific: Uses your username to create a personal space

Usage

Save the script somewhere in your PATH (e.g., ~/bin/git-organize-untracked), make it executable:

chmod +x ~/bin/git-organize-untracked

Then run it from any git repository:

git-organize-untracked

This approach has several benefits:

  • Keeps your working directory clean
  • Preserves files you might need later
  • Maintains a history of untracked files by date
  • Keeps personal files separate from the project

The .username directory pattern combined with this script provides a clean, organized way to manage personal files and temporary artifacts in your git projects without cluttering your working directory or git status output.


If you liked this post, please consider supporting our Free and Open Source software work – you can sponsor us on Github and Patreon or star our FLOSS repositories.