Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 827
AlexWang Avatar asked Jun 11 '15 00:06

AlexWang


People also ask

What is the difference between git reset and git reset -- hard?

git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory. Only use this if you know what you're doing.

What does git reset -- hard do?

Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too.

What does git clean mean?

Summary. To recap, git clean is a convenience method for deleting untracked files in a repo's working directory. Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .

Is git reset hard Safe?

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.

What does Git reset-mixed mean?

Mixed: This command git reset -mixed is used to remove the file which we have committed using the git commit command. Hard: This command git reset -hard is used to remove all things which we have pushed in our code.We can further specify the point to which we want the code to be removed. Git Reset Hard Working

What is the use of Git reset hard command?

Git reset hard is a command which should be used carefully as to can delete your entire code commit history and what you have done till a particular commit. Therefore it should be used with certain caution. Apart from git reset hard there are other commands also which can be used to backtrack your changes such as git checkout.

How does Git clean work?

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.

What is the default mode for Git reset?

--mixed is the default mode for git reset. This variant changes the HEAD ref to point to the given commit. The contents of your index are also modified to agree with the tree structure named by the named commit. Furthermore, your working directory contents are changed to reflect the state of the tree represented by the given commit.


1 Answers

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 
like image 82
z atef Avatar answered Sep 21 '22 15:09

z atef