Languages
[Edit]
EN

Git reset hard with untracked files removal

5 points
Created by:
Root-ssh
6357

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
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.