25

Кунг-фу по git-clean. TL;DR git clean -fdx удалить все новые файлы, пустые каталоги, и то что попадает под действие .gitignore Ключ -n вместо -f застаяляет git clean не выполнять реальных действий по удалению.

Создаём репозиторий

$ git init
Initialized empty Git repository in /home/waserd/tmp/.git/
$ touch a b c 
$ mkdir tmp
$ touch tmp/lock
$ git add .
$ echo 'tmp/*' > .gitignore
$ git add .
$ git commit -m init
[master (root-commit) bce15e8] init
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 a
 create mode 100644 b
 create mode 100644 c
 create mode 100644 tmp/lock

Вносим в него различные правки

$ touch d tmp/{1,2,3}
$ ls > a
$ mkdir e
$ touch e/f
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       d
#       e/
no changes added to commit (use "git add" and/or "git commit -a")

Смотрим что удалит git clean по умолчанию

$ git clean -n
Would remove d
Would not remove e/

Ключ -d удалит пустые каталоги заодно

$ git clean -nd
Would remove d
Would remove e/

А ключ -x удалит всё, будто .gitignore у нас нет.

$ git clean -ndx
Would remove d
Would remove e/
Would remove tmp/1
Would remove tmp/2
Would remove tmp/3

Удаляем

$ git clean -fdx
Removing d
Removing e/
Removing tmp/1
Removing tmp/2
Removing tmp/3

Почти чисто =)

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a
#
no changes added to commit (use "git add" and/or "git commit -a")