Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

Git - Reset

Created by : Mr Dk.

2019 / 04 / 04 17:29

Ningbo, Zhejiang, China


About

用 Git 的时候,有时候会遇到一些需要撤销更改,或者回退的情况。以前是自己一个人玩,基本不会遇到类似的问题。现在正在和一个牛 b 博士进行协作,动不动他就会把仓库弄乱 😑。今天花了点时间了解了一下 Git 的管理思维。

The Three Trees

不知道为何在 Git 中将其命名为 树。其实它的意思是一个 文件的集合,而不是数据结构。

TreeRole
HEADLast commit snapshot, next parent
IndexProposed next commit snapshot
Working DirectorySandbox

其中,HEAD 和 Index 记录在 /.git 文件夹中,Working Derectory 就是展现的工作目录。

  • HEAD 是当前分支引用的指针,指向分支上的最后一次提交。即 上一次提交的快照,是开始一次编辑之前的初始状态。

  • Index 是 预期的下一次提交,也就是 Git 中所谓的 暂存区域:stage。在其中,一些文件将会被替换为新版本。

  • 工作目录中的文件在提交至暂存区之前,可以随意编辑。

Workflow

Git 主要通过操作上述三棵树的状态来进行版本控制,直观上来看:

  • 将 Working Directory 中的变更提交到暂存区 Index
  • 将暂存区 Index 中的变更提交到 HEAD
  • 树之间可以进行状态转移、切换
git-reset-workflow

一次正常的提交过程

假设 Git 仓库中最后一次提交的 file.txt 文件的版本为 V1。在上一次的 git commit 提交完成之后:

git-last-commit

接下来,对工作目录中的 file.txt 进行了相应的改动,改动后的版本为 V2:

git-edit-file

接下来,使用 git add file.txt 将 V2 版本保存到暂存区:

git-add-file

最后,使用 git commit 将 V2 版本从暂存区中提交,HEAD 指针前进:

git-commit

只要三棵树的状态一致,就认为目录是干净的。

在切换分支时:

  • 修改 HEAD 指针指向对应分支
  • 将 Index 填充为该次提交的快照
  • 将 Index 中的内容复制到 Working Directory 中

Reset

Soft

reset 做的第一件事是移动 HEAD 指针的指向。本质上相当于 撤销了上一次的 git commit,回滚到了 git commit 之前。git reset --soft 在这一步结束后停止:

git-reset-soft

在这一步中,可以对 Index 进行更新,并重新 git commit。

Mixed

reset 要做的第二件事,除了上一步的 HEAD 指针前移以外,还会恢复 Index 的状态。这是 git reset 的默认行为。

本质是:撤销上一次的 git commit,还会取消暂存所有的东西。相当于回滚到 git commit 和 git add 命令执行之前。git reset [--mixed] 在这一步结束后停止。

git-reset-mixed

Hard

reset 做的第三件事是将 Working Directory 恢复到 HEAD 指向的状态。在这一步下,撤销了所有的 git commit、git add 和 Working Directory 中的所有修改。git reset --hard 在这一步结束后停止:

git-reset-hard

git reset --hard 是一个危险的操作,因为它强制覆盖了工作目录中的文件,无法恢复。

Review

Git 的 reset 命令以特定顺序重写三棵树:

  • 移动 HEAD 指针 - 指定 --soft 则到此停止
  • 恢复 Index - 指定 --mixed 或不指定(默认),则到此停止
  • 恢复 Working Directory - 指定 --hard 则到此停止

可以为 reset 提供一个作用路径,将作用范围限定为指定的文件或文件集合。由于 HEAD 是一个指针,无法同时指向两个提交,因此会跳过这一步;但 Index 和 Working Directory 可以部分更新。

假如运行 git reset file.txt (git reset --mixed HEAD file.txt)

  1. 移动 HEAD 指针 (跳过)
  2. 将 Index 恢复至 HEAD 的对应版本

本质上:将 file.txt 从 HEAD 复制到 Index 中;实际上:产生了 取消暂存文件 的效果。

git-reset-file

Summary

挺复杂的 有了图就容易理解多了。归纳一下可能会用的比较多的场景:

  1. 取消暂存的文件

    $ git reset HEAD <file_name>
    
  2. 撤销对文件的修改

    $ git checkout -- <file_name>
    
Edit this page on GitHub
Prev
Git - Rebase
Next
Git - Stash