Git Clean Command

Learn via video courses
Topics Covered

Overview

In situations where the project is large and has a lot of tracked and untracked files and we want to remove the untracked files altogether, we can use the git clean command. The git clean command is a very crucial command which is used to clean the untracked files present in a git repository or the local working directory. We can also use the squash commits and the rebase master to a branch or the branch to master command but this command is the easiest way to clean the untracked files.

Pre-Requisites

The prerequisites for learning the git clean command can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git clean command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems: Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

What Is the Git Clean Command?

The git clean command is a very crucial command which is used to clean the untracked files present in a git repository or the local working directory. The main usage of this command is used to remove the untracked files. In situations where the project is large and has a lot of tracked and untracked files. If we want to remove the untracked files altogether, we can use the git clean command. Although we can use squash commits and rebase master to a branch or a branch to master this command is the easiest way to clean the untracked files.

We use several flags with the git clean command to perform various functionalities along with the removal of untracked files. Some of the variations of the git clean command are discussed in the next section.

There are some limitations of the git clean command, let us discuss the limitations of the command:

  1. The git clean command ignores the .gitignore files and does not remove the .gitignore file.
  2. This command ignores the index files.
  3. The git commit command also ignores the existing commit files and newer directories that are created recently.
  4. It won't remove the historically tracked files.
  5. We cannot directly use the git clean command as it will generate an error saying: git clean fatal: clean.requireForce defaults to true and neither -i, -n, nor -f was given; refusing to clean. We must use either the flag --force or -n to make the command work.

Let us now learn about these flags in the next section.

Usage of Git Clean Command

Let us now learn about the various usage and variations of the git clean command.

Note: We can use the git status command to check the tracked and untracked files in a git repository.

Git Clean -n

The git clean -n command is used to perform a dry run. Since the git clean command is a crucial command as git will permanently delete the files so git first tells us to dry run using the -n flag. The command shows us the files that will be removed if we run the git clean command.

Git Clean -f

The git clean -f command is used to forcefully delete the files. Now, if we have used the dry run and we have not faced any issues then use can use the -f flag to delete the untracked files. we must know that this command will not remove the .gitignore file. In the git clean -f command, we can also specify the file that we want to delete by providing the path of the specific file.

Git Clean -f -x

If we want to delete the .gitignore file then we can use the git clean -f -x as it forcefully (f) deletes the ignored file (x).

Git Clean -f -d

If we want to delete the untracked directories then we can use the git clean -f -x=d as it forcefully (f) deletes the untracked directories (d).

Git clean -fdx

We can also combine all the variations in a single command (git clean -fdx) for simplification but we must be completely sure before running these commands as it may cause damage to our running project.

Implementation

In the above section, we have discussed the various usage and variations of the git clean command. Let us now do some practical and run these commands to see the output for more clarity.

Before running the commands, let us first see the current status of the git repository.

If we directly use the git clean command then the following output will be shown.

1. git clean -n command

2. git clean -f command

3. git clean -f -x command

4. git clean -f -d command

Interactive mode or git clean interactive

Apart from the above-discussed command line ad-hoc git commands, we can use the git clean's interactive mode to delete the untracked files. We only need to pass the -i flag after the git clean command to open the interactive editor mode.

Refer to the image provided below for more clarity. Now we can choose among the options like:

  1. clean- it will clean all the untracked files.
  2. filter by pattern- it will filter the files according to our provided pattern.
  3. select the numbers- we can provide the number of the files to be deleted.
  4. ask each- it will ask each of the files to be deleted.
  5. quit- it will exit the interactive mode.
  6. help- will display the help related to the git clean interactive mode.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • The git clean command is a very crucial command which is used to clean the untracked files present in a git repository or the local working directory.
  • In situations where the project is large and has a lot of tracked and untracked files and we want to remove the untracked files altogether, we can use the git clean command.
  • We can also use the squash commits and the rebase master to a branch or the branch to master command but this command is the easiest way to clean the untracked files.
  • The git clean command ignores the .gitignore files and does not remove the .gitignore file.
  • This command ignores the index files and also ignores the existing commit files and newer directories that are created recently.
  • We cannot directly use the git clean command as it will generate an error saying: git clean fatal: clean.requireForce defaults to true and neither -i, -n, nor -f was given; refusing to clean. We must use either the flag --force or -n to make the command work.
  • The git clean -n command is used to perform a dry run. The git clean -f command is used to forcefully delete the files.
  • If we want to delete the .gitignore file then we can use the git clean -f -x as it forcefully (f) deletes the ignored file (x).
  • If we want to delete the untracked directories then we can use the git clean -f -x=d as it forcefully (f) deletes the untracked directories (d).
  • We can even combine all the variations in a single command (git clean -fdx) for simplification but we must be completely sure before running these commands as it may cause damage to our running project.