Untracked files can clutter up your Git working tree and mess things up down the road. Sometimes these untracked files can be text or other files you don't want in your remote repository or those you mistakenly created one way or another after staging a commit.

Whatever the case may be, it's always helpful to clean your Git working tree to remove these files.

What Are Untracked Files During a Git Commit?

If you've updated some existing files in your project and also added new files locally and you wish to push that update to your remote repository on GitHub, Git requires that you stage these changes for commit.

A mere update you make to pre-existing files that you've committed already doesn't remove them from tracked files.

When you stage an update for commit, new files also get staged with them, and Git adds them to tracked files. However, new files that you add to your project after staging your commit don't get tracked.

These can be unimportant or leftover files that you temporarily used or those that surface one way or another after merging or pushing some changes. Consequently, these untracked files still lurk around your working tree, and when you run git status, Git returns them as untracked files.

You can delete these files by cleaning your Git working tree. Otherwise, if you still think you need some of them locally, you can add them to the .gitignore file. Files that you add to .gitignore won't be affected by the clean-up, not if you decide to include them.

Cleaning Git is as easy as deleting a Git branch locally or remotely. Let's see the various ways you can clean Git to delete untracked files or folders below.

How to Clean Git and Remove Untracked Files or Folders

Before removing untracked files, you should double-check to ensure that you want to delete them. To do that, run the code below:

        git clean -d -n

The command returns all untracked folders and files that Git will remove from your working tree.

To remove these files and directories, run:

        git clean -d -f

To remove files only without deleting folders, use:

        git clean -f

Although the above methods don't remove files listed in .gitignore, you can use the command below to clean items listed in the .gitignore file as well:

        git clean -fx

To remove only ignored files without including other files, this time, change the lower case "x" to an upper-case "X":

        git clean -fX

To check if there are still unstaged files in your working tree, run the following command:

        git status

You can also clean Git interactively by using:

        git clean -i

To include files in .gitignore in the interactive clean mode, use:

        git clean -ix

To clean files listed in .gitignore only using the interactive mode, run the following command. Ensure that you use the uppercase "X" this time:

        git clean -ifX

Once the interactive mode comes up, you can choose to filter the files by number or string patterns. You can also select the ask if option to double-check each file before deleting it. If you like, you can select the clean option to remove the files straight away.

Running git status gives you current staging information, and if there are any unstaged files or folders, it lets you know as well.

Still See Removed Files as Untracked After Running Git Clean?

However, after checking the Git status, if files you've previously removed are still appearing under the untracked files section, then you should clear the Git cache. Then run git clean again to remove the files.

Related: How to Clone a Repository Using GitHub Desktop

To clear your Git cache:

        git rm -r --cached [filename]

If you have more than one file still appearing after cleaning Git, then use the following command to clear the Git cache for each file:

        git rm -r --cached [filename1] [filename2] [filename3]...

However, ensure that you add the file extension for each of the files and remember to clean Git again to remove them.

Why Do You Need to Clean Git to Remove Untracked Files?

Sometimes, you want to tidy things up in your Git working tree before leaving a project for another time. You're then likely to push or merge the last changes you made to the project to ensure that you can pick up exactly from where you left off the next time.

But while pushing or merging, some files you don't want in your repository can drop in by mistake.

Failure to check such files and remove them can mess up your remote repository, as they get pushed the next time you're making an update to your remote repository. In addition to that, such files can break things when deploying to platforms like Heroku that uses git for deployment.

So: keep your Git clean!