git reset的用法
git reset三个选项
--mix,--hard,--soft
数据
针对每个选项都是操作这个文件。
[root@centos demo]# git init Initialized empty Git repository in /root/demo/.git/ [root@centos demo]# echo one >> a.txt [root@centos demo]# git add a.txt [root@centos demo]# git commit -m "first commit" [master (root-commit) b7ee3a2] first commit 1 file changed, 1 insertion(+) create mode 100644 a.txt [root@centos demo]# echo two >> a.txt [root@centos demo]# git commit -am "second commit" [master 92ae9c4] second commit 1 file changed, 1 insertion(+) [root@centos demo]# echo three >> a.txt [root@centos demo]# git commit -am "third commit" [master 0985eec] third commit 1 file changed, 1 insertion(+) [root@centos demo]# echo four >> a.txt [root@centos demo]# git commit -am "four commit" [master 5bd480c] four commit 1 file changed, 1 insertion(+) [root@centos demo]# git show-branch --more=4 #查看四次提交记录 [master] four commit [master^] third commit [master~2] second commit [master~3] first commit
git reset --mix
在省略reset选项的时候,默认的就是使用--mix
[root@centos demo]# git reset HEAD~2 Unstaged changes after reset: M a.txt [root@centos demo]# 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: a.txt no changes added to commit (use "git add" and/or "git commit -a") [root@centos demo]# cat a.txt one two three four [root@centos demo]# git diff diff --git a/a.txt b/a.txt index 69f75fc..a4c0ca1 100644 --- a/a.txt +++ b/a.txt @@ -1,2 +1,4 @@ one two +three +four
从运行结果可以看出来,--mix有以下特点:
假设使用reset命令从版本D回到版本B,那么HEAD就会指向B。同时,版本D的代码内容并不会删除,会保留版本D相对于B的diff。
可以使用git reset ORIG_HEAD回到之前的版本。
git reset --hard
对原始数据进行下面操作:
[root@centos demo]# git show-branch --more=4 [master] four commit [master^] third commit [master~2] second commit [master~3] first commit [root@centos demo]# git reset --hard HEAD~2 HEAD is now at e57a55f second commit [root@centos demo]# git show-branch --more=4 [master] second commit [master^] first commit [root@centos demo]# git status On branch master nothing to commit, working tree clean [root@centos demo]# cat a.txt one two
从运行结果可以发现,--hard选项有以下特点:
使用--hard选项回到版本B的啥时候,HEAD会回到版本B,同时,代码内容也会回到版本B,也就是说,版本B之后的内容都被删除了。
但是还是可以使用git reset ORIG_HEAD回到版本D。然后版本D的内容就会恢复,但是需要再次git add && git commit。
git reset --soft
[root@centos demo]# git show-branch --more=5 [master] four commit [master^] third commit [master~2] second commit [master~3] first commit [root@centos demo]# git reset --soft HEAD~2 [root@centos demo]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: a.txt [root@centos demo]# git diff #注意上面一个命令提示修改了a.txt,但是却没有diff [root@centos demo]# cat a.txt #a文件的内容仍旧没变 one two three four [root@centos demo]# git reset ORIG_HEAD [root@centos demo]# git show-branch --more=5 [master] four commit [master^] third commit [master~2] second commit [master~3] first commit
--soft选项很特别,和--mix很相似,区别是:
1、如果从D版本回到B版本,进行git diff时,不会显示版本D相对于版本B的diff,因为在B处执行git diff时,是和D进行diff,而不是和B进行diff(但是执行git status时,还是会提示有变化,但是可以忽略)
2、在从B版本回到D版本后,不用在进行git add && git commit。可以认为HEAD一直都指向D,执行B的只是一个tmp_head。
如需转载,请注明文章出处,谢谢!!!