Difference between git reset --hard and git clean

13.4k views Asked by At

Hi I am curious about the difference between these two commands. When they introduce here: https://www.atlassian.com/git/tutorials/undoing-changes

Looks like git reset --hard also sets both the staging and the working directory to match the latest commit, but in the end they say that git reset --hard won't change the current working directory. So I am very confused here, can someone clarify it?

1

There are 1 answers

4
z atef On

They do two different things . Let say , you did GIT PULL and then started editing some files and probably have added and commited those changes to the be pushed ... and then for some reason you decided to just discard all the changes that have been made to the given files and go back an earlier state . in the case you will do

$ git reflog
... snip ...
cf42fa2... HEAD@{0}: commit: fixed misc bugs
~
~
cf42fa2... HEAD@{84}: commit: fixed params for .....
73b9363... HEAD@{85}: commit: Don't symlink to themes on deployment.
547cc1b... HEAD@{86}: commit: Deploy to effectif.com web server.
1dc3298... HEAD@{87}: commit: Updated the theme.
18c3f51... HEAD@{88}: commit: Verify with Google webmaster tools.
26fbb9c... HEAD@{89}: checkout: moving to effectif

Choose the commit that you want to roll back to, like so:

git reset --hard 73b9363

after resetting HEAD , all changes/staged files will be gone.

As for git clean . Below is how git-scm.com describes it.

DESCRIPTION
Cleans the working tree by recursively removing files that 
are not under version control, starting from the current directory.

Normally, only files unknown to Git are removed, but if the -x
option is specified, ignored files are also removed. This 
can, for example, be useful to remove all build products.

If any optional <path>... arguments are given, only those paths are affected.

More about reset vs clean and their --options

lnydex99uhc:~  user$ git reset -h
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] <tree-ish> [--] <paths>...
   or: git reset --patch [<tree-ish>] [--] [<paths>...]

    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    -p, --patch           select hunks interactively

VS

 lnydex99uhc:~ user$ git clean -h
    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