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 one
cleanThe 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.
Before using the
cleancommand, 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 using
add gitCommand. 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 the
add gitCommand. 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 using
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
Forcethem, especially if you use the wildcard
- 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-Statusas 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 the
rmCommand. However, this can be time-consuming and it's easy to accidentally delete the wrong file. Other alternatives like
go to redefinehave 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
.cfgfile or create object files. In this case the
.ignore.gitfile is used to tell Git to ignore this file. All files that match an exclusion rule in
.ignore.gitare not displayed in the output of
Git-Statusand the various Git commands do not affect them.
cleanThe 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.gitfile 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
- They can be added to this
.ignore.gitfile, causing Git to ignore it.
- They can be removed with a variant of
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
gitThe package is usually pre-installed. To see if it's there, run the command
git --version🇧🇷 If Git is already installed, this command displays the current version. If Git has not yet been installed, use the command
sudo apt install gitto install it.
UseHow to remove untracked files with Git Clean
This guide is written for non-root users. Commands that require elevated privileges are preceded by the prefix
sudo🇧🇷 If you do not know
sudocommand, see theUsers and groupslead.
This section shows how to remove untracked files in Git using
cleanCommand. This command has several options that allow users to control the command's behavior or output.
cleanit 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 the
git initCommand. run those
inside sideCommand in the base directory of the repository.
No additional options is the command
cleandoes nothing. One of
-fOptions usually need to be appended.
fatal: clean.requireForce defaults to true and neither -i, -n nor -f is specified; refuse to clean
git clean -fis 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.
cleancarefully. This action cannot be undone.
The example below shows how
git clean -fhandles a combination of untracked and tracked but uncommitted files. Before the operation,
testdatei1.txtwas added but not adopted. the two files
testdatei3.txtweren't added, so both aren't tracked in Git yet.
Changes to apply: New file: testfile1.txt Untracked files: testfile2.txttestfile3.txt
-fOption to delete the two untracked files.
git clean -f
Remover testfile2.txtRemove testfile3.txt
use any of these
lsCommand to check if untracked files are deleted. This one
StatusThe command confirms that the tracked but uncommitted file is left intact. However, the untracked files no longer exist.
Changes to apply: new file: testfile1.txt
git clean -nreduces the associated risks
sauberCommand. 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 as
git clean -nThe 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 under
Git-Statusbecause they weren't actually deleted.
...Untracked files: testfile2.txttestfile3.txt
git clean -eThe 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 called
...Untracked files: testfile4.mdtestfile4.txt
testdatei4.txtappend option during deletion process
-e Testdatei4.txtfor the
-fThe option is still required to force the remaining deletions.
git clean -f -e testfile4.txt
This is the
mdThe file is deleted, but the
TXTfile is not.
...Untracked files: testfile4.txt
git clean -dis 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.
cleanOperation. The command only applies to untracked files in that directory. The following example applies
cleanto all files
git clean -f -d example
git configallows users to change the default setting
cleanBehavior. This allows
cleanto delete untracked files without the
-fPossibility. makes effective
git clean -f🇧🇷 Use the command to add this option to the configuration file
git config clean.requireForce falso🇧🇷 For more information, see the Git documentation.Clean up.
UseHow to remove untracked files with Git Clean in interactive mode
-qoption is running
cleanin sleep mode. This means
cleandoesn't report the removed files, but still shows errors.
-EUThe option is used to run
cleanin 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.
-EUOption displays a menu listing all available options. It also lists files that are currently scheduled for deletion.
To switch to idle mode, run the
git sauber -iCommand. 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 -ftut.
- filter by default: This option allows users to enter exclusion patterns.
cleanignores untracked files with names matching any of these patterns. For example, the filter pattern
cleando not delete untracked files that end with
- 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
From the main menu, users can select one of the first four options to remove files, exit interactive mode, or view the help page.
1behaves exactly like this
git 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 character
cstarts this option too.
The second option is
filter 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 files
to use the
filter by defaultfollow the steps below in the main menu.
choose an option
2or enter the
fKey. Git lists all untracked files and asks for another response.
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.
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
Remover testfile2.txtRemove testfile3.txt
It is often easier to specify files to exclude from a numbered list. Enter
sto access the
select by numbersMenu. 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
3or use the
skey to enter no
select by numbersMenu.
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 elements
4, but no article
3🇧🇷 Git highlights selected files with a
* 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>
cto remove selected files.
Remover testfile2.txtRemove testfile3.mdRemove testfile4.txt
The last method to delete files is the
ask everyonePossibility. type a
oneto take advantage of this opportunity. It lists all the files one by one and asks the user if they should be deleted. answers
jto delete the file or
nto skip the file.
to use the
ask everyonemethod, follow these steps.
onekey to access the
Git displays the first file and asks if you want to delete it or not.
Remover testfile2.txt [s/N]?
jto add the file to the exclusion list.
nfor each of the remaining files.
After the user reviews each file, Git deletes all selected untracked files.
The remaining two options are self-explanatory. possibility
q) exits the interactive menu. possibility
6is the help menu, which can also be accessed via
h🇧🇷 The help menu explains the different options.
How to remove skipped files with Git Clean
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
.ignore.gitfile 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 the
lscommand and see those files, but they don't show up in the results of commands like
Git-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.
cleannormally ignores the files and filename patterns listed in
.ignore.gitFile. This is consistent with the behavior of other Git commands, which also ignore these files by default.
However, this behavior can be overridden
-XPossibility. 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.
Git still respects any exclusion rules added with
In the example below, all untracked
*.cProject files appear in
Git-Status🇧🇷 However, the
*.Öfiles no. This occurs because the
.ignore.gitThe file contains the pattern
*.Ö🇧🇷 This tells Git to ignore files with that extension.
- File: .gitignore
cleandoes not affect untracked files that match
.ignore.gitSample. To use
git clean -nconfirmed
testdatei3. owill 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, use
git clean -f -X. It is
-XThe 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
Use the command to remove all untracked files regardless of whether they are skipped or not
git clean -f -xinstead. This one
-xThe option removes all untracked files, including skipped files.
git clean -f -x
Final thoughts on removing untracked files in Git
Remova testfile3.cRemove testfile3.oRemove testfile3.txt
This guide explains how to use it.
cleanto 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 the
cleanCommand. This command deletes untracked files while leaving other files untouched.
sauberThe command usually requires the force option
-fbe effective. It also has a dry running mode and can run recursively.
cleanfeatures a handy interactive mode that allows users to exclude files based on a predefined filter or select them from a numbered list. By default,
cleanwill not delete files matching the
.ignore.gitFile. However, one option only targets untracked skipped files, while another affects all untracked files. For more information about the
cleanCommand, see theGit documentation.
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
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.
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.
- 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.