How to remove untracked files in Git (2023)

During the development process, programmers and other Git users often end up with a lot of old and unwanted files. This can include prototypes, test data and computer generated files. While these files don't necessarily cause problems, deleting them increases efficiency and improves organization. This onecleanThe command is the fastest, safest and easiest way to delete these files. This guide explains how to use Git to remove untracked files and provides many examples to illustrate usage.clean.

Introduction to Untracked Files and Git

Before using thecleancommand, you need to understand what an untracked file is and why untracked files are important. There are many types of files in any Git project. A key difference is between tracked and untracked files.

tracked fileshave already been added to Git usingadd gitCommand. Once a file is added to Git, Git is fully aware of it. It knows the details and contents of the file and can retrieve that information if needed. Tracked files can be modified or unmodified. A modified file can be provided, also with theadd gitCommand. So a tracked file can be in one of several states, but Git will still track it.

The other category of files are theuntracked filesthat have not yet been added to the repository. Git can tell these files exist, but it doesn't know anything else about them. It does not crawl your content in its internal database. Since Git doesn't actively monitor these files, it can't take any action on them. For example, it cannot restore or restore the contents of these files. These files remain untracked until added to Git usingadd gitCommand.

One danger with untracked files is that information is not saved. In case of hard disk failure or accidental deletion or overwriting, all data will be lost permanently. Since Git never saved the file, it cannot restore it.

Of course, sometimes files are not tracked because they were never meant to be added to the system. Some examples might include experimental changes, test data, discarded prototypes, build artifacts, and obsolete files. Some applications also place a large number of automatically generated files in the working directory. It would be unwise to add them to Git.

Of course, these files can be left in an untracked state and usually cause no problems. However, there is some risk in keeping old and unnecessary files untracked for the following reasons:

  • It is possible accidentallyAddeForcethem, especially if you use the wildcard*Symbol.
  • They clutter up a workspace and take up unnecessary disk space. This can cause confusion when you return to a workspace later. It can be difficult to remember what the files were used for and whether they are still important.
  • They appear in the command output asGit-Statusas untracked files. This makes it harder to tell which files are really important.

There are several alternatives to get rid of these files. For example, you can simply delete them with thermCommand. However, this can be time-consuming and it's easy to accidentally delete the wrong file. Other alternatives likego to redefinehave a broader scope and can also unintentionally undo changes to tracked or committed files.

Also, some files shouldn't be tracked in Git, but are still important and shouldn't be deleted. A good example is a.cfgfile or create object files. In this case the.ignore.gitfile is used to tell Git to ignore this file. All files that match an exclusion rule in.ignore.gitare not displayed in the output ofGit-Statusand the various Git commands do not affect them.

It iscleanThe command is the easiest and most efficient way to remove untracked files in Git. This command is very targeted, easy to use and has no unwanted side effects. It uses the content of.ignore.gitfile and will not delete skipped files unless specifically instructed to do so. However, it is important to use this command very carefully. When a file is deleted, it is gone forever. Git cannot restore the content.

In summary, all files in a Git repository must be handled in one of three ways:

  • They can be added with the repositoryadd git.
  • They can be added to this.ignore.gitfile, causing Git to ignore it.
  • They can be removed with a variant ofcleanCommand.
before you start
  1. If you haven't already done so, create a Linode account and compute instance. see ourStarting with LinodeeCreate a compute instanceFührer.

  2. follow ourConfigure and secure a compute instanceInstructions for updating your system. You can also set the time zone, configure your hostname, create a limited user account and secure SSH access.

  3. It is also useful to consult our guidesIntroduction to GiteHow to Navigate the Linux Terminal and File System.

  4. OptionalGit must already be installed on Linode before trying the examples in this guide. This onegitThe package is usually pre-installed. To see if it's there, run the commandgit --version🇧🇷 If Git is already installed, this command displays the current version. If Git has not yet been installed, use the commandsudo apt install gitto install it.


This guide is written for non-root users. Commands that require elevated privileges are preceded by the prefixsudo🇧🇷 If you do not knowsudocommand, see theUsers and groupslead.

How to remove untracked files with Git Clean

