JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Ignore untracked files in Git

Author:JIYIK Last Updated:2025/04/20 Views:

This article will discuss two methods that can be used to ignore untracked files in a Git repository. If there are multiple untracked files and folders in your local repository, running the git status command will output many lines.

Let’s get straight to the point.


Ignore untracked files in Git

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

This 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

git statusThe command shows two files ready for commit and several untracked files. We want to exclude the untracked files from the output without deleting them.

How will we handle this?

git statusAdd the -uno parameter to the command ; That's it. 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 -uno parameter is an alias for --untracked-files=no .

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

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

We use the following command.

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

This will permanently hide files that are not currently tracked in your repository.

请注意You need to have an existing .gitignore file for the command to work.

If you don't have that file, gitignore will ignore itself. If you don't have a .gitignore file, use the following command.

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

git status --porcelainThe <head> section will be run before the .gitignore file is created. We use <head> git status --porcelaininstead of <head> git status --shortto get the output in an easily parsable format.

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

cut -c4-This paves the way for untracked files in part by removing the first three characters of each line.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Ignore everything except certain files in Git

Publish Date:2025/04/20 Views:151 Category:OPERATING SYSTEM

This article outlines the steps to make Git ignore all but a few files in a Git repository. The .gitignore file is a useful Git utility that allows us to tell Git which files to track and which files not to track. If you want your .gitignor

Get the current branch in Git

Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM

This article describes how to use git branch the command and git symbolic-ref the command to get the branch you are currently working on in git. Get the current branch Use git branch the command to get a list of all branches. The branch nam

Update branches from master in Git

Publish Date:2025/04/20 Views:142 Category:OPERATING SYSTEM

When working in Git with many developers and analysts working on various branches simultaneously, we may encounter many problems. A common problem is when one team member makes changes in his local branch while others work on that remote br

Commit changes to a Git branch

Publish Date:2025/04/20 Views:65 Category:OPERATING SYSTEM

In this article, you'll learn how to save commits to a new or existing branch in Git. This article explains how to move commits to: A new branch Existing branches You’ll often find yourself committing the same staged changes to different

Tagging an older commit in Git

Publish Date:2025/04/20 Views:115 Category:OPERATING SYSTEM

This article outlines the steps required to tag old commits in Git. We use git tags to mark specific points in our commit history as important. Typically, a git tag marks a stable release or an important milestone in a project. How do you t

List all tags in Git

Publish Date:2025/04/20 Views:120 Category:OPERATING SYSTEM

This article will teach us how to list all tags in Git. Git is a version control system that tracks changes in project directories using a Git repository. Changes made to files are tracked in a Git repository using commits. Using tags, we c

Recovering a reverted Git commit

Publish Date:2025/04/20 Views:197 Category:OPERATING SYSTEM

This article outlines the steps required to revert a reverted Git commit. By the end of this article, you will have the necessary knowledge to recover a reverted commit without rewriting your commit history. Recovering a reverted Git commit

Git merge repository

Publish Date:2025/04/20 Views:160 Category:OPERATING SYSTEM

When working on a project with multiple team members having separate repositories for each team, at some point in time we come across a situation where we want to combine these separate repositories into one master repository to deploy all

Git push to another branch with a different name

Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM

git push It has a rich set of options that allow you to use the full power of Git. One of them is its source:destination refspecs parameters. We use these git push to go to a specific branch with a name of our choosing. Finally, we'll see s

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial