During the development process, programmers and other Git users often end up with a lot of old and unwanted files. This can include prototypes, test data and computer generated files. While these files don't necessarily cause problems, deleting them increases efficiency and improves organization. This oneclean
The command is the fastest, safest and easiest way to delete these files. This guide explains how to use Git to remove untracked files and provides many examples to illustrate usage.clean
.
Before using theclean
command, you need to understand what an untracked file is and why untracked files are important. There are many types of files in any Git project. A key difference is between tracked and untracked files.
tracked fileshave already been added to Git usingadd git
Command. Once a file is added to Git, Git is fully aware of it. It knows the details and contents of the file and can retrieve that information if needed. Tracked files can be modified or unmodified. A modified file can be provided, also with theadd git
Command. So a tracked file can be in one of several states, but Git will still track it.
The other category of files are theuntracked filesthat have not yet been added to the repository. Git can tell these files exist, but it doesn't know anything else about them. It does not crawl your content in its internal database. Since Git doesn't actively monitor these files, it can't take any action on them. For example, it cannot restore or restore the contents of these files. These files remain untracked until added to Git usingadd git
Command.
One danger with untracked files is that information is not saved. In case of hard disk failure or accidental deletion or overwriting, all data will be lost permanently. Since Git never saved the file, it cannot restore it.
Of course, sometimes files are not tracked because they were never meant to be added to the system. Some examples might include experimental changes, test data, discarded prototypes, build artifacts, and obsolete files. Some applications also place a large number of automatically generated files in the working directory. It would be unwise to add them to Git.
Of course, these files can be left in an untracked state and usually cause no problems. However, there is some risk in keeping old and unnecessary files untracked for the following reasons:
- It is possible accidentally
Add
eForce
them, especially if you use the wildcard*
Symbol. - They clutter up a workspace and take up unnecessary disk space. This can cause confusion when you return to a workspace later. It can be difficult to remember what the files were used for and whether they are still important.
- They appear in the command output as
Git-Status
as untracked files. This makes it harder to tell which files are really important.
There are several alternatives to get rid of these files. For example, you can simply delete them with therm
Command. However, this can be time-consuming and it's easy to accidentally delete the wrong file. Other alternatives likego to redefine
have a broader scope and can also unintentionally undo changes to tracked or committed files.
Also, some files shouldn't be tracked in Git, but are still important and shouldn't be deleted. A good example is a.cfg
file or create object files. In this case the.ignore.git
file is used to tell Git to ignore this file. All files that match an exclusion rule in.ignore.git
are not displayed in the output ofGit-Status
and the various Git commands do not affect them.
It isclean
The command is the easiest and most efficient way to remove untracked files in Git. This command is very targeted, easy to use and has no unwanted side effects. It uses the content of.ignore.git
file and will not delete skipped files unless specifically instructed to do so. However, it is important to use this command very carefully. When a file is deleted, it is gone forever. Git cannot restore the content.
In summary, all files in a Git repository must be handled in one of three ways:
- They can be added with the repository
add git
. - They can be added to this
.ignore.git
file, causing Git to ignore it. - They can be removed with a variant of
clean
Command.
If you haven't already done so, create a Linode account and compute instance. see ourStarting with LinodeeCreate a compute instanceFührer.
follow ourConfigure and secure a compute instanceInstructions for updating your system. You can also set the time zone, configure your hostname, create a limited user account and secure SSH access.
It is also useful to consult our guidesIntroduction to GiteHow to Navigate the Linux Terminal and File System.
OptionalGit must already be installed on Linode before trying the examples in this guide. This one
git
The package is usually pre-installed. To see if it's there, run the commandgit --version
🇧🇷 If Git is already installed, this command displays the current version. If Git has not yet been installed, use the commandsudo apt install git
to install it.
UseHow to remove untracked files with Git CleanThis guide is written for non-root users. Commands that require elevated privileges are preceded by the prefix
sudo
🇧🇷 If you do not knowsudo
command, see theUsers and groupslead.
This section shows how to remove untracked files in Git usingclean
Command. This command has several options that allow users to control the command's behavior or output.clean
it also has an interactive mode that makes it easy to selectively delete a subset of files.
Try the examples in this section with a local Git repository. If Linode doesn't already have a repository, you can create one using thegit init
Command. run thoseinside side
Command in the base directory of the repository.
No additional options is the commandclean
does nothing. One of-EU
,-n
, or-f
Options usually need to be appended.
clean
fatal: clean.requireForce defaults to true and neither -i, -n nor -f is specified; refuse to clean
git clean -f
is probably the most common alternative. This forces Git to remove all untracked files without any further opportunity to refine or change the operation. When finished, it will show a summary of deleted files. This command is functionally equivalent to manually deleting files.
CautionAlways use
clean
carefully. This action cannot be undone.
The example below shows howgit clean -f
handles a combination of untracked and tracked but uncommitted files. Before the operation,testdatei1.txt
was added but not adopted. the two filestestdatei2.txt
etestdatei3.txt
weren't added, so both aren't tracked in Git yet.
Git-Status
Changes to apply: New file: testfile1.txt Untracked files: testfile2.txttestfile3.txt
to runclean
Use of-f
Option to delete the two untracked files.
git clean -f
Remover testfile2.txtRemove testfile3.txt
use any of theseGit-Status
orls
Command to check if untracked files are deleted. This oneStatus
The command confirms that the tracked but uncommitted file is left intact. However, the untracked files no longer exist.
Git-Status
Changes to apply: new file: testfile1.txt
git clean -n
reduces the associated riskssauber
Command. It lists all the files the command wants to delete, but it doesn't actually delete them. It acts as a "test" for the command and can be used to prevent accidental deletions.
Starting with the same setup asgit clean -n
The command lists the files that Git would "remove" in a real operation.
git clean -n
Would remove testfile2.txt Would remove testfile3.txt
After that, the files are still listed underGit-Status
because they weren't actually deleted.
Git-Status
...Untracked files: testfile2.txttestfile3.txt
It isgit clean -e
The option allows users to enter a specific exclusion pattern or filename. Files with this name or with this pattern will not be deleted.
In the example below, there are two untracked files calledtestfile4.md
etestdatei4.txt
.
Git-Status
...Untracked files: testfile4.mdtestfile4.txt
Ignoretestdatei4.txt
append option during deletion process-e Testdatei4.txt
for thesauber
Command. East-f
The option is still required to force the remaining deletions.
git clean -f -e testfile4.txt
Remover testfile4.md
This is themd
The file is deleted, but theTXT
file is not.
...Untracked files: testfile4.txt
git clean -d
is recursive and clears the current directory and all subdirectories.
git clean -f -d
Remover arquivo/testfile2.txtRemover testfile4.txt
It is also possible to specify a directory to limit the file size.clean
Operation. The command only applies to untracked files in that directory. The following example appliesclean
to all filesexample
Directory.
git clean -f -d example
git config
allows users to change the default settingclean
Behavior. This allowsclean
to delete untracked files without the-f
Possibility. makes effectiveclean
equivalentgit clean -f
🇧🇷 Use the command to add this option to the configuration filegit config clean.requireForce falso
🇧🇷 For more information, see the Git documentation.Clean up.
UseHow to remove untracked files with Git Clean in interactive modeIt is
-q
option is runningclean
in sleep mode. This meansclean
doesn't report the removed files, but still shows errors.
It is-EU
The option is used to runclean
in interactive mode. It allows users to more accurately select the files to be deleted. This is a good option for situations where you want to exclude some but not all untracked files. It's also good for anyone who wants to be extra careful when running this command.
It is-EU
Option displays a menu listing all available options. It also lists files that are currently scheduled for deletion.
To switch to idle mode, run thegit sauber -i
Command. Git displays the main menu for interactive mode.
git sauber -i
Would remove the following items: testfile2.txt testfile3.md testfile3.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>
The list of options is as follows.
- sauber: This deletes the untracked files in the list in the same way
git clean -f
tut. - filter by default: This option allows users to enter exclusion patterns.
clean
ignores untracked files with names matching any of these patterns. For example, the filter pattern*.TXT
saidclean
do not delete untracked files that end with.TXT
Renovation. - select by number: This displays a numbered list of untracked files. Users can use these numbers to select files for deletion.
- ask everyone: this goes through the list of untracked files one by one and allows the user to choose whether to delete each file or not.
- Go out
- Help
From the main menu, users can select one of the first four options to remove files, exit interactive mode, or view the help page.
possibility1
behaves exactly like thisgit clean -f
🇧🇷 It removes all untracked files and its use is self-explanatory. It is usually selected after narrowing the file list using one of the other methods. The characterc
starts this option too.
The second option isfilter by default
🇧🇷 It allows users to enter a pattern. Any filename that matches this pattern will be ignored. This will limit and possibly reduce the number of filessauber
option removed.
to use thefilter by default
follow the steps below in the main menu.
choose an option
2
or enter thef
Key. Git lists all untracked files and asks for another response.2
testfile2.txt testfile3.md testfile3.txtInput ignores default>>
Enter the pattern you want Git to ignore. For example, to avoid deleting markdown files, enter the pattern
*.md
. It is*
character acts as a placeholder. Any file matching this pattern will be ignored while the user remains in interactive mode. Git removes matching files from consideration and displays an updated list of eligible files.*.md
testfile2.txt testfile3.txtInput ignore pattern>>
Add any other defaults you want Git to ignore at this point. When all patterns have been entered, use theENTERbutton to return to the main menu. Git then lists all files scheduled for deletion and waits for more entries.
Would remove the following items: testfile2.txt testfile3.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>
To clear the file list, type
1
orc
.1
Remover testfile2.txtRemove testfile3.txt
It is often easier to specify files to exclude from a numbered list. Enter3
ors
to access theselect by numbers
Menu. This option allows users to specify individual files, a range of files, or a wildcard.*
Specification of all files. Only intentionally selected files can be deleted.
From the interactive main menu, follow these instructions to select and delete files by number.
Enter the option
3
or use thes
key to enter noselect by numbers
Menu.3
1: Testfile2.txt 2: Testfile3.md 3: Testfile3.txt4: Testfile4.txt Select items to exclude >>
Enter the items to be excluded, separating them with commas. A range can also be used to select multiple consecutive items using the format
start end
🇧🇷 The following response selects elements1
,2
, e4
, but no article3
🇧🇷 Git highlights selected files with a*
.1-2,4
* 1: testfile2.txt * 2: testfile3.md 3: testfile3.txt * 4: testfile4.txt Select items to exclude >>
ChooseRETURNSto return to the main menu. Git lists selected files.
Would remove the following items: testfile2.txt testfile3.md testfile4.txt*** Commands*** 1: clear 2: filter by pattern 3: select by numbers 4: ask every 5: exit 6: helpAnd now>
Enter
1
orc
to remove selected files.1
Remover testfile2.txtRemove testfile3.mdRemove testfile4.txt
The last method to delete files is theask everyone
Possibility. type a4
or oneone
to take advantage of this opportunity. It lists all the files one by one and asks the user if they should be deleted. answersj
to delete the file orn
to skip the file.
to use theask everyone
method, follow these steps.
Use o
4
orone
key to access theask everyone
Possibility.4
Git displays the first file and asks if you want to delete it or not.
Remover testfile2.txt [s/N]?
Enter
j
to add the file to the exclusion list.j
Enter
j
orn
for each of the remaining files.After the user reviews each file, Git deletes all selected untracked files.
Remover testfile2.txt
The remaining two options are self-explanatory. possibility5
(q
) exits the interactive menu. possibility6
is the help menu, which can also be accessed viah
🇧🇷 The help menu explains the different options.
clean - start cleaning filter by default - exclude items from deletion select by numbers - select items to delete by number ask all - confirm each deletion (like "rm -i") exit - stop cleaning help - this screen? - Quick selection help
How to remove skipped files with Git CleanIt is.ignore.git
file specifies which untracked files are to be ignored. This is the best option for dealing with configuration or system files that should not be checked or deleted. Users can run thels
command and see those files, but they don't show up in the results of commands likeGit-Status
🇧🇷 This file is usually created automatically and comes pre-installed with a list of common extensions that can be ignored. See the documentation for more information.ignore histo.
clean
normally ignores the files and filename patterns listed in.ignore.git
File. This is consistent with the behavior of other Git commands, which also ignore these files by default.
However, this behavior can be overridden-x
or-X
Possibility. These options tell Git not to follow the default ignore rules. This means that Git can consider all files covered by.ignore.git
🇧🇷 However, they differ in how they handle other untracked files.
UseGit still respects any exclusion rules added with
-e
Possibility.
In the example below, all untracked*.c
Project files appear inGit-Status
🇧🇷 However, the*.Ö
files no. This occurs because the.ignore.git
The file contains the pattern*.Ö
🇧🇷 This tells Git to ignore files with that extension.
- File: .gitignore
1
*.Ö
Usually,clean
does not affect untracked files that match.ignore.git
Sample. To usegit clean -n
confirmedtestdatei3. o
will not be considered for exclusion.
git clean -n
Would remove testfile3.c. Would remove testfile3.txt. Would remove testfile4.txt
To remove only skipped files and leave other untracked files untouched, usegit clean -f -X
. It is-X
The option removes the untracked.Ö
files. Developers can use this option to force a perfectly clean build.
git clean -f -X
Git only removes untracked files that match a pattern in.ignore.git
.
Remover testfile3.o
Use the command to remove all untracked files regardless of whether they are skipped or notgit clean -f -x
instead. This one-x
The option removes all untracked files, including skipped files.
git clean -f -x
Remova testfile3.cRemove testfile3.oRemove testfile3.txt
Final thoughts on removing untracked files in GitThis guide explains how to use it.clean
to remove untracked files in Git. While old untracked files aren't necessarily harmful, they do clog up a workspace and create a sense of clutter and chaos. There are several methods to remove them, but the easiest way to remove an untracked file in Git is theclean
Command. This command deletes untracked files while leaving other files untouched.
It issauber
The command usually requires the force option-f
be effective. It also has a dry running mode and can run recursively.clean
features a handy interactive mode that allows users to exclude files based on a predefined filter or select them from a numbered list. By default,clean
will not delete files matching the.ignore.git
File. However, one option only targets untracked skipped files, while another affects all untracked files. For more information about theclean
Command, see theGit documentation.
More information
For more information on this topic, see the resources below. While they are provided in the hope that you will be helpful, please note that we cannot guarantee the accuracy or timeliness of any externally hosted material.
This page was originally published in
FAQs
How do I remove untracked files from git without deleting? ›
Using the git rm –cached Command
We've mentioned that git rm FILE will remove files from the index and local working tree by default. However, the git rm command provides the –cached option to allow us only to remove files from the repository's index and keep the local file untouched.
git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !
How to untrack all files in git? ›- git rm --cached <filename>
- git rm <filename>
- git rm -r --cached <folder>
The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.
Why do I have untracked files in git? ›Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .
How do I untrack a tracked file? ›Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.
How do I Unstage untracked files? ›To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.
How do I remove unchanged files in git? ›- Remove every file from Git's index. git rm --cached -r .
- Rewrite the Git index to pick up all the new line endings. git reset --hard.
To undo git add before a commit, run git reset <file> or git reset to unstage all changes.
How to remove all git files? ›Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains. This Git repo remove command also allows you to delete the Git repo while allowing all of the other files and folder to remain untouched.
How to remove files in git commit? ›
If this is your last commit and you want to completely delete the file from your local and the remote repository, you can: remove the file git rm <file> commit with amend flag: git commit --amend.
How do you remove a file that Cannot be removed? ›- Go to Start, type Task Manager, and choose "Task Manager" to open it.
- Find the application that is currently using the file, and select "End task".
- Then, try to delete the file again on your Windows PC.
git clean -f removes untracked files in the directory where you call it only. To remove other untracked files within the root directory, use git clean -f:/ Run the git clean -f command twice if a different repo manages the untracked directory whose files you want to remove from git.
Where do untracked files come from? ›An untracked file in Git is a file created in the repository's working directory but not yet added to the repo's tracking index via the git add command.
How do I clean up my github repository? ›- If you just clean untracked files, run git clean -f.
- If you want to also remove directories, run git clean -f -d.
- If you just want to remove ignored files, run git clean -f -X.
- If you want to remove ignored as well as non-ignored files, run git clean -f -x.
- Click the vertical three-dots icon on the top right-hand corner and then click Settings.
- Click Privacy and security.
- Select Cookies and other site data.
- Select Block third-party cookies.
- Click Privacy and security.
- Click clear browsing data.
- Select All time from Time range.
In the Views pane, select the view that contains the files you want to remove. Select the file you want to remove. Select File | Remove.
How do I delete a file Trace? ›In the Trace files section, click the Windows Update logs button or Remote installation logs button, depending on which trace files you want to delete. The list of trace files opens. In the list of trace files, select the file that you want to delete. Click the Remove button.
Why does my file say untracked? ›Untracked files are the ones still not versioned—”tracked”—by Git. This is the state of new files you add to your repository. That basically means Git is aware the file exists, but still hasn't saved it in its internal database.
Which command is used to Unstage in git? ›Using git reset to Unstage
Apart from restore you can also use git reset to unstage changes. If you're using a Git version older than 2.23, you will have to use reset because restore is quite a new feature in Git. Exactly like git restore --staged , this makes sure the file is NOT included in our next commit.
How do I remove unwanted files from pull request? ›
- delete file from your remote/origin branch and commit (won't be possible without it)
- git stash.
- git pull.
- git stash pop.
Deleting the . git folder does not delete the other files in that folder which is part of the git repository. However, the folder will no longer be under versioning control.
How do I ignore untracked files in git commit? ›- A . gitignore file, which ignores the files and directories in a repository.
- The git clean -fx command, which removes untracked and tracked files.
- The git clean -fd command, which removes untracked files and directories.
In order to delete files recursively on Git, you have to use the “git rm” command with the “-r” option for recursive and specify the list of files to be deleted. This is particularly handy when you need to delete an entire directory or a subset of files inside a directory.
How to remove files from git commit? ›- remove the file git rm <file>
- commit with amend flag: git commit --amend.
- On GitHub.com, navigate to the main page of the repository.
- Under your repository name, click Settings.
- Under Danger Zone, click Delete this repository.
- Read the warnings.
- To verify that you're deleting the correct repository, type the name of the repository you want to delete.
- In the search box on the taskbar, type disk cleanup, and select Disk Cleanup from the list of results.
- Select the drive you want to clean up, and then select OK.
- Under Files to delete, select the file types to get rid of. To get a description of the file type, select it.
- Select OK.