This section shows how to remove untracked files in Git usingcleanCommand. This command has several options that allow users to control the command's behavior or output.cleanit also has an interactive mode that makes it easy to selectively delete a subset of files.

Try the examples in this section with a local Git repository. If Linode doesn't already have a repository, you can create one using thegit initCommand. run thoseinside sideCommand in the base directory of the repository.

No additional options is the commandcleandoes nothing. One of-EU,-n, or-fOptions usually need to be appended.

fatal: clean.requireForce defaults to true and neither -i, -n nor -f is specified; refuse to clean

git clean -fis probably the most common alternative. This forces Git to remove all untracked files without any further opportunity to refine or change the operation. When finished, it will show a summary of deleted files. This command is functionally equivalent to manually deleting files.


Always usecleancarefully. This action cannot be undone.

The example below shows howgit clean -fhandles a combination of untracked and tracked but uncommitted files. Before the operation,testdatei1.txtwas added but not adopted. the two filestestdatei2.txtetestdatei3.txtweren't added, so both aren't tracked in Git yet.

Changes to apply: New file: testfile1.txt Untracked files: testfile2.txttestfile3.txt

to runcleanUse of-fOption to delete the two untracked files.

git clean -f
(Video) Remove untracked files from GIT | git clean
Remover testfile2.txtRemove testfile3.txt

use any of theseGit-StatusorlsCommand to check if untracked files are deleted. This oneStatusThe command confirms that the tracked but uncommitted file is left intact. However, the untracked files no longer exist.

Changes to apply: new file: testfile1.txt

git clean -nreduces the associated riskssauberCommand. It lists all the files the command wants to delete, but it doesn't actually delete them. It acts as a "test" for the command and can be used to prevent accidental deletions.

Starting with the same setup asgit clean -nThe command lists the files that Git would "remove" in a real operation.

git clean -n
Would remove testfile2.txt Would remove testfile3.txt

After that, the files are still listed underGit-Statusbecause they weren't actually deleted.

...Untracked files: testfile2.txttestfile3.txt

It isgit clean -eThe option allows users to enter a specific exclusion pattern or filename. Files with this name or with this pattern will not be deleted.

In the example below, there are two untracked files calledtestfile4.mdetestdatei4.txt.

...Untracked files: testfile4.mdtestfile4.txt

Ignoretestdatei4.txtappend option during deletion process-e Testdatei4.txtfor thesauberCommand. East-fThe option is still required to force the remaining deletions.

git clean -f -e testfile4.txt

This is themdThe file is deleted, but theTXTfile is not.

...Untracked files: testfile4.txt

git clean -dis recursive and clears the current directory and all subdirectories.

git clean -f -d
(Video) How to Remove Local Untracked files from the current Git working tree
Remover arquivo/testfile2.txtRemover testfile4.txt

It is also possible to specify a directory to limit the file size.cleanOperation. The command only applies to untracked files in that directory. The following example appliescleanto all filesexampleDirectory.

git clean -f -d example

git configallows users to change the default settingcleanBehavior. This allowscleanto delete untracked files without the-fPossibility. makes effectivecleanequivalentgit clean -f🇧🇷 Use the command to add this option to the configuration filegit config clean.requireForce falso🇧🇷 For more information, see the Git documentation.Clean up.


It is-qoption is runningcleanin sleep mode. This meanscleandoesn't report the removed files, but still shows errors.

How to remove untracked files with Git Clean in interactive mode

It is-EUThe option is used to runcleanin interactive mode. It allows users to more accurately select the files to be deleted. This is a good option for situations where you want to exclude some but not all untracked files. It's also good for anyone who wants to be extra careful when running this command.

It is-EUOption displays a menu listing all available options. It also lists files that are currently scheduled for deletion.

To switch to idle mode, run thegit sauber -iCommand. Git displays the main menu for interactive mode.

git sauber -i
Would remove the following items: testfile2.txt testfile3.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>

