Git

How to Use Git clean -x Option?

The “git clean” command is used for deleting untracked files from their projects. It removes the file from the Git working area. Additionally, once deleted files from the working directory, users are not able to recover them. This command works with different options, such as “-x”, “-f”, “-d”, and many more, which are used to perform different operations. Moreover, these options are used along with each other to perform multiple operations rather than individually.

This study will explain:

How to List the Unstaged Files Using the “git clean -xn” command?

To list the untracked files using the “git clean” command along with the “-xn” option. To do so, we will first create a new file, place it in the working directory, and execute the previously discussed command along with the “-xn” option.

Step 1: Redirect to Local Repository

At first, utilize the provided command and switch to the target local repository:

$ cd "C:\Users\nazma\Git\perk1"

Step 2: Create New File

Then, generate a new file by running the “touch” command:

$ touch file2.txt

Step 3: Track Modifications

Now, execute the following command to track the added changes:

$ git add file2.txt

Step 4: Generate New File

Similarly, make a new file with the help of the provided command:

$ touch file3.txt

Step 5: Check Git Status

Next, run the “git status .” command to show the current working repository status:

$ git status .

According to the below-given output, the most recently created “file3.txt” file is placed in the working directory:

Step 6: View Untracked File

To list the untracked files, run the “git clean” command:

$ git clean -xn

Here, the “-xn” option is used for displaying the list of all untracked and ignored files of the repository that need to be deleted:

How to List the Untracked Files Using the “git clean -dxn” command?

Another way to check the list of all unstaged files is by executing the following command:

$ git clean -dxn

How to Remove the Untracked Files Using the “git clean -fx” command?

Suppose developers want to delete all untracked files from the Git working area. In that case, the “git clean” command can be used along with the “-xf” option:

$ git clean -xf

According to the provided output, the untracked file is removed successfully:

That’s it! We have explained the usage of the “git clean” command along with the “-x” option for listing and removing untracked files.

Conclusion

The “git clean” command is used for deleting the unstaged files. It can display the list of untracked files along with the “-x” option. If developers want to list the untracked files, the “git clean -xn” and “git clean -dxn” commands can be used. To remove the untracked files the “git clean -xf” command can be used to remove the untracked files. This study illustrated the usage of the “git clean” command along with the “-x” option.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.