28

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

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

4

1 回答 1

18

他们做两件不同的事情。比方说,你做了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.

更多关于 reset vs clean 和他们的 --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
于 2015-06-11T00:55:22.673 回答