The list of options is as follows.

  • sauber: This deletes the untracked files in the list in the same waygit clean -ftut.
  • filter by default: This option allows users to enter exclusion patterns.cleanignores untracked files with names matching any of these patterns. For example, the filter pattern*.TXTsaidcleando not delete untracked files that end with.TXTRenovation.
  • select by number: This displays a numbered list of untracked files. Users can use these numbers to select files for deletion.
  • ask everyone: this goes through the list of untracked files one by one and allows the user to choose whether to delete each file or not.
  • Go out
  • Help

From the main menu, users can select one of the first four options to remove files, exit interactive mode, or view the help page.

possibility1behaves exactly like thisgit clean -f🇧🇷 It removes all untracked files and its use is self-explanatory. It is usually selected after narrowing the file list using one of the other methods. The charactercstarts this option too.

The second option isfilter by default🇧🇷 It allows users to enter a pattern. Any filename that matches this pattern will be ignored. This will limit and possibly reduce the number of filessauberoption removed.

to use thefilter by defaultfollow the steps below in the main menu.

  1. choose an option2or enter thefKey. Git lists all untracked files and asks for another response.

    testfile2.txt testfile3.txtInput ignores default>>
  2. Enter the pattern you want Git to ignore. For example, to avoid deleting markdown files, enter the pattern*.md. It is*character acts as a placeholder. Any file matching this pattern will be ignored while the user remains in interactive mode. Git removes matching files from consideration and displays an updated list of eligible files.

    testfile2.txt testfile3.txtInput ignore pattern>>
  3. Add any other defaults you want Git to ignore at this point. When all patterns have been entered, use theENTERbutton to return to the main menu. Git then lists all files scheduled for deletion and waits for more entries.

    Would remove the following items: testfile2.txt testfile3.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>
  4. To clear the file list, type1orc.

    (Video) Git - Remove Untracked Files

    Remover testfile2.txtRemove testfile3.txt

It is often easier to specify files to exclude from a numbered list. Enter3orsto access theselect by numbersMenu. This option allows users to specify individual files, a range of files, or a wildcard.*Specification of all files. Only intentionally selected files can be deleted.

From the interactive main menu, follow these instructions to select and delete files by number.

  1. Enter the option3or use theskey to enter noselect by numbersMenu.

    1: Testfile2.txt 2: 3: Testfile3.txt4: Testfile4.txt Select items to exclude >>
  2. Enter the items to be excluded, separating them with commas. A range can also be used to select multiple consecutive items using the formatstart end🇧🇷 The following response selects elements1,2, e4, but no article3🇧🇷 Git highlights selected files with a*.

    * 1: testfile2.txt * 2: 3: testfile3.txt * 4: testfile4.txt Select items to exclude >>
  3. ChooseRETURNSto return to the main menu. Git lists selected files.

    Would remove the following items: testfile2.txt testfile4.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>
  4. Enter1orcto remove selected files.

    Remover testfile2.txtRemove testfile3.mdRemove testfile4.txt

The last method to delete files is theask everyonePossibility. type a4or oneoneto take advantage of this opportunity. It lists all the files one by one and asks the user if they should be deleted. answersjto delete the file ornto skip the file.

to use theask everyonemethod, follow these steps.

  1. Use o4oronekey to access theask everyonePossibility.

  2. Git displays the first file and asks if you want to delete it or not.

    Remover testfile2.txt [s/N]?
  3. Enterjto add the file to the exclusion list.

    (Video) git clean untracked files fdx
  4. Enterjornfor each of the remaining files.

  5. After the user reviews each file, Git deletes all selected untracked files.

    Remover testfile2.txt

The remaining two options are self-explanatory. possibility5(q) exits the interactive menu. possibility6is the help menu, which can also be accessed viah🇧🇷 The help menu explains the different options.

clean - start cleaning filter by default - exclude items from deletion select by numbers - select items to delete by number ask all - confirm each deletion (like "rm -i") exit - stop cleaning help - this screen? - Quick selection help
How to remove skipped files with Git Clean

It is.ignore.gitfile specifies which untracked files are to be ignored. This is the best option for dealing with configuration or system files that should not be checked or deleted. Users can run thelscommand and see those files, but they don't show up in the results of commands likeGit-Status🇧🇷 This file is usually created automatically and comes pre-installed with a list of common extensions that can be ignored. See the documentation for more information.ignore histo.

