How to Ignore Untracked Files in Git

John Wachira Feb 02, 2024
How to Ignore Untracked Files in Git

This article will discuss two methods you can use to ignore untracked files in our Git repository. Running the git status command will output many lines if you have several untracked files and folders in your local repository.

Let’s jump right in.

Ignore Untracked Files in Git

We will cover the two methods using two different scenarios. In our first case, we have the repository Delftscopetech.

Here is what we have in our repository.

pc@JOHN MINGW64 ~/Documents/GitHub/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Delft.pdf
        new file:   sample.ph
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Example Code 2
        Example Code 2.php
        Example Code 3.php
        Example code 1.php
        downloadpdf.php
        htmllinkpdf.html
        insert.php

The git status command shows two files ready to commit and several untracked files. We wanted to exclude the untracked files from the output without deleting them.

How would we go about this?

Add the -uno parameter to the git status command; it’s that simple. Let’s try it out with our repository.

pc@JOHN MINGW64 ~/Documents/GitHub/Delftscopetech (main)
$ git status -uno
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Delft.pdf
        new file:   sample.php
Untracked files not listed (use -u option to show untracked files)

The parameter -uno is an alias for --untracked-files=no.

If we run the git status command without the parameter, it will show the untracked files.

What if we want to permanently ignore untracked files? How do we do that?

We use the command below.

git status --porcelain | grep '^??' | cut -c4- >> .gitignore

This will permanently hide current untracked files in your repository. Note that you need to have an existing .gitignore file for the command to work.

If you do not have the file, the gitignore will ignore itself. Use the command below if you do not have the .gitignore file.

echo "$(git status --porcelain | grep '^??' | cut -c4-)" > .gitignore

The git status --porcelain part will run before the .gitignore file is created. We use git status --porcelain instead of git status --short to get the output in an easy-to-parse format.

The grep '^??' part will filter out all lines starting with ??. According to the manual, these lines correspond to the untracked files in the repository.

The cut -c4- part paves the way to the untracked file by removing the first three characters of every line.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Ignore