Skip to content

Commit c3b831b

Browse files
Pavel RoskinJunio C Hamano
authored andcommitted
Add git-clean command
This command removes untracked files from the working tree. This implementation is based on cg-clean with some simplifications. The documentation is included. [jc: with trivial documentation fix, noticed by Jakub Narebski] Signed-off-by: Pavel Roskin <proski@gnu.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 12d81ce commit c3b831b

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ git-checkout
1515
git-checkout-index
1616
git-cherry
1717
git-cherry-pick
18+
git-clean
1819
git-clone
1920
git-clone-pack
2021
git-commit

Documentation/git-clean.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
git-clean(1)
2+
============
3+
4+
NAME
5+
----
6+
git-clean - Remove untracked files from the working tree
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'git-clean' [-d] [-n] [-q] [-x | -X]
12+
13+
DESCRIPTION
14+
-----------
15+
Removes files unknown to git. This allows to clean the working tree
16+
from files that are not under version control. If the '-x' option is
17+
specified, ignored files are also removed, allowing to remove all
18+
build products.
19+
20+
OPTIONS
21+
-------
22+
-d::
23+
Remove untracked directories in addition to untracked files.
24+
25+
-n::
26+
Don't actually remove anything, just show what would be done.
27+
28+
-q::
29+
Be quiet, only report errors, but not the files that are
30+
successfully removed.
31+
32+
-x::
33+
Don't use the ignore rules. This allows removing all untracked
34+
files, including build products. This can be used (possibly in
35+
conjunction with gitlink:git-reset[1]) to create a pristine
36+
working directory to test a clean build.
37+
38+
-X::
39+
Remove only files ignored by git. This may be useful to rebuild
40+
everything from scratch, but keep manually created files.
41+
42+
43+
Author
44+
------
45+
Written by Pavel Roskin <proski@gnu.org>
46+
47+
48+
GIT
49+
---
50+
Part of the gitlink:git[7] suite

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
114114

115115
SCRIPT_SH = \
116116
git-add.sh git-bisect.sh git-branch.sh git-checkout.sh \
117-
git-cherry.sh git-clone.sh git-commit.sh \
117+
git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
118118
git-count-objects.sh git-diff.sh git-fetch.sh \
119119
git-format-patch.sh git-log.sh git-ls-remote.sh \
120120
git-merge-one-file.sh git-parse-remote.sh \

git-clean.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2005-2006 Pavel Roskin
4+
#
5+
6+
USAGE="[-d] [-n] [-q] [-x | -X]"
7+
LONG_USAGE='Clean untracked files from the working directory
8+
-d remove directories as well
9+
-n don'\''t remove anything, just show what would be done
10+
-q be quiet, only report errors
11+
-x remove ignored files as well
12+
-X remove only ignored files as well'
13+
SUBDIRECTORY_OK=Yes
14+
. git-sh-setup
15+
16+
ignored=
17+
ignoredonly=
18+
cleandir=
19+
quiet=
20+
rmf="rm -f"
21+
rmrf="rm -rf"
22+
rm_refuse="echo Not removing"
23+
echo1="echo"
24+
25+
while case "$#" in 0) break ;; esac
26+
do
27+
case "$1" in
28+
-d)
29+
cleandir=1
30+
;;
31+
-n)
32+
quiet=1
33+
rmf="echo Would remove"
34+
rmrf="echo Would remove"
35+
rm_refuse="echo Would not remove"
36+
echo1=":"
37+
;;
38+
-q)
39+
quiet=1
40+
;;
41+
-x)
42+
ignored=1
43+
;;
44+
-X)
45+
ignoredonly=1
46+
;;
47+
*)
48+
usage
49+
esac
50+
shift
51+
done
52+
53+
case "$ignored,$ignoredonly" in
54+
1,1) usage;;
55+
esac
56+
57+
if [ -z "$ignored" ]; then
58+
excl="--exclude-per-directory=.gitignore"
59+
if [ -f "$GIT_DIR/info/exclude" ]; then
60+
excl_info="--exclude-from=$GIT_DIR/info/exclude"
61+
fi
62+
if [ "$ignoredonly" ]; then
63+
excl="$excl --ignored"
64+
fi
65+
fi
66+
67+
git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} |
68+
while read -r file; do
69+
if [ -d "$file" -a ! -L "$file" ]; then
70+
if [ -z "$cleandir" ]; then
71+
$rm_refuse "$file"
72+
continue
73+
fi
74+
$echo1 "Removing $file"
75+
$rmrf "$file"
76+
else
77+
$echo1 "Removing $file"
78+
$rmf "$file"
79+
fi
80+
done

0 commit comments

Comments
 (0)