cleannormally ignores the files and filename patterns listed in.ignore.gitFile. This is consistent with the behavior of other Git commands, which also ignore these files by default.

However, this behavior can be overridden-xor-XPossibility. These options tell Git not to follow the default ignore rules. This means that Git can consider all files covered by.ignore.git🇧🇷 However, they differ in how they handle other untracked files.


Git still respects any exclusion rules added with-ePossibility.

In the example below, all untracked*.cProject files appear inGit-Status🇧🇷 However, the*.Öfiles no. This occurs because the.ignore.gitThe file contains the pattern*.Ö🇧🇷 This tells Git to ignore files with that extension.

File: .gitignore

Usually,cleandoes not affect untracked files that match.ignore.gitSample. To usegit clean -nconfirmedtestdatei3. owill not be considered for exclusion.

git clean -n
Would remove testfile3.c. Would remove testfile3.txt. Would remove testfile4.txt

To remove only skipped files and leave other untracked files untouched, usegit clean -f -X. It is-XThe option removes the untrackedfiles. Developers can use this option to force a perfectly clean build.

git clean -f -X

Git only removes untracked files that match a pattern in.ignore.git.

Remover testfile3.o

Use the command to remove all untracked files regardless of whether they are skipped or notgit clean -f -xinstead. This one-xThe option removes all untracked files, including skipped files.

git clean -f -x
Remova testfile3.cRemove testfile3.oRemove testfile3.txt
Final thoughts on removing untracked files in Git

This guide explains how to use it.cleanto remove untracked files in Git. While old untracked files aren't necessarily harmful, they do clog up a workspace and create a sense of clutter and chaos. There are several methods to remove them, but the easiest way to remove an untracked file in Git is thecleanCommand. This command deletes untracked files while leaving other files untouched.

It issauberThe command usually requires the force option-fbe effective. It also has a dry running mode and can run recursively.cleanfeatures a handy interactive mode that allows users to exclude files based on a predefined filter or select them from a numbered list. By default,cleanwill not delete files matching the.ignore.gitFile. However, one option only targets untracked skipped files, while another affects all untracked files. For more information about thecleanCommand, see theGit documentation.

More information

For more information on this topic, see the resources below. While they are provided in the hope that you will be helpful, please note that we cannot guarantee the accuracy or timeliness of any externally hosted material.

This page was originally published in

(Video) How to get rid of untracked files in git


How do I remove untracked files from git without deleting? ›

Using the git rm –cached Command

We've mentioned that git rm FILE will remove files from the index and local working tree by default. However, the git rm command provides the –cached option to allow us only to remove files from the repository's index and keep the local file untouched.

Does git reset remove untracked files? ›

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !

How to untrack all files in git? ›

Use git rm to Git untrack file.
  1. git rm --cached <filename>
  2. git rm <filename>
  3. git rm -r --cached <folder>
20 Aug 2020

How to remove a file from git? ›

The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.

Why do I have untracked files in git? ›

Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .

How do I untrack a tracked file? ›

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

How do I Unstage untracked files? ›

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.

How do I remove unchanged files in git? ›

23 Answers
  1. Remove every file from Git's index. git rm --cached -r .
  2. Rewrite the Git index to pick up all the new line endings. git reset --hard.

How to untrack files in git before commit? ›

To undo git add before a commit, run git reset <file> or git reset to unstage all changes.

How to remove all git files? ›

Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains. This Git repo remove command also allows you to delete the Git repo while allowing all of the other files and folder to remain untouched.

How to remove files in git commit? ›

If this is your last commit and you want to completely delete the file from your local and the remote repository, you can: remove the file git rm <file> commit with amend flag: git commit --amend.

How do you remove a file that Cannot be removed? ›

Method 1. How to Delete Files that Cannot be Deleted from Task Manager
  1. Go to Start, type Task Manager, and choose "Task Manager" to open it.
  2. Find the application that is currently using the file, and select "End task".
  3. Then, try to delete the file again on your Windows PC.
16 Sept 2022

How do I remove untracked files from remote? ›

