AlexWang
AlexWang

Reputation: 379

Difference between git reset --hard and git clean

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?

Upvotes: 32

Views: 14621

Answers (2)

Peet Brits
Peet Brits

Reputation: 3247

Overview

  • git reset = remove tracked changes.
  • git clean = remove untracked changes.

Example: Reset

Reset the current HEAD (tracked files) to the specified state.

$ git reset
$ git reset --hard
  • git reset = unstage all staged changes.
  • --hard = discard all tracked changes (staged and unstaged), and delete files and directories in the way of writing tracked files.
    • (outcome: reset the index of the working tree).

Example: Clean

Remove files not tracked, skipping ignored files (unless -x).

$ git clean -f -d
$ git clean -f -d -x
  • -f = force (might be required based on other settings).
  • -d = includes untracked directories.
  • -x = skip ignore rules (e.g. will delete files ignored by ".gitignore", like the bin and .vs folders).

Upvotes: 0

z atef
z atef

Reputation: 7679

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

Upvotes: 23

Related Questions