EN
Git reset hard with untracked files removal
5 points
xxxxxxxxxx
1
# reset current branch and remove untracked directories and files
2
git reset --hard HEAD && git clean -fd
- This cmd will reset our branch and remove all untracked files and directories (non-ignored by .gitignore).
- All uncommited changes will be removed and we have clean state on the branch.
- Very powerful command, before using it think twice.
Practical example on some temporary files:
If we want to remove all files and directories (even ignored by .gitignore - it will also remove all IDE configurations if stored under project), below command can do the trick:
xxxxxxxxxx
1
# reset current branch + remove ignored and non-ignored files
2
# it will remove all ignored files like .idea configs etc
3
git reset --hard HEAD && git clean -fdx
4
5
# reset current branch + remove ignored directories and files
6
git reset --hard HEAD && git clean -fdX
7
8
# reset current branch + remove directories
9
git reset --hard HEAD && git clean -fd
xxxxxxxxxx
1
$ git reset --hard HEAD
Git will leave untracked files.
git clean
most useful flags:
xxxxxxxxxx
1
-n, --dry-run dry run
2
-f, --force force
3
-d remove whole directories
git clean -help
entire output:
xxxxxxxxxx
1
$ git clean -help
2
usage: git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...
3
4
-q, --quiet do not print names of files removed
5
-n, --dry-run dry run
6
-f, --force force
7
-i, --interactive interactive cleaning
8
-d remove whole directories
9
-e, --exclude <pattern>
10
add <pattern> to ignore rules
11
-x remove ignored files, too
12
-X remove only ignored files
xxxxxxxxxx
1
$ git clean --dry-run
This command will list what will be deleted.
It is useful to run it before running git clean -fd.