Read Anthropic’s case study about Graphite Reviewer

Git ignore untracked files

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


In Git, managing untracked files—those new files in your working directory that haven't been added to the staging area yet—can be is necessary for maintaining a clean workspace and ensuring that only relevant changes are included in commits. This guide delves into various methods for handling untracked files in Git.

Untracked files are typically new files in your repository that Git hasn't been told to track yet. These can include log files, compiled code, local configuration files, and other temporary or non-essential files. Managing these files properly is essential to keep your repository clean and to prevent the addition of unnecessary or sensitive files to the version control history.

The simplest and most effective way to permanently ignore untracked files is by specifying them in a .gitignore file. This file contains patterns that match file names or directories that Git should ignore.

Example .gitignore file:

Terminal
# Ignore all log files
*.log
# Ignore specific directory
node_modules/
# Ignore specific file
config/local.env

Create or modify the .gitignore in your project's root directory and add patterns for the files or directories you want to ignore. This setup prevents these files from being shown as untracked and keeps them out of your repository.

While the .gitignore file handles most needs to ignore files, sometimes you may need to ignore untracked files temporarily without adding rules to .gitignore. Here are some ways to do it:

When you switch branches, untracked files do not impact the checkout process as they are not tracked by Git, so you can switch branches without concern by running:

Terminal
git checkout <branch-name>

Untracked files in your working directory are not affected by the switch; they remain in the working directory as untracked files, unless they somehow conflict with the branch you're checking out, (ie. if files with the same name exist on the other branch).

To add files to staging without adding untracked files, use:

Terminal
git add -u

This command updates the staging area with changes from tracked files only, ignoring any untracked files.

If your workspace is cluttered with untracked files that you want to clean up, Git provides tools to remove them.

To remove untracked files from your directory, use:

Terminal
git clean -f

To remove untracked directories in addition to untracked files, use:

Terminal
git clean -fd

Caution: git clean is a destructive command that will permanently delete files from your working directory. Always double-check which files will be removed (using git clean -n for a dry run) before running the command.

When you perform a hard reset, Git resets your staging area and working directory to match a specified commit. However, git reset --hard does not affect untracked files.

If you've done a hard reset and still see untracked files, they remain because reset --hard only affects tracked files. Use git clean as described above to remove these untracked files if necessary.

Terminal
git reset --hard HEAD
git clean -fd # Remove untracked files and directories

The combination of these two commands will set your local working directory to the state exactly at the point of the last commit(the HEAD), deleting all untracked files in the process.

  1. Regularly update .gitignore: As your project evolves, so should your .gitignore file. Regularly review and update it to reflect new types of generated files or new directories that should be ignored.
  2. Be cautious with git clean: Since git clean can permanently delete files, use it cautiously. Always perform a dry run first using git clean -n.
  3. Document ignore decisions: When adding patterns to .gitignore, document why certain files are ignored, either in comments within the .gitignore file itself or in your project documentation. This transparency helps new contributors understand the project setup.

For further reading on untracked files in Git, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2