Remove Untracked Files Git
Overview
Untracked files are the files that are present in the Git repository folder but are not tracked by Git. If these files are accidentally deleted or removed Git cannot help recover these. There are multiple ways to remove untracked files Git. We can delete these using the git clean command, we can ask Git to ignore them using .gitignore, or we can add them to git stash and recover them when required.
Pre-requisites
- Basic Git commands
What are Git Untracked Files?
All the files that are present in the project folder are recognized by Git, but all of them are not tracked. Tracking is important to keep note of versions of the particular file and changes made to it in each new version.
Each newly created file is untracked initially in Git. A newly created file or any changes made to the existing file must be first added to the staging area and then in the next commit, it must be saved or committed. If the file is not added to the staging area and is not committed, it is called an untracked file.
An untracked file is a file in Git's working directory that hasn't been added to the staging area and thus, is not committed as well.
Untracked files must either be tracked or committed or removed to avoid losing important data or files.
The git status command lists the untracked files in the repository. These untracked files are then added to the staging area using the git add command and are committed to the repository with the git commit command.
Types of Untracked Files in Git
Some of the commonly seen untracked files are:
- A newly created file containing local code, developed for the codebase.
- A default file created by the IDE.
- Collection of local dependency files, local build files, or local OS files are all examples of untracked files.
The type of files helps us define the action to be taken on the untracked file.
How to Remove Untracked Files in Git?
There are various ways to remove untracked files git, but as discussed above removing untracked files may result in data loss. Thus, they should be studied first, and depending on their type a safe option must be chosen.
Let us first initialize a Git repository and add a few files to it.
Convert Untracked Files to Tracked Files
The git status command shows the list of untracked files. Both the newly created files are untracked. To track these using Git we will simply add them to the staging area and commit them.
Now, as we see in the status that file1.txt is no more in the untracked files.
Manually Delete Untracked Files
Another way to remove untracked files git is, certain types of untracked files can be simply deleted manually by visiting the project folder in file explorer, right-clicking on the untracked file, and deleting it.
These can also be deleted using the command line. In macOS, Windows, and GUI Linux environments simply visit the folder using Git Bash or the command line where you run Git commands. In the project root, execute the git status command to list the files that are not tracked. The rm [file name along with its path].
Using .gitignore to Handle Untracked Files in Git
Many times we neither wish to remove untracked files git nor wish to commit them. Git provides a .gitignore option which hides a file and is no more tracked. It is a .txt file in which we list all the files of the project that we want Git to ignore and let them be untracked. It prevents showing them as untracked when we run the git status command. Also, it doesn't allow us to add or commit these files in case someone accidentally uses these commands on the ignored files.
Note: Git ignores the files in the .gitignore but it surely tracks the .gitignore itself to be updated on which file to ignore.
Let us see how we can hide files from Git tracking using .gitignore. We will create a ignore file using the command touch .gitignore.
On executing the git status command we see that file2.txt is untracked. We append the file name into the .gitignore file using the echo command and >> to append. In the next status execution, we see that the .gitignore command is untracked as we modified it and its changes are not tracked or saved by Git. But file2.txt is removed from this list. As soon as we commit changes in the .gitignore file, Git shows no untracked files.
Git Clean - The Git Command for Removing Untracked Files
If the untracked files are more and are spread across the project it becomes difficult to reach out to each file and delete it manually. To remove all such files at once we can use a lesser-known Git command git clean.
By default it is necessary to specify a parameter -f with each option used, otherwise, we get an error message and it doesn't perform the clean operation. But we can set clean.requireForce to false if we don't wish to specify -f with each command.
Let us now explore each of the options available with the git clean command with an example. We will begin by adding untracked files to our repository using the echo command or manually creating and adding files to the project folder.
Force Cleaning -f
The -f parameter stands for force. It forces the permanent deletion of untracked files in the Git repository. Optionally, we can set clean.requireForce to false thus, making -f the default when we run the git clean command and it is no more required to mention -f.
Syntax:
Have a look at the example below, we are adding two files to the repository. These files are yet untracked and on performing git clean -f both the files are deleted and are no more shown as untracked files.
Interactive Cleaning -i
When executed, it displays a menu with various options the user can choose from for cleaning.
Syntax:
- Clean - It deletes all the untracked files.
- Filter by Pattern - This option allows you to enter patterns using which Git filters files. We can apply multiple filters to separate desired files, simply press enter when done filtering. This will again show the menu, press c to remove selected files and q to exit. We can also use numbers i.e. 1 to remove files and 5 to quit without removing anything.
- Select by Numbers - The option allows us to select files to delete by their index number. indexes can be separated by commas. Files to be removed are started, on pressing enter it again shows the menu. Press c or 1 to delete and q or 5 to exit from the selection.
- Ask Each - This option individually asks for each file to remove or not, the user can respond with y to remove and n to not remove.
- Quit - It simply exists from the interactive clean.
- Help - This option can be accessed with h to display help. Help displays functions of each option discussed above.
Stimulating a Clean -n
It is safer to have a backup of the repository before performing git clean because once the files are deleted, there is no way to recover them.
Therefore, it is a good idea to perform a dry run before performing an actual clean. It will display all the files and directories that will be deleted, if only the desired files are being removed, it is safe to execute git clean.
To perform a dry run -n option is used along with other mandatory options.
Syntax:
Rather than saying Removing file4.txt it says Would remove file4.txt. It just tells which files will be removed if the above operation is performed. We can use -n along with any of the available options to dry-run the execution.
If any of the files are important then, we should either start tracking it or add it to the .gitignore file.
Directory Cleaning -d
It deletes all the untracked directories along with files.
Syntax:
In the example above we can see that it has two folders folder1 and folder2 which are empty and are created using the mkdir folder_namecommand.
On executing git clean -d -f, it removes untracked and empty folders along with the untracked files.
Ignored files Clean -x
It removes all the ignored files at once. The files that are specified in the .gitignore are removed from the repository. To remove specific files only we may use the -i option to filter or to choose using index numbers.
Syntax:
Note: Multiple options can be combined to delete files and directories.
Can You Git Stash Untracked Files?
Another amazing Git functionality is git stash which lets us save untracked files. Git provides a folder where we can store all the untracked files simply into the stash and restore them anytime we again wish to use them in the project. Files added to git stash are no more tracked by Git.
An example of git stash is given below. The git stash -u command adds all the untracked files into the stash. To restore the file git stash pop can be used. To restore all files git stash popstash@{x} command is used.
Here, file4 is untracked and thus, is added to the git stash. Now, this file after being added to the stash is no more tracked by Git.
Does Git reset Remove Untracked Files?
The git reset command is used to take the project to the previous working state. It deletes all the progress in the project from a particular point or commits. We are required to be extra careful while using this command as in certain cases it may lead to deleting important files or information.
It operates with three options the --hard option, the --soft option, or the default --mixed option. Reset soft doesn't affect the staging area and working directory, it only reverts committed changes. Reset the mixed option reverts committed changes and added files from the staging area back to the working directory. The git reset --hard command removes files from the staging area and also reverts commits but it leaves the untracked files untouched.
Thus none of the options of the git reset command deletes untracked files.
Let us see an example of resetting to check if it deletes untracked files.
As the above example depicts, untracked files are not affected by the git reset command.
Conclusion
- Untracked files are the files that are present in the directory but are not being tracked or versioned by Git. In case these files are modified or deleted Git will not have any record to recover them.
- We can either start tracking them using the git add and git commit commands.
- We can also delete them manually using the rm command.
- .gitignore file allows us to specify files that we want Git to ignore even if it is untracked.
- We can add untracked files to git stash to temporarily ignore them and later fetch them back in the project with the git stash pop command.
- The git clean is a very useful command to remove untracked files git at once. It provides various options to selectively delete untracked files as well as directories.
- Git reset cannot be used to delete untracked files.