Git reset hard with untracked files removal

5 points
Created by:

1. Git reset hard and remove all untracked files and directories

git reset --hard HEAD && git clean -fd

This cmd will reset our branch and remove all untracked files and directories.
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:

2. Explanation of git reset --hard HEAD

$ git reset --hard HEAD

Git will leave untracked files.

3. Explanation of git clean -fd

git clean most useful flags:

-n, --dry-run         dry run
-f, --force           force
-d                    remove whole directories

git clean -help entire output:

$ git clean -help
usage: git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...

    -q, --quiet           do not print names of files removed
    -n, --dry-run         dry run
    -f, --force           force
    -i, --interactive     interactive cleaning
    -d                    remove whole directories
    -e, --exclude <pattern>
                          add <pattern> to ignore rules
    -x                    remove ignored files, too
    -X                    remove only ignored files

4. Explanation of git clean --dry-run

$ git clean --dry-run

This command will list what will be deleted.
It is useful to run it before running git clean -fd.

5. References

  1. git reset - Docs
  2. git clean - Docs
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé