git reset 日常三种使用
创建演示目录
[test@host ~]$ mkdir git-reset-demo
[test@host git-reset-demo]$ cd git-reset-demo
[test@host git-reset-demo]$ git init ./
[test@host git-reset-demo]$ git branch
[test@host git-reset-demo]$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
首次提交
[test@host git-reset-demo]$ touch test.php
[test@host git-reset-demo]$ vim test.php
test.php
<?php
echo "test";
[test@host git-reset-demo]$ git commit -am 'add test.php'
On branch master
Initial commit
Untracked files:
test.php
nothing added to commit but untracked files present
[test@host git-reset-demo]$ git add test.php
[test@host git-reset-demo]$ git commit -am 'add test.php'
[test@host git-reset-demo]$ git log
commit a103f2660105306c37ed881edc61a18a777a44df (HEAD -> master)
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:18:40 2022 +0800
add test.php
第二次提交
新增一个文件
[test@host git-reset-demo]$ touch abc.txt
[test@host git-reset-demo]$ vim test.php
test.php 新增一行
<?php
echo "test";
echo "test again";
[test@host git-reset-demo]$ git diff
[test@host git-reset-demo]$ git commit -am 'echo test again'
漏提交 abc.txt,且不想在生存新的commit,还想修改commit message,可用git commit --amend
[test@host git-reset-demo]$ git add abc.txt
编辑commit message
[test@host git-reset-demo]$ git commit --amend abc.txt
git reset [–mixed] 回滚到上个commit
[test@host git-reset-demo]$ git log
commit e261cafc6c26ee70cb8557a913a8d9864e989188 (HEAD -> master)
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:20:47 2022 +0800
echo test again & add abc.txt
commit a103f2660105306c37ed881edc61a18a777a44df
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:18:40 2022 +0800
add test.php
[test@host git-reset-demo]$ git reset a103f2660105306c37ed881edc61a18a777a44df
Unstaged changes after reset:
M test.php
[test@host git-reset-demo]$ git status
第三次提交
[test@host git-reset-demo]$ add abc.txt
[test@host git-reset-demo]$ git status
[test@host git-reset-demo]$ git commit -am 'echo test again & add abc.txt again'
[work@majinbo-eopa git-reset-demo]$ git commit --amend
git reset --soft 回滚到上个commit
[test@host git-reset-demo]$ git log
commit 1dc2a66ffcc8640e53a76297c36cfd93a40df0dc (HEAD -> master)
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:23:02 2022 +0800
echo test again & add abc.txt & commit again
commit a103f2660105306c37ed881edc61a18a777a44df
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:18:40 2022 +0800
add test.php
[test@host git-reset-demo]$ git reset --soft a103f2660105306c37ed881edc61a18a777a44df
[test@host git-reset-demo]$ git status
第四次提交
# 无diff内容
[test@host git-reset-demo]$ git diff
[test@host git-reset-demo]$ git commit -am 'soft after commit'
git rest --hard 回滚到上个commit
[test@host git-reset-demo]$ git log
commit 87658b207d04d9dc7ede4a674a37fefb8f001f86 (HEAD -> master)
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:26:17 2022 +0800
soft after commit
commit a103f2660105306c37ed881edc61a18a777a44df
Author: majinbo <test@test.com>
Date: Fri Apr 29 10:18:40 2022 +0800
add test.php
[test@host git-reset-demo]$ git reset --hard a103f2660105306c37ed881edc61a18a777a44df
HEAD is now at a103f26 add test.php
[test@host git-reset-demo]$ git status
On branch master
nothing to commit, working tree clean
git branch
git reset 日常三种使用总结
影响范围 | HEAD | 索引(暂存区) | 工作目录 | 原有文件内容的变更 | 目录结构的变更(增加或者删除文件) |
---|---|---|---|---|---|
–mixed | 是 | 是 | 否 | 变更内容存在 | 新增文件:还存在,变成未add的状态 ;删除文件没有 |
–soft | 是 | 否 | 否 | 变更内容存在 | 新增文件:还存在,变成add的状态; 删除文件没有 |
–hard | 是 | 是 | 是 | 变更内容丢失 | 新增文件丢失、删除的文件相当于没删 |
QA
- Git是什么 ?
Git是目前世界上最先进的分布式版本控制系统, 工作区中有一个隐藏目录.git时,为Git的版本库。 - 如何理解HEAD、索引(暂存区)、工作目录?
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。