Skip to content

Remove Untracked Git Files

By Jasper Frumau

Sometimes you want to remove untracked git files. This simply because you are trying to do a git pull request to test things locally and you get an error telling you you need to merge the files or remove them altogether. Right then you may not be interested in what you tried out and just want the latest. So what to do?

Git Pull Error

The error you get on git pull will be something along the lines of:

git pull
Updating d52fc724..ff3ed39a
error: The following untracked working tree files would be overwritten by merge:
 config/horizon.php
 public/vendor/horizon/css/app.css
 public/vendor/horizon/css/app.css.map
 public/vendor/horizon/img/favicon.png
 public/vendor/horizon/img/horizon.svg
 public/vendor/horizon/img/sprite.svg
 public/vendor/horizon/js/app.js
 public/vendor/horizon/js/app.js.map
 public/vendor/horizon/mix-manifest.json

Even using –force won’t help in this case. You will still be requested to deal with the merger or removal of files.

Git Clean

To get past this bottleneck you can do a:

git clean -n

do deal with all. To do a dry run to make sure nothing bad happens and remove files and directories that are untracked you can do a

git clean -f -d --dry-run

Once you have decided it is all good you can do the same command without –dry-run. Once that is done you will see pulls will work again:

git pull
Updating d52fc724..ff3ed39a
Fast-forward
 app/Console/Kernel.php                                            |    6 +-
 app/Exceptions/Handler.php                                        |   36 +-
 app/Http/Controllers/Auth/RegisterController.php                  |   10 +-