- 作用:將工作從暫存區(qū)退回到工作區(qū)
- 有下面幾種參數(shù)
-
git reset HEAD filename
將filename從暫存區(qū)退回到工作區(qū)
先進(jìn)行提交到暫存區(qū):[lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git add a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git status On branch dev Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: a.txt
回退:
[lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git reset head Unstaged changes after reset: M a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git status On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: a.txt no changes added to commit (use "git add" and/or "git commit -a")
-
git reset head~n
mixed方式,默認(rèn)無(wú)參;
回退版本,^和n代表版本數(shù),這里直接回退到未暫存階段
先用git log -n
查看一下最近n次提交的日志:
lkcdeMacBook-Pro:gitSample lkc$ git log -3 commit 037a1bdaa48667e2fa3e62678b3d3f62dc2b6b7b Author: xxx <xxx.com> Date: Tue Jun 6 10:11:10 2017 +0800 e2 commit f9c2fdc70cfa68d7c42466b5f8f3a5cab1bc651c Author: xxx <xxx.com> Date: Tue Jun 6 10:10:26 2017 +0800 a commit 948836a561b210d14424a1efe631cda549b566f3 Author: lkc <lkc@lkcdeMacBook-Pro.local> Date: Mon Jun 5 17:10:15 2017 +0800 commit information
回退之后查看:
lkcdeMacBook-Pro:gitSample lkc$ git reset head~2 lkcdeMacBook-Pro:gitSample lkc$ git log -3 commit 948836a561b210d14424a1efe631cda549b566f3 Author: lkc <lkc@lkcdeMacBook-Pro.local> Date: Mon Jun 5 17:10:15 2017 +0800 commit information
-
git reset --soft head~1
將版本庫(kù)軟回退到前一個(gè)版本,就是將將本地版本庫(kù)的頭指針重置到上一個(gè)版本,且將這次回退之后的所有變更移到緩存區(qū).
lkcdeMacBook-Pro:gitSample lkc$ git log -3 commit c70659c624382a8b87de1c19063989fc38c44f4c Author: xxx <xxx.com> Date: Tue Jun 6 10:22:49 2017 +0800 e2,f2 commit 43e7ac4eddf470c30b29c9b37c53deb65cf78222 Author: xxx <xxx.com> Date: Tue Jun 6 10:22:22 2017 +0800 c2,d2 commit 62fe9fd3c5fe0de520000bae8b435502663373c2 Author: xxx <xxx.com> Date: Tue Jun 6 10:22:08 2017 +0800 a2,b2
軟回退:
lkcdeMacBook-Pro:gitSample lkc$ git reset --soft head~1 lkcdeMacBook-Pro:gitSample lkc$ git log -3 commit 43e7ac4eddf470c30b29c9b37c53deb65cf78222 Author: xxx <xxx.com> Date: Tue Jun 6 10:22:22 2017 +0800 c2,d2 commit 62fe9fd3c5fe0de520000bae8b435502663373c2 Author: xxx <xxx.com> Date: Tue Jun 6 10:22:08 2017 +0800 a2,b2 commit 948836a561b210d14424a1efe631cda549b566f3 Author: lkc <lkc@lkcdeMacBook-Pro.local> Date: Mon Jun 5 17:10:15 2017 +0800 commit information lkcdeMacBook-Pro:gitSample lkc$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: e.txt new file: f.txt
-
git reset --hard head~1
回退版本,不僅將本地版本庫(kù)的頭指針指向上一個(gè)版本還會(huì)重置暫存區(qū),并將本地的工作區(qū)代碼也回退到該版本.
原來(lái)e.txt的內(nèi)容:this is e2.txt
修改后:
this is e3.txt
提交:
lkcdeMacBook-Pro:gitSample lkc$ git add e.txt lkcdeMacBook-Pro:gitSample lkc$ git commit -m "e3" [master 6b057ba] e3 1 file changed, 1 insertion(+), 1 deletion(-) lkcdeMacBook-Pro:gitSample lkc$ git log -2 commit 6b057ba047f012b13b822016fb3b2f8b6946ae4b Author: xxx <xxx.com> Date: Tue Jun 6 10:32:25 2017 +0800 e3 commit 99c0d29602438a20bf4007e2494b52044a58187b Author: xxx <xxx.com> Date: Tue Jun 6 10:25:43 2017 +0800 e3,f3
回退并查看日志:
lkcdeMacBook-Pro:gitSample lkc$ git reset --hard head~1 HEAD is now at 99c0d29 e3,f3 lkcdeMacBook-Pro:gitSample lkc$ git log -2 commit 99c0d29602438a20bf4007e2494b52044a58187b Author: xxx <xxx.com> Date: Tue Jun 6 10:25:43 2017 +0800 e3,f3 commit 43e7ac4eddf470c30b29c9b37c53deb65cf78222 Author: xxx <xxx.com> Date: Tue Jun 6 10:22:22 2017 +0800 c2,d2
查看e.txt:
this is e2.txt