git reset -hard和git clean之间的区别

时间:2015-06-11 00:26:24

标签: git git-reset git-clean

您好我很好奇这两个命令之间的区别。当他们在这里介绍时:https://www.atlassian.com/git/tutorials/undoing-changes

看起来像git reset --hard还设置了staging和工作目录以匹配最新的提交,但最后他们说git reset --hard不会改变当前的工作目录。所以我在这里很困惑,有人可以澄清一下吗?

1 个答案:

答案 0 :(得分:10)

他们做了两件不同的事情。比方说,你做了GIT PULL,然后开始编辑一些文件,可能已经添加并将这些更改提交给被推送...然后由于某种原因你决定放弃已经对所做的所有更改给定文件并返回较早的状态。在你将要做的情况下

$ 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

选择要回滚的提交,如下所示:

git reset --hard 73b9363

重置HEAD后,所有更改/暂存文件都将消失。

至于git clean。以下是git-scm.com对其的描述。

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.

有关重置与清洁及其 - 选项的更多信息

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