Git – Clean
1. Introduction
This is an in-depth article related to Git Clean. Git was developed by Vincent Driessen in 2010. Git typically has two branches in its repositories like master and develop. The development branches can be feature-specific, hot fix-specific, release-specific, and trunk.
2. Git Clean
A typical Git branching strategy depends on the stage of the development lifecycle, and release plan. Developers can have their own branches for development. Features are developed during the sprint in the releases. Branches are created for different features. To clear the untracked files in the repository, git has a command called clean. This command helps in getting rid of the untracked files. In the working repository, you can use this to remove the untracked files in the repository.
2.1 Prerequisites
Java 7 or 8 is required on the Linux, windows, or mac operating system. Maven 3.6.1 is required for building this application. A GitHub account is required.
2.2 Download
You can download Java 8 can be downloaded from the Oracle website. Apache Maven 3.6.1 can be downloaded from the Apache site. GitHub can be accessed at this link. Gitkraken can be downloaded from this site. Git can be downloaded from this link.
2.3 Setup
You can create an account on GitHub account using the link. You can create a GitHub repository with a Calculator application java code. You can add the file manually to get started. The code is shown below :
Calculator.java
package org.build.maventool; /** * Calculator class */ public class Calculator { /** * getProduct * @param val1 integer * @param val2 integer * @return product of val1 and val2 */ public int getProduct(int val1, int val2) { return val1*val2; } /** * getSum * @param val1 integer * @param val2 integer * @return sum of val1 and val2 */ public int getSum(int val1, int val2) { return val1+val2; } /** * main method * @param args arguments */ public static void main(String[] args) { Calculator calc = new Calculator(); int product = calc.getProduct(43,4); System.out.println("Product of " + " 43 and 4 is "+product); int sum = calc.getSum(43,4); System.out.println("Sum of " + " 43 and 4 is "+sum); System.out.println("New message"); } }
You can use the below maven pom for building this file.
maven pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>maven-hello</groupId> <artifactId>Maven-project</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>Maven-project</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.build.maventool.Calculator</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
2.4 what is git clean?
Git has commands like reset, check out, and clean to manage the lifecycle of the repository. Git clean command works on untracked files. In the working directory, you might have unwanted files which are not added to the tracking index.
The command can be used as described below with different flags:
- -n: to dry run.
- -f: forcefully file deletion.
- -f -x: delete .gitignore files
- -f -d: delete the untracked directories.
git clean command will not work on the below artifacts in the working directory:
- the .gitignore files
- new directories which are created recently
- index files.
- existing commit files
2.5 Using ‘git clean’
Let us look at an example where the working directory has untracked files. You can see the output below of the git status command.
Showing Untracked Files
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git status On branch main Your branch is up-to-date with 'origin/main'. Untracked files: (use "git add ..." to include in what will be committed) output.txt nothing added to commit but untracked files present (use "git add" to track) (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
The command should not be run at this point. You will get the below error if you run.
git clean errors
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
To remove the untracked files, you can see the next sections in which flags are used with this command.
2.6 Using ‘git clean -n’
Let us look at the -n flag. This is used to show the files which are removed when the clean command is executed.
git clean -n flag
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean -n Would remove output.txt (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.7 Using ‘git clean –force’
Now let us look at the –force flag. If the clean.requireForce configuration is set to false, you do not require this command and flag. This command removes the untracked files from the working directory. It does not remove the untracked directories and files specified with .gitignore.
git clean –force flag
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean --force Removing output.txt (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.8 Using ‘git clean -fdx’
Now let us look at the -fdx combination of three flags with the command. This command will delete untracked files, untracked directories, and files that git usually ignores.
git clean -fdx flag
2.9 Conclusion
To summarize, git clean is used to delete unwanted files from the working tree. In dev operations, git stash, rebase and reset commands are popular. If the developer wants to remove the unwanted files, you use this command with specific flags, -n, –force, and –fdx. Each flag has a specific feature for cleaning or mentioning the files which are to be removed.
3. Download the Source Code
You can download the full source code of this example here: Git Clean