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
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
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
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
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
Also read: How to remove a Directory in Linux?
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.