George Nance

Deleting untracked files from git

The problem:

Have you ever tried to pull down some new changes and come across this error?

bash
error: The following untracked working tree files would be overwritten by merge:
resources/views/untrackedFolder/untrackedFile.blade.php
resources/views/untrackedFolder/anotherUntrackedFile.blade.php
Please move or remove them before you can merge.
Aborting

Untracked files huh? Okay, that doesn't sound too difficult. You should be able to run

git reset --hard

But wait

They are still there! What gives !?

wait what

The reason is because git reset --hard only removes files that are already a part of the repo

Solution

From the git docs, we will find this command:

bash
git clean -n

-n this flag will show you what files will be removed.

You should use this flag first to determine what is going to be removed.

bash
git clean -f

-f this flag means it will delete the files permanently.

Other useful commands

git clean -fd will also delete folders

git clean -fX removes ignored files

git clean -fx removes both ignored and non-ignored files

There you go, you should have a clean repository that has the changes committed before.

A web development newsletter that you will like

My goal with this blog is to create meaningful content for web devs, and my newsletter is no different! It's sent monthly, and includes upcoming posts, and other stuff I think you'd enjoy. I will never spam you and you can unsubscribe at any time.