How to delete untracked files from the current git branch
September 05, 2019
The problem:
Have you ever tried to pull down some new changes and come across this error?
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
Okay, that doesn't sound too difficult. Lets just try to do a git reset --hard
.
But wait, they are still there! What gives !?
Well git reset
doesn't want to step on your toes and delete any files that it doesn't know about. Thats why git reset --hard
will not remove files that have not been added to version control
So how do I remove these untracked files from my git branch?
From the git docs, we will find this command:
git clean -n
This command will show you the files that will be deleted that are not under version control. You should run this before the next command to see the damage you are about to do.
git clean -f
This command will delete the files permanently.
But what about folders and ignored files?
- To delete folder, run
git clean -fd
- To remove ignored files, run
git clean -fX
- To remove ignored and non-ignored files, run
git clean -fx
There you go, you should have a clean repository that only has the changes that have been committed before.