2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!

Fork me on GitHub

大话git中的撤销操作

 

下面以现实场景作为情境。

基础知识,理解git中的几个区域

本地代码已经add,未commit

修改本地工作目录中的readme.md,添加文字"第一次修改"

然后查看下状态

➜  experimentation git:(master) ✗ git status
On branch master
Your branch is up-to-date with 'origin/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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

进行Add操作,并查看状态

➜  experimentation git:(master) ✗ git add README.md
➜  experimentation git:(master) ✗ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

➜  experimentation git:(master) ✗

这时候,变动进入了缓存区(Index)

但是我们突然发现我们改动错了,其实我是想改动experimentation.txt文件。

方法一:我们当然可以再次修改experimentation和readme文件(删除readme中的修改,给experimentation中添加“第一次修改”),然后再Add

方法二:我们想的可能是撤回之前的add操作,然后给experimentation中添加“第一次修改”,然后再次Add,那这样的话,我们如何操作呢?

  • 撤销某个add文件:git reset HEAD <filename>

  • 撤销全部add文件:git reset HEAD .

➜  experimentation git:(master) ✗ git reset HEAD README.md
Unstaged changes after reset:
M       README.md
➜  experimentation git:(master) ✗ git status
On branch master
Your branch is up-to-date with 'origin/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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Tip git log与git reflog的区别
git reflog 可以查看所有分支的所有操作记录(包括commit,reset的操作,已经被删除的commit记录)
git log不能察看已经删除的commit记录

本地代码已经add,已commit


将本地代码多commit几次,再来看下我们的提交log

  • 方法一(推荐):执行git revert <commitId>

撤回到指定的版本,包括文件和状态。

  • 方法二:执行git reset <commitId>
➜  experimentation git:(master) git reset 2a6c701
Unstaged changes after reset:
M       experimentation.txt
➜  experimentation git:(master) ✗ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 4 and 6 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
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:   experimentation.txt

no changes added to commit (use "git add" and/or "git commit -a")

回退至指定的版本,区别如下

Tip git revert与git reset的区别
git reset是直接删除指定的commit之前的所有commit(这是不安全的,特别是push后),把HEAD向后移动。
git revert是一次新的commit,HEAD继续前进,与普通的commit一样。

posted on   qize  阅读(586)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

0 commits in this month

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示