git clean -f removes untracked files in the directory where you call it only. To remove other untracked files within the root directory, use git clean -f:/ Run the git clean -f command twice if a different repo manages the untracked directory whose files you want to remove from git.

Where do untracked files come from? ›

An untracked file in Git is a file created in the repository's working directory but not yet added to the repo's tracking index via the git add command.

How do I clean up my github repository? ›

git clean
  1. If you just clean untracked files, run git clean -f.
  2. If you want to also remove directories, run git clean -f -d.
  3. If you just want to remove ignored files, run git clean -f -X.
  4. If you want to remove ignored as well as non-ignored files, run git clean -f -x.
30 Sept 2022

How do I remove tracking files? ›

Google Chrome
  1. Click the vertical three-dots icon on the top right-hand corner and then click Settings.
  2. Click Privacy and security.
  3. Select Cookies and other site data.
  4. Select Block third-party cookies.
  5. Click Privacy and security.
  6. Click clear browsing data.
  7. Select All time from Time range.

How do I delete a tracking file? ›

In the Views pane, select the view that contains the files you want to remove. Select the file you want to remove. Select File | Remove.

How do I delete a file Trace? ›

In the Trace files section, click the Windows Update logs button or Remote installation logs button, depending on which trace files you want to delete. The list of trace files opens. In the list of trace files, select the file that you want to delete. Click the Remove button.

Why does my file say untracked? ›

Untracked files are the ones still not versioned—”tracked”—by Git. This is the state of new files you add to your repository. That basically means Git is aware the file exists, but still hasn't saved it in its internal database.

Which command is used to Unstage in git? ›

Using git reset to Unstage

Apart from restore you can also use git reset to unstage changes. If you're using a Git version older than 2.23, you will have to use reset because restore is quite a new feature in Git. Exactly like git restore --staged , this makes sure the file is NOT included in our next commit.

How do I remove unwanted files from pull request? ›

Here are the steps:
  1. delete file from your remote/origin branch and commit (won't be possible without it)
  2. git stash.
  3. git pull.
  4. git stash pop.

What happens if I remove .git file? ›

Deleting the . git folder does not delete the other files in that folder which is part of the git repository. However, the folder will no longer be under versioning control.

How do I ignore untracked files in git commit? ›

You can handle untracked files from a Git branch using these methods:
  1. A . gitignore file, which ignores the files and directories in a repository.
  2. The git clean -fx command, which removes untracked and tracked files.
  3. The git clean -fd command, which removes untracked files and directories.
29 Dec 2020

How to remove all files in git? ›

In order to delete files recursively on Git, you have to use the “git rm” command with the “-r” option for recursive and specify the list of files to be deleted. This is particularly handy when you need to delete an entire directory or a subset of files inside a directory.

How to remove files from git commit? ›

If this is your last commit and you want to completely delete the file from your local and the remote repository, you can:
  1. remove the file git rm <file>
  2. commit with amend flag: git commit --amend.

How to clear a git repository? ›

Deleting a repository
  1. On, navigate to the main page of the repository.
  2. Under your repository name, click Settings.
  3. Under Danger Zone, click Delete this repository.
  4. Read the warnings.
  5. To verify that you're deleting the correct repository, type the name of the repository you want to delete.

How do I clear all files? ›

Disk cleanup in Windows
  1. In the search box on the taskbar, type disk cleanup, and select Disk Cleanup from the list of results.
  2. Select the drive you want to clean up, and then select OK.
  3. Under Files to delete, select the file types to get rid of. To get a description of the file type, select it.
  4. Select OK.


1. How to delete untracked files in git with only one command
(AWS Cloud in Easy steps)
2. GiT Lab 12: Remove local (untracked) files from my current GiT branch
(Gabe Brown)
3. Git: How to delete all untracked files? (4 Solutions!!)
(Roel Van de Paar)
4. Git Tutorials: Deleting Untracked files, directory and ignored files, Git Clean Command | part 30
(Vashishth Muni Singh)
5. How to add all untracked files in Git
(Sagar S)
(Host Promo)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated: 02/21/2023

Views: 6488

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.