2015-06-11 37 views
11

您好我很好奇這兩個命令之間的區別。當他們在這裏介紹時:https://www.atlassian.com/git/tutorials/undoing-changes的復位 - 硬和git乾淨

看起來像git reset --hard也設置暫存和工作目錄以匹配最新的提交,但最終他們說git reset --hard不會改變當前工作目錄。所以我在這裏很困惑,有人能澄清它嗎?

+1

其中*不*它說「了Git的復位 - 硬將不會改變當前工作目錄'?它明確地說相反 - 「換句話說:這個** [硬重置]消除了所有未提交的更改**」。 – user2864740

+0

此外,要注意「請記住,[硬盤] ** *復位不僅影響跟蹤文件***,所以單獨[清潔]命令需要清理未跟蹤的。」 – user2864740

+0

@ user2864740右它抹殺了所有未提交的變化,但如果還重置工作目錄相匹配的最後一次提交,爲什麼我們仍然需要清潔的Git命令,因爲在這種情況下,不會有任何未跟蹤文件作爲工作目錄和臨時區域是相同的 – AlexWang

回答

5

他們這樣做是兩回事。比方說,你做了GIT PULL,然後開始編輯一些文件,並且可能已經添加並提交了這些更改,然後由於某種原因,您決定放棄對給定文件所做的所有更改並轉至回到以前的狀態。在情況下,你會做

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

選擇的承諾,你要回滾到,像這樣:

git reset --hard 73b9363 

復位後頭部,所有未提交的修改/文件將會消失。

至於git的清潔。以下是git-docs的描述。

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. 

更多關於復位VS乾淨,他們--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 


    [1]: http://git-scm.com/docs/git-clean 
+0

感謝分享。這是否意味着這是一個強大的命令,可能會影響項目的未來編碼? – AlexWang

+0

它會影響最近的更改,並重置爲早期的提交,告訴git垃圾回收自上次提交後的所有新更改。 – zee

+0

Wrt「所有更改/上演的文件將會消失」 - 這隻會影響*未提交*的更改。承諾的更改(如reflog所示)將始終保持不變,只要它們可以訪問,就可以隨時恢復 - 例如。通過分支,標籤或孩子。 (唯一一次git'失去'一個提交是GC運行時,它們*不可*,除了id。)重寫提交是一個不同的野獸,但git reset根本無法修改/刪除以前的提交。 – user2864740