git clean

Git clean

Description

Description

The git clean is an undo command, which completes other commands like git reset and git checkout. However, unlike the other commands that operate on files already added to the Git tracking index, the git clean command runs on untracked files. Untracked files are those created within the working directory, but are not yet added to the tracking index. The example below shows the difference between tracked and untracked files:

mkdir test_directory
cd test_directory/
git init .
#Initialized empty Git repository in /Users/kev/code/test_directory/.git/
echo "tracked file" > ./test_tracked_file
git add ./test_tracked_file
echo "untracked" > ./test_untracked_file
mkdir ./test_untracked_dir && touch ./test_untracked_dir/file
git status
#On branch master
#No commits yet
#Changes to be committed:
      # (use "git rm --cached <file>..." to unstage)
                #new file: test_tracked_file
#Untracked files:
       # (use "git add <file>..." to include in what will be committed) 
                #test_untracked_dir/ 
                #test_untracked_file

As a result of the example above, you will have a new Git repository in the test_directory directory, which then creates a test_tracked_file added to the Git index. Besides, an test_untracked_file is created, and an test_untracked_dir. The example then calls git status which displays output pointing on the internal state of tracked and untracked changes. The git clean command is executed to show its predetermined aim.

At this point, you should not execute the git clean command. The example below demonstrates what errors it may produce. Initially, Git is globally configured to require a “force” option to initiate for the git clean. Once executed, you cannot undo git clean. When it is fully executed, it will create a hard file system deletion. You should make sure that you really want to delete the untracked files before running it.

git clean
#fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean

Common options and usage

Common options and usage (常见选项和用法)

The git clean command has various usages with different options.

-n

The -n option performs a tryout of git clean. It shows the files that are going to be removed, but doesn’t remove them.

git clean -n
#Would remove test_untracked_file

As you can see, it says that the test_untracked_file will be removed when the git clean command is executed.

-f or --force

The –force option is required unless the clean.requireForce configuration option is set to false. It deletes untracked files from the current directory, except the untracked folders or files specified with .gitignore.

git clean -f
#Removing test_untracked_file

The output shows that the test_untracked_file has been deleted. At this point, git status will show that the test_untracked_file has been deleted and cannot be found. By default, git clean -f will operate on all the untracked files within the current directory. Additionally, a <path> value can be passed with the -f option that will remove a specific file.

If you want to remove any untracked directory, you can use the -d option that tells git clean to do so, as by default it will ignore directories.

git clean -f <path>
-d include directories

You can also use the -dn combination. (您也可以使用-dn组合。)

git clean -dn
#Would remove test_untracked_dir/
git clean -df
#Removing test_untracked_dir/

It firstly outputs that test_untracked_file that is up for removal. Then we execute a forced clean and receive output that test_untracked_dir is removed. (它首先输出要删除的test_untracked_file。然后,我们执行强制清理并接收删除test_untracked_dir的输出。)

git clean -x

The -x option indicates git clean to also include ignored files. You have better execute a ‘dry run’ first, before the final deletion. The -x option will act on all ignored files. This could be unintended things like ./.idea IDE configuration files.

git clean -xf

The -x option can be passed and composed with other options. The example above is a combination with -f that will delete untracked files from the current directory as well as any files that Git usually ignores. (-x选项可以传递并与其他选项组合。上面的示例是-f的组合,它将从当前目录中删除未跟踪的文件以及Git通常忽略的任何文件。)

Interactive mode

Interactive mode (交互模式)

The git clean has an interactive mode that is activated while passing the -ioption. In the example below we also use the -d option, in order to act over the test_untracked_dir. After activating the interactive mode, it will display a What now> prompt. This prompt will demand to select a command to apply to the untracked files. These commands are 6.

git clean -di
Would remove the following items:
test_untracked_dir/ test_untracked_file
*** Commands ***
1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help
What now>

We are going to look through each command below. (我们将查看下面的每个命令。)

What now> 6
clean - start cleaning
filter by pattern - exclude items from deletion
select by numbers - select items to be deleted by numbers
ask each - confirm each deletion (like "rm -i")
quit - stop cleaning
help - this screen
? - help for prompt selection


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部