Skip to content

How to remove untracked files in Git?

Git mainly handles files in a repository in three separate categories, namely tracked, untracked and ignored. While cleaning off your repository, you must keep the required files around while getting rid of any junk or automatically generated files.

Untracked files in a repository are the ones that Git doesn’t really know about. In this article, we’re taking a look at how you can remove untracked files from a repository. 

Also read: 25 essential Linux Terminal commands


How to remove untracked files in Git?

To remove any untracked files, we’ll be using the git clean command. The command comes with a bunch of flags to help us control the deletion process.

Note that deletion in Git is permanent, and you wouldn’t be able to recover any deleted files (easily anyway) once they’re gone. It’s a good idea to back up your repository before performing any cleaning operations. 

Here’s a list of flags you can use with the git clean command.

  • -f: This is more of a configuration command that essentially forces git to run and clean any files. If the Git configuration variable clean.requireForce is not set to false, the command will not run unless you use this flag. 
  • -X: Removes only files ignore by GIt.
  • -x: This command forces Git to ignore any rules mentioned in the .gitignore file but still follow any rules mentioned with the -e flag.
  • -n: This shows what would be done if you were to run the command you used this flag with. 
  • -d: Removes untracked repositories alongside untracked files.

So in order to remove all untracked files from a repository, we’ll be using the following command.

git clean -d -n
How to remove untracked files in Git? | Candid.Technology

This command will first show you all the files and directories it’ll remove. Once you’re sure you’re deleting the right files, you can delete them using the following command.

git clean -d -f
How to remove untracked files in Git? | Candid.Technology

If you want more control over the deletion process, you can add the -i flag. This will make the command show all the files and directories that’ll be removed and then give you a few choices to decide.

You can also limit your cleaning operations to particular directories by simply passing the path to the directory to be check as an argument to the command. 

git clean -d -n /usr/bin/repo1
How to remove untracked files in Git? | Candid.Technology

Also read: How to delete Files in Linux?


How to remove ignored files in Git?

Git clean also lets users remove ignored files and directories from their repositories. All you have to do is use the aforementioned -x or  -X flag with the command.

To remove all ignored files and directories, use the following command.

git clean -d -n -x
How to remove untracked files in Git? | Candid.Technology

If you want to remove only the ignored files and directories, use the other flag. The following command will delete all the files and directories listed in your .gitignore file and keep the untracked files. 

git clean -d -n -X
How to remove untracked files in Git? | Candid.Technology

Also read: How to remove a Directory in Linux?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

Share on facebook
Share on whatsapp
Share on twitter
Share on reddit
Share on linkedin
Share on pocket
Share on pinterest
Share on telegram
Share on stumbleupon
Share on digg
Share on tumblr
Share on email
Share on skype
Share on xing
Share on vk
Share on odnoklassniki
Share on mix








>