Remove untracked files with "git clean" [Practical Examples]

Quick cheat sheet to use git clean command

Git clean removes untracked files. It gives you the freedom to choose the files to clear from the working tree. That depends on the flag you supply it with. For instance,


git clean -n

to identify the files to delete,

git clean -f

to remove all untracked files,

git clean -i

to clear untracked files interactively, or

git clean -fd

to delete both untracked files and directories.

Sometimes you want to clear files depending on whether they are ignored or unignored. All you do is switch between capital and small letter x. Use capital X

git clean -fX

to delete ignored files. To remove both ignored and unignored files, use the small letter x as follows:

git clean -fx

You probably wonder what we mean by untracked, ignored, or unignored files. Most importantly, you want to explore the above git clean commands' usage practically. Read on to learn more.


The role of git clean in a typical git workflow

It would be best to understand untracked vs tracked or ignored vs unignored folders and files. The challenging part when trying to understand git clean is that we expect all files under git workflow to be tracked on running the init command.

Well, that is usually not the case. Here is the thing.

Initializing a repo warms up git to store versions of changes in a git database. Then, in the working directory, you create or modify files.

You can inform git to ignore a file or folder. That means git will not stage, commit or push the changes to a remote repository. For example, referencing the massive node_modules folder in the .gitinore file prevents pushing the changes to GitHub.

Otherwise, the files enter the second stage of the git workflow: the staging area, commonly referred to as the index. The files or folders become unignored. Git then temporarily stacks changes waiting for you to undo or commit them.

At the index, you can unstage files using a mixed reset

git reset HEAD

or the restore command.

git restore --staged <file>

You can then clear the untracked files, specifying one of the above git clean command flags.

Otherwise, you commit the files using a descriptive message. As a result, the files acquire a unique identifier called the commit hash.

You can untrack the committed changes through a hard reset by specifying a commit hash

git reset --hard <commit hash>

or the HEAD.

git reset HEAD^

git reset HEAD~N

Where Nrepresents the number of commits from the HEAD.

Then, the files get deleted from the working tree. So, no need to run git clean them.

However, the files you had not committed do not leave the working tree during a hard reset. That is when you deploy git clean to remove the files.

Let us set up a lab to view the git clean command in action practically.


My lab setup to explore git clean

Let us clone a repository, clone it and build a basic commit history to practice the git clean command.

Clone a remote repository

I am creating a repo on GitHub called git_clean

remote to explore git clean

I then grab its URL and clone it on the terminal.

Remove untracked files with "git clean" [Practical Examples]


Create files and directories

Let us start by creating a folder with two files.

cd git_clean

mkdir toDelete

cd toDelete

touch f1 f2

Navigate to the root directory.

cd ..

And check the repo status.

git status

We have an untracked directory.

Remove untracked files with "git clean" [Practical Examples]

Let us create a file at the root directory.

touch file

Recheck the status.

git status

We have a file and a directory with two files. That is enough to start practicing git clean using the six typical git clean commands: n, f, i, fd, fX and fx. Later we can combine the clean command with other commands such as git reset.


Example-1: List untracked files using git clean -n

Running the git clean command using the n flag

git clean -n

reveals files we can clear using other flags, as shown below.


Example-2: Remove a file using git clean -f

The f in the git clean command stands for a file. For instance, specifying it with a negative sign,

git clean -f

notifies git to delete all untracked files that are not part of a directory.

Running the command on our root directory discards the file called file. However, since f1 and f2 are enclosed inside a directory, git clean -f does not remove them from the working tree.

git clean

We can remove the files by pairing -f and -d in the clean command.


Example-3: Remove both files and directories using the git clean -fd

The d in the -fd stands for untracked directories. Let us use it to delete the directory called toDelete with files in it: f1 and f2.

git clean -fd

Recheck the status.

git status

All our tracked files and folders are gone. We only have file because it is already indexed, committed, and pushed. You can check undeleted files using the ls command.

git ls


Example-4: Interactively clear files by using the -i flag

Let us create an untracked file Before practicing interactive git clean.

touch file1

And interactively clear it.

git clean -i

Running the git clean command with the i flag prompts an option. Let us go with the first one (1).

interactive git clean

The file gets removed.


Example-5: Delete ignored files using git clean -fX

Using the -f flag alongside capital -X, deletes ignored files. Let us create and ignore files before applying the command.

touch .gitignore file1.txt file2.txt

echo file1 >> .gitignore

echo file2 >> .gitignore

We are referencing the text files in the .gitinore file. So, simply put, we are telling git, "Hey git, please don't track the two text files if we decide to stage, commit or push the text files to the remote git_clean repo."

On inspecting the file's contents, we see the two files inside the .gitinore file.

cat .gitignore

We can delete the files from the command line by running the clean command as follows:

git clean -fX


Example-6: Remove both ignored and unignored files using git clean -fx

Let us reintroduce the files we just deleted into the .gitinore file and another HTML file in the root directory.

touch file1.txt file2.txt index.html

echo file1.txt >> .gitignore

echo file2.txt >> .gitignore

Then remove them by combining the -f and small -x flags in the clean command.

git clean -fx

Both ignored and unignored files disappeared.

Remove untracked files with "git clean" [Practical Examples]

We have applied the above six commands on unstaged files. What if we want to clear indexed or committed files? That is when to combine git clean with other commands like the reset command.


Example-7: Combine git clean with other commands

Before deleting the untracked files, let us create, stage, and undo the change.

touch main.cpp
git add .

Checking the status confirms both files are indexed.

Remove untracked files with "git clean" [Practical Examples]

Let us unstage them.

git reset HEAD

The files got untracked, and we can now remove them from the working directory using the clean command.

git clean -f

Remove untracked files with "git clean" [Practical Examples]

And voila, both files got deleted thanks to the git clean and git reset combination!


Key Takeaway

Git clean is one of the most efficient commands to delete unwanted files or folders from the working tree. It enables you to delete the target path using the commands explained in this tutorial. Go ahead and apply it.


Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment