git-clean命令

本文最后更新于:7 个月前

探究clean命令

git-clean命令

官方链接:Git - git-clean Documentation (git-scm.com)

1
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>…​]

首先看看官方如何介绍git-clean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git clean --help
NAME
git-clean - Remove untracked files from the working tree 在工作树中删除未被追踪的文件

SYNOPSIS
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]


DESCRIPTION
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory. 通过递归地删除没有在版本控制系统下的文件来清理工作树,从命令行当前目录开始递归

Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products. 通常,只有git系统没有感知到文件才会被删除,但如果添加了-x 参数的话,被ignore(一般是在.gitignore中指定)的文件也会被删除,例如,-x这个参数被用来删除所有构建出来的内容

If any optional <pathspec>... arguments are given, only those paths that match the pathspec are affect. 如果 pathspec 路径参数指定了的话,只有该指定路径下的文件会执行clean命令


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
OPTIONS
-d # 通常,如果没有指定路径参数的话,clean不会递归去删除未被追踪的文件夹,以避免删除太多东西。-d参数表示不加路径参数时,也递归地去删除

-f, --force
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked
nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-i, --interactive
Show what would be done and clean files interactively. See “Interactive mode” for details.

-n, --dry-run
Don’t actually remove anything, just show what would be done.

-q, --quiet
Be quiet, only report errors, but not the files that are successfully removed.

-e <pattern>, --exclude=<pattern>
Use the given exclude pattern in addition to the standard ignore rules (see gitignore(5)).

-x
Don’t use the standard ignore rules (see gitignore(5)), but still use the ignore rules given with -e options from the command line. This allows removing all untracked files, including build
products. This can be used (possibly in conjunction with git restore or git reset) to create a pristine working directory to test a clean build.

-X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

实验步骤

基础情况:

(1)已经存在两个分支,main、develop

(2)develop是基于main创建的

(3)main、develop分支目前只有一个readme文件

  • 在main分支上添加文件如下:

image-20230721180458067

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"name": "git_test_project",
"version": "1.0.0",
"description": "git实操测试训练场",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Qiuzcc/tmp_test_git.git"
},
"author": "qiuzhicong",
"license": "ISC",
"bugs": {
"url": "https://github.com/Qiuzcc/tmp_test_git/issues"
},
"homepage": "https://github.com/Qiuzcc/tmp_test_git#readme",
"devDependencies": {
"typescript": "^5.1.6"
}
}

.gitignore

1
2
node_modules/
ignore.txt
  • commit提交
  • 切换到develop分支

image-20230721180458067image-20230721180655265

(左侧是main分支目录,右侧是develop分支目录)

可以看到:main分支下创建的、但是被添加到.gitignore的东西(也即是没有被track的内容)被”携带“到了develop分支的工作目录下。被track的内容(main_dir整个文件节)没有被携带到新分支的工作目录下

这就会带来一个问题:下面举个例子

  • 在A分支上,通过.gitignore(A)忽略了某些文件(用filesA表示)
  • B分支的.gitignore(B)与A分支的不同,恰好没有忽略filesA
  • 从A分支切换到B分支时,因为filesA没有被tracked,所以被带到了B分支的工作树中(或者说工作区中)
  • 这是如果B分支直接git add的话,会将filesA也add进去,而这并不是用户所期望发生的,用户可能会在不知情的情况下add了文件,而这有可能导致一些bug的出现!!

慎用clean命令!!

在develop分支将被ignore的文件删除后,会对其它分支产生直接影响(切换回其它分支时,被删除的文件无法通过切换分支恢复)


git-clean命令
http://timegogo.top/2023/07/21/Git/git-clean命令/
作者
丘智聪
发布于
2023年7月21日
更新于
2023年7月23日
许可协议