git reset

2017-06-12  本文已影响215人  xdlkc
  1. git reset HEAD filename

    将filename从暂存区退回到工作区
    先进行提交到暂存区:

    [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")
    
  1. git reset head~n

     mixed方式,默认无参;    
    

    回退版本,^和n代表版本数,这里直接回退到未暂存阶段
    先用

    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
    
  2. git reset --soft head~1

    将版本库软回退到前一个版本,就是将将本地版本库的头指针重置到上一个版本,且将这次回退之后的所有变更移到缓存区.

    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
    
  3. git reset --hard head~1

    回退版本,不仅将本地版本库的头指针指向上一个版本还会重置暂存区,并将本地的工作区代码也回退到该版本.
    原来e.txt的内容:

    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
    
git reset帮助
上一篇下一篇

猜你喜欢

热点阅读