• git reset hard,mixed,soft


    首先,我们得了解git reset命令的形式之一:

    git reset [] []

    此命令的作用是恢复HEAD分支到位置,并根据决定是否恢复index file和working tree。恢复是指将staging area和working tree的状态还原到commit的状态(如果不指定commit,则默认为last commit)。如果没有pick哪个mode,即omitted mode,则mode默认为–mixed。可以为commit的hash id或引用值。

    此外,mode还有两个值:–soft和–hard。

    –soft不会改动(touch) index file和working tree,但是会撤销head到commit之间的all history,这是三个模式所共有的作用。

    $ git status
    
    On branch main
    
    Changes not staged for commit:
    
     (use "git add ..." to update what will be committed)
    
     (use "git restore ..." to discard changes in working directory)
    
    ​    modified:  README.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    $ git add README.md
    
    $ git status
    
    On branch main
    
    Changes to be committed:
    
     (use "git restore --staged ..." to unstage)
    
    ​    modified:  README.md
    
    $ git log --graph --oneline
    
    \* 0a071ac (HEAD -> main) A
    
    \* 0c888f4 Initial Commit
    
    $ git reset --soft 0c888f4
    
    $ git status
    
    On branch main
    
    Changes to be committed:
    
     (use "git restore --staged ..." to unstage)
    
    ​    modified:  README.md
    
    $ git log --graph --oneline
    
    \* 0c888f4 (HEAD -> main) Initial Commit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    可以观察到commit A的committed changes都reset为staged changes to be committed。原本已经stage等待commit的changes不会被reset。HEAD指向,HEAD到的提交都会被undo。

    –mixed在–soft的基础上,进一步reset,它会恢复index到时的状态。已经在index中的changes会reset回working tree,note that modify working tree≠reset working tree。

    $ git reset 0c888f4
    
    Unstaged changes after reset:
    
    M    README.md
    
    • 1
    • 2
    • 3
    • 4
    • 5

    git reset会报告影响到的changes。

    –hard会在–mixed的基础上恢复working tree的状态。被跟踪的changes会被丢弃。

    Any untracked files or directories in the way of writing any tracked files are simply deleted.

    Git文档中的这句话我不是太理解,通过询问ChatGPT,翻译为:任何阻碍跟踪文件的写入的未跟踪文件或目录将被删除。

    下面是一个Stack Overflow对该疑问的解释代码例子:

    There is a case in which git reset --hard has effects on untracked files.
    
    $ touch a.txt
    
    $ git add .
    
    $ git commit -m'A'
    
    $ echo hello >> a.txt
    
    $ git add .
    
    $ git commit -m'B'
    
    $ git rm a.txt
    
    $ echo world > a.txt
    
    $ git status -s
    
    The status output is
    
    D  a.txt
    
    ?? a.txt
    
    The a.txt in the index is removed and the one in the work tree is untracked.
    
    $ cat a.txt
    
    world
    
    Reset in the mode of --hard,
    
    $ git reset --hard
    
    $ cat a.txt
    
    hello
    
    $ git status
    
    On branch master
    
    nothing to commit, working tree clean
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    The untracked a.txt in the work tree is deleted. But we could also say it’s been overwritten with the tracked a.txt in HEAD.

  • 相关阅读:
    节点导纳矩阵
    vivo 在离线混部探索与实践
    C/C++整数和与均值 2019年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
    9月【笔耕不辍】勋章活动获奖名单公布
    【GIT版本控制】--高级分支策略
    Python 笔记01 (变量和面向对象编程)
    Filter过滤器
    用Python和TensorFlow实现图像分类:从零开始
    git的基本操作
    mysql性能优化之数据类型(持续更新)
  • 原文地址:https://blog.csdn.net/2201_75288929/article/details/134526816