How to Remove Untracked Files in Git

How to Remove Untracked Files in Git – Git user works with three types of files in the git repository. These are tracked files, untracked files, and ignore files. The files which have been added and committed in the repository are called tracked files. The other files of the repository that are not ignored files are called untracked files.

Suppose the untracked files are not necessary for the repository. In that case, it is better to remove the unnecessary untracked file and make the working directory clean. `git clean` command is used to remove the untracked file from the current working directory. After executing this command, the removed file can’t be recovered.

So, it is better to keep the backup of the repository before executing `git clean` to prevent the accidental deletion of the necessary files. The ways to remove the untracked files from the repository using the `git clean` command as shown in this tutorial.

Table of Contents

Difference Between Tracked vs. Untracked Files

The files in the Git working directory can be either tracked or untracked.

In your working or local directory your files are either tracked or untracked. Tracked means those files added and committed in a previous snapshot and that Git is aware, tracking them for changes.

Untracked files are the opposite, those files were not in the previous commit and have not been staged to be committed. You have the option of either stage them and commit them to your repository, or remove them!

If we do git status right after modifying/added files it would show us the list of untracked files and files that are tracked.

Tracked files are the ones that have been added and committed, and Git knows about. Tracked files can be unmodified, modified, or staged. All other files in the working directory are untracked and git is not aware of those files.

Sometimes your git working directory may get cluttered up with unnecessary files that are either auto-generated, leftover from merges, or created by mistake. In those situations, you can either add those files in .gitignore or remove them. If you want to keep your repository nice and clean, the better option is to remove the unnecessary files.

This article explains how to remove untracked files in Git.

Removing Untracked Files

The command that allows you to remove untracked files is git clean.

It is always a good idea to backup your repository because once deleted, the files and changes made to them cannot be recovered.

Before running the actual command and removing untracked files and directories use the -n option that will perform a “dry run” and show you what files and directories will be deleted:

git clean -d -n 

The output will look something like this:

Would remove content/test/
Would remove content/blog/post/example.md

If some of the files listed above are important, you should either start tracking these files with git add <file> or add them to your .gitignore.

Once you are sure you want to go ahead and delete the untracked files and directories, type:

git clean -d -f

The command will print all successfully deleted files and directories:

Removing content/test/
Removing content/blog/post/example.md

The -d option tells git to remove untracked directories too. If you don’t want to delete empty untracked directories, omit -d option.

The -f option stands for force. If not used and the Git configuration variable clean.requireForce is set to true, Git will not delete the files.

To interactively delete the untracked files, use the -i option:

git clean -d -i

The output will show the files and directories to be removed, and ask you what to do with those files:

Would remove the following items:
  content/test/   content/blog/post/example.md
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help

Select one of the choices and hit Enter.If you want to limit the clean operation to given directories, pass the paths to the directories to be checked for untracked files as arguments to the command. For example, to check for files under the src directory, you would run:

git clean -d -n src

Removing Ignored Files

The git clean command also allows removing ignored files and directories.

To remove the all ignored and untracked files, use the -x option:

git clean -d -n -x

If you want to remove only the ignored files and directories, use the -X option:

git clean -d -n -X

The command above will delete all files and directories listed in your .gitignore and keep the untracked files.

How to use the git clean command

The git clean command is a tool designed to deal with situations exactly like this, where you have a handful of files to delete and can’t be bothered to track them all down one at a time. It finds all the changes that you haven’t committed or put in staging yet and can delete them in one fell swoop.

It has several different flags, but these are the three that you’ll use most often:

-d: this flag specifies that you want to delete untracked directories too, not just files. You usually want to have this one on, especially if you aren’t clear on exactly which files you want to delete, and know that you want everything untracked gone.

-n, or –dry-run: this flag causes Git to show you which files will be affected, instead of deleting them. Again, if you’re not sure which files you want to get rid of, this is helpful to make sure you don’t accidentally delete anything you want to keep.

-f, or –force: you have to include this flag if you want any files to be deleted. Git very much errs on the side of caution and makes you pass in this extra confirmation. That’s because once a file is deleted with this method, it can’t be restored.

Why not just use git rm?

If you’re a Git command line user, you might be wondering at this point – why don’t we use git rm to delete these files? There are a couple of reasons.

First, with git rm, you need to target each file individually, whereas git clean provides “search and destroy” functionality. This is much faster because you don’t need to give a relative path for each file you want to remove.

But second, and more importantly, git rm actually only works on tracked Git files, that is, files which have already been added to source control by being committed or added to staging. (The Git docs for this command recommend using regular old OS-level rm for untracked files.)

Using git stash to delete files in a safer way

Another method of getting a clean working directory is to use git stash to stash and delete both tracked and untracked files. You can do this using the –include-untracked command, which stashes all untracked files and then runs git clean behind the scenes for us.

Pros and cons of using git stash

The great benefit of this method is that you aren’t deleting any data. It’s all stashed safely away and can be restored at any time. Re-applying the stashed files (with git stash apply) will even restore your untracked files to the state they were without adding them to Git. This is an excellent way to save a “snapshot” of uncommitted changes.

The downside to this method is that it will stash everything, including any changes made to tracked files. You can get around that by adding changes to your tracked files to staging (with git add) and then tacking on the flag –keep-index. This flag will prevent anything in staging from being stashed, so only changes to unstaged and untracked files will be cleared.

Altogether, that command looks like this:
git add my-changed-but-staged-file.txt
git add some-other-changed-and-staged-file.txt
git stash –include-untracked –keep-index

This method is a little more work if you have both untracked files and unstaged changes in your working directory, but since you’re able to restore your files if you realize you made a mistake, it can be situationally useful.

Using .gitignore to avoid committing changes

There’s a third method that you can use to prevent staging or committing untracked files, and that is by adding them (or their parent directories) to your .gitignore file.

Whenever Git is performing operations on files, it first checks to see if those files are in your .gitignore file — if they are, it ignores them. This is useful for when you have files that need to always be present in your working directory, such as build files or your `node_modules` directory, but shouldn’t make it into your repository.

You can read up more on .gitignore in the official docs. And if you decide to use a .gitignore file, you’ll probably want to enforce its inclusion in your projects.

Conclusion

You can remove untracked files from a repository using either the .gitignore or the git clean command. The git clean command removes files recursively. This command starts in your current working directory.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x