git reset
如下所示的三次提交
commit 61dda81e8d2414bc2415d55c66f11889fc3c5f80 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:32:52 2015 +0800 third commit commit 489f1fa75ae537ce09bebdf121a18407827e7928 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:59 2015 +0800 second commit commit 5886426c4c12f8546496afe45edd06669c4a7863 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:07 2015 +0800 first commit
使用git reset进行回退和撤销操作,git reset有可选的三个参数
根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容
git reset -soft :取消了commit
git reset -mixed(默认) :取消了commit ,取消了add
git reset -hard :取消了commit ,取消了add,取消源文件修改
=================
sample git:(master) git reset --soft HEAD~2 ➜ sample git:(master) ✗ git log commit 5886426c4c12f8546496afe45edd06669c4a7863 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:07 2015 +0800 first commit
通过log可以看到回退到了第一次commit,soft模式回退了本地仓库的HEAD指针,没有回退暂存区域,也就是说直接commit还可以恢复提交,当运行git status时,可以看到有提示说
➜ sample git:(master) ✗ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: hello.txt
我们直接来运行git commit来恢复提交,
➜ sample git:(master) ✗ git commit -m 'recover' [master 103612f] recover 1 file changed, 2 insertions(+), 1 deletion(-) ➜ sample git:(master) git log commit 103612f4dc4f1ee5552977811f85a04cd28d6322 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:37:27 2015 +0800 recover commit 5886426c4c12f8546496afe45edd06669c4a7863 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:07 2015 +0800 first commit ➜ sample git:(master) vi hello.txt
同时文件的内容也恢复了,但两次的提交信息没有回复。
现在的git log,
commit 103612f4dc4f1ee5552977811f85a04cd28d6322 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:37:27 2015 +0800 recover commit 5886426c4c12f8546496afe45edd06669c4a7863 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:07 2015 +0800 first commit
通过git reset --hard 可以直接回退到第一次提交后的工作区域,直接舍弃暂存区域的修改。
➜ sample git:(master) git reset --hard HEAD~1 HEAD is now at 5886426 first commit
同时文件的内容也回退到第一次提交后的内容。
但是要注意:
当在push代码以后,使用git reset --hard <commit...> 回退代码到某个版本之前,但是这样会有一个问题,你线上的代码没有变,线上commit,index都没有变,当你把本地代码修改完提交的时候你会发现全是冲突.....这种情况要使用git revert
此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息。
现在的log信息,
commit 58add32d2dcd0598c1bfba0dea2dbb6575b87096 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:48:36 2015 +0800 second commit commit 5886426c4c12f8546496afe45edd06669c4a7863 Author: Yale Li <[email protected]> Date: Tue Aug 25 10:31:07 2015 +0800 first commit
运行git reset --mixed
➜ sample git:(master) git reset --mixed HEAD~1 Unstaged changes after reset: M hello.txt ➜ sample git:(master) ✗ git status On branch master 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
运行git log会发现第二次的提交信息没有了,但是文件的内容还在,标记为 modified。
效果如下,
➜ sample git:(master) ✗ git status On branch master 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a") ➜ sample git:(master) ✗ git add hello.txt ➜ sample git:(master) ✗ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: hello.txt ➜ sample git:(master) ✗ git reset HEAD hello.txt Unstaged changes after reset: M hello.txt ➜ sample git:(master) ✗ git status On branch master 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
效果如下
➜ sample git:(master) ✗ git checkout -- hello.txt ➜ sample git:(master) vi hello.txt
========END========