1. 程式人生 > >git reset的用法

git reset的用法

soft 技術 dex working fir insert bsp 結果 epo

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。同時,從版本B到版本D之間做的文件修改並不會丟失,會保留版本D相對於B的diff。

git reset --hard

  

git reset的用法