How to remove untracked files with git
When we develop, we often create temporary files that we do not want to commit to our repository.
This article shows you how to remove untracked files with Git.
Please note that for the simple example below, it is more convenient to use the standard command to delete files: rm
.
Suppose we have three untracked files in our working directory.
> git status
Your branch is ahead of 'origin/develop' by 30 commits.
Untracked files:
bar.txt
baz.txt
foo.txt
nothing added to commit but untracked files present
The best way to remove the unwanted files is to use the interactive version of git clean
like so:
> git clean -i
Would remove the following items:
bar.txt baz.txt foo.txt
*** Commands ***
1: clean 2: filter by pattern 3: select by numbers
4: ask each 5: quit 6: help
What now> 1
Removing bar.txt
Removing baz.txt
Removing foo.txt
The interactive mode is excellent because we see what we will delete.
For the next example, suppose we still have the three files in the working directory.
With the option -n
, we tell Git that we only want to see which files would be deleted without actually deleting anything.
> git clean -n
Would remove bar.txt
Would remove baz.txt
Would remove foo.txt
We decide to remove all three files and therefore use the -f
option:
> git clean -f
Removing bar.txt
Removing baz.txt
Removing foo.txt
If you also want to remove files which are ignored by Git because they are listed in .gitignore
you can use the following command:
> git clean -fx
git-clean Summary
Git Command | Description |
---|---|
git clean -f | Remove untracked files |
git clean -fx | Remove untracked files and ignored files |
git clean -fX | Remove ignored files only |
git clean -i | Remove files interactively |
git clean -fd | Remove untracked directory |
Please be careful when deleting files because there is no trash bin when operating on the command line.
Posted on CuteMachine.
Written by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.