Categories
branch git git-branch

How to remove local (untracked) files from the current Git working tree

7710

How do you delete untracked local files from your current working tree?

5

  • 101

    This interactive git cheat sheet ndpsoftware.com/git-cheatsheet.html shows the git workspace (google gives you better results with “workspace” than “working copy”).

    – qneill

    Feb 1, 2013 at 15:43


  • 36

    Note: if you only want to remove some untracked files, but not all of them, git clean has now an interactive mode! See my answer to this other question: git 1.8.4+

    – VonC

    Jul 23, 2013 at 6:00


  • Before you post a new answer, consider there are already 25+ answers for this question. Make sure that your answer contributes what is not among existing answers

    Jun 15, 2017 at 15:30

  • 8

    To clarify for the understanding of the uninitiated and those new to Git – run git status and if it shows a file as untracked, and you don’t want that file in the repo, you can just go to your filesystem and delete or move it. This will not do anything bad to your local repo or to Git. You can also use git clean or some variation in the answers below, including the interactive version to delete just selective files, but interactive mode can be tedious. Whatever you do, make sure you understand what git clean will delete or use --dry-run to have it tell you without deleting anything.

    – LightCC

    Sep 2, 2017 at 6:15

  • 5

    If the files are not yet being tracked, couldn’t you just remove them without git? rm files-to-be-deleted

    – mhatch

    Mar 14, 2018 at 20:48

9477

git-clean – Remove untracked files from the working tree

Synopsis

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

Description

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.

If any optional <path>... arguments are given, only those paths are affected.


Step 1 is to show what will be deleted by using the -n option:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

Clean Step – beware: this will delete files:

# Delete the files from the repository
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to “true” (the default) in your configuration, one needs to specify -f otherwise nothing will actually happen.

Again see the git-clean docs for more information.


Options

-f, --force

If the Git configuration variable clean.requireForce is not set to
false, git clean will refuse to run unless given -f, -n or -i.

-x

Don’t use the standard ignore rules read from .gitignore (per
directory) and $GIT_DIR/info/exclude, but do still use the ignore
rules given with -e options. This allows removing all untracked files,
including build products. This can be used (possibly in conjunction
with git reset) to create a pristine working directory to test a clean
build.

-X

Remove only files ignored by Git. This may be useful to rebuild
everything from scratch, but keep manually created files.

-n, --dry-run

Don’t actually remove anything, just show what would be done.

-d

Remove untracked directories in addition to untracked files. If an
untracked directory is managed by a different Git repository, it is
not removed by default. Use -f option twice if you really want to
remove such a directory.

10

  • 341

    git clean -f works only in the directory where it’s called (and subdirectories). If you want to clean the whole working copy, you should call it in its root directory.

    Mar 8, 2013 at 10:51

  • 23

    It is also removing all files inside .gitignore. I need to delete only files/folders which are new and not in .gitignore

    – Kostanos

    Aug 5, 2013 at 23:02

  • 30

    @Kostanos If you don’t want to remove files that are in .gitignore, then do not provide the -x flag.

    – Lo-Tan

    Aug 23, 2013 at 18:44

  • 63

    git clean -f :/ works as if you had run it in the root repo dir. See also later answers also accounting for submodules with git clean -ffxd :/

    – here

    Oct 16, 2014 at 20:38

  • 4

    After struggling and cursing for 45 solid minutes, I found @EduardoBezerra’s comment… I think the OP should edit his answer to emphasize git clean’s behavior – of only deleting stuff in the directory you are – and subdirectories thereof. It’s not clear from git help or from the answer itself.

    Sep 20, 2018 at 15:43


1200

Use git clean -f -d to make sure that directories are also removed.

  1. Don’t actually remove anything, just show what would be done.

    git clean -n
    

    or

    git clean --dry-run
    
  2. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use the -f option twice if you really want to remove such a directory.

    git clean -fd
    

You can then check if your files are really gone with git status.

3

  • 123

    As previously stated, good to dry-run it with git clean -n -d

    – Ms01

    Dec 9, 2013 at 10:54

  • 17

    Same thing is to do git clean -nd and git clean -fd.

    – Micer

    Jul 10, 2017 at 9:13


  • Using -n or --dry-run by themselves did nothing. As per @Ms01 and @Micer, you need to add -d, as in git clean -nd to show what it is going to remove.

    – joehanna

    Sep 28, 2021 at 23:25


544

I am surprised nobody mentioned this before:

git clean -i

That stands for interactive and you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files. Overall, still faster than running the mandatory --dry-run before the real cleaning.

You will have to toss in a -d if you also want to take care of empty folders. At the end, it makes for a nice alias:

git iclean

That being said, the extra hand holding of interactive commands can be tiring for experienced users. These days I just use the already mentioned git clean -fd

3

  • 18

    @pal4life It was added in 1.8.4, you might be running an older version of git? github.com/git/git/blob/master/Documentation/RelNotes/1.8.4.txt

    Sep 30, 2014 at 12:37

  • I like this – I’m more comfortable having it in my bash history than any of the other options, because it’s no big deal if I accidentally ctrl-r or ctrl-p to it.

    – csvoss

    Oct 21, 2019 at 22:38

  • 1

    Could it be that git clean -i works only if called from the root of the working copy?

    Jun 4, 2020 at 10:43