Git 基础 - 回滚撤销commit

Posted wooluwalker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git 基础 - 回滚撤销commit相关的知识,希望对你有一定的参考价值。

 

1 ###################################################################
[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog * db69602 (HEAD -> dev) add test_reset.md * fab7157 dev修改READM.md * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 * a565481 (origin/dev) Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reset dev^ #撤销上一次commit
[@wooluwalkerdeMacBook
-Pro:wenjian (dev)]$ git dog * fab7157 (HEAD -> dev) dev修改READM.md * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 * a565481 (origin/dev) Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit 2 ######################################################################### 分三次commit 分别commit test_reset1.md、test_reset2.md、test_reset3.md 到暂存区 [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog * 7622464 (HEAD -> dev) test_reset3.md * 56afa1e test_reset2.md * 4797045 add test_reset1.md * fab7157 dev修改READM.md * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 * a565481 (origin/dev) Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reset dev^^^ #撤销最近三次的commit【或git rest dev~5】 [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog * fab7157 (HEAD -> dev) dev修改READM.md * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 * a565481 (origin/dev) Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit

 



 

--hard
工作目录和暂存区的文件都会丢弃
[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog
* 3a7c43c (HEAD -> dev) add  test_reset1.md,test_reset2.md,test_reset3.md #最近的一次commit
* 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 #尝试回退到该版本
| * 4e893c1 (origin/dev) add  test_reset1.md,test_reset2.md,test_reset3.md
|/  
* a565481 Update README.md
*   19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev
|  
| * b1777d8 commit merge_excel_utils
| * ee37473 manage utils
| * 0b6e16c commit files
* | 441940f 添加项目说明
|/  
* 0f1f9d7 Initial Commit
[@wooluwalkerdeMacBook
-Pro:wenjian (dev)]$ git reset --hard 217fef2 #hard模式回退 HEAD is now at 217fef2 Update README.md,添加日期 [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git st #工作区 和 暂存区都是空的 On branch dev Your branch and origin/dev have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ ll # 三个文件 test_reset1.md,test_reset2.md,test_reset3.md 在工作区被删除
total 24
drwxr-xr-x  10 walker  staff   320B  2  2 22:10 .
drwxr-xr-x   9 walker  staff   288B  2  2 22:03 ..
-rw-r--r--   1 walker  staff   6.0K  2  2 20:16 .DS_Store
drwxr-xr-x  15 walker  staff   480B  2  2 22:10 .git
-rw-r--r--   1 walker  staff    73B  2  2 22:10 README.md
-rw-r--r--   1 walker  staff     0B  2  2 13:08 __init__.py
drwxr-xr-x   7 walker  staff   224B  2  2 13:08 __pycache__
drwxr-xr-x   9 walker  staff   288B  2  2 13:08 data
drwxr-xr-x   6 walker  staff   192B  2  2 13:08 merge
drwxr-xr-x   8 walker  staff   256B  2  2 13:08 utils

 


--mixed

丢掉暂存区文件,但是不会丢弃工作目录的文件【默认是这种模式】

[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog * 4e893c1 (HEAD -> dev, origin/dev) add test_reset1.md,test_reset2.md,test_reset3.md #最新的一次提交 | * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 #测试回退到该版本 |/ * a565481 Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch ‘dev‘ | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reset 217fef2 #默认--mixed 模式 Unstaged changes after reset: M README.md [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git st #reset之后,暂存区中的文件也被删除 On branch dev Your branch and ‘origin/dev‘ have diverged, and have 1 and 1 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: README.md Untracked files: (use "git add <file>..." to include in what will be committed) # 但是工作目录中的文件还存在 test_reset1.md test_reset2.md test_reset3.md no changes added to commit (use "git add" and/or "git commit -a") [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ ll #工作目标中的文件还存在 total 48 drwxr-xr-x 13 walker staff 416B 2 2 22:02 . drwxr-xr-x 9 walker staff 288B 2 2 22:03 .. -rw-r--r-- 1 walker staff 6.0K 2 2 20:16 .DS_Store drwxr-xr-x 15 walker staff 480B 2 2 22:05 .git -rw-r--r-- 1 walker staff 54B 2 2 22:00 README.md -rw-r--r-- 1 walker staff 0B 2 2 13:08 __init__.py drwxr-xr-x 7 walker staff 224B 2 2 13:08 __pycache__ drwxr-xr-x 9 walker staff 288B 2 2 13:08 data drwxr-xr-x 6 walker staff 192B 2 2 13:08 merge -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset1.md -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset2.md -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset3.md drwxr-xr-x 8 walker staff 256B 2 2 13:08 utils

 

--soft
工作目录和暂存区的文件都不会丢弃

[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git dog * f9069ec (HEAD -> dev) add test_reset1.md,test_reset2.md,test_reset3.md #本地dev分支最近一次提交 * 217fef2 (origin/master, origin/HEAD, master) Update README.md,添加日期 #尝试回退到该版本 | * 4e893c1 (origin/dev) add test_reset1.md,test_reset2.md,test_reset3.md |/ * a565481 Update README.md * 19cfeea 第一次将远程dev分支代码合并到master分支Merge branch dev | | * b1777d8 commit merge_excel_utils | * ee37473 manage utils | * 0b6e16c commit files * | 441940f 添加项目说明 |/ * 0f1f9d7 Initial Commit [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reset --soft 217fef2 #回退到该版本 [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git st # 暂存区中的文件还存在 On branch dev Your branch and origin/dev have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: test_reset1.md new file: test_reset2.md new file: test_reset3.md [@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ ll #工作目录中的文件也还存在 total 48 drwxr-xr-x 13 walker staff 416B 2 2 22:14 . drwxr-xr-x 6 walker staff 192B 2 2 22:14 .. -rw-r--r-- 1 walker staff 6.0K 2 2 20:16 .DS_Store drwxr-xr-x 15 walker staff 480B 2 2 22:15 .git -rw-r--r-- 1 walker staff 73B 2 2 22:10 README.md -rw-r--r-- 1 walker staff 0B 2 2 13:08 __init__.py drwxr-xr-x 7 walker staff 224B 2 2 13:08 __pycache__ drwxr-xr-x 9 walker staff 288B 2 2 13:08 data drwxr-xr-x 6 walker staff 192B 2 2 13:08 merge -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset1.md -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset2.md -rw-r--r-- 1 walker staff 15B 2 2 19:57 test_reset3.md drwxr-xr-x 8 walker staff 256B 2 2 13:08 utils

 

执行完git reset --hard 之后被丢失的  test_reset1.md,test_reset2.md,test_reset3.md 三个文件如何找回:

 

[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reflog
217fef2 (HEAD -> dev, origin/master, origin/HEAD, master) HEAD@{0}: reset: moving to 217fef2 #上一次reset
8176e66 HEAD@{1}: commit: add test_reset1.md,test_reset2.md,test_reset3.md #上一次添加三个文件,回退到该版本即可
217fef2 (HEAD -> dev, origin/master, origin/HEAD, master) HEAD@{2}: reset: moving to 217fef2
f9069ec HEAD@{3}: commit: add test_reset1.md,test_reset2.md,test_reset3.md
217fef2 (HEAD -> dev, origin/master, origin/HEAD, master) HEAD@{4}: reset: moving to 217fef2
3a7c43c HEAD@{5}: commit: add test_reset1.md,test_reset2.md,test_reset3.md
217fef2 (HEAD -> dev, origin/master, origin/HEAD, master) HEAD@{6}: reset: moving to 217fef2
4e893c1 (origin/dev) HEAD@{7}: commit: add test_reset1.md,test_reset2.md,test_reset3.md
a565481 HEAD@{8}: reset: moving to a565481
217fef2 (HEAD -> dev, origin/master, origin/HEAD, master) HEAD@{9}: reset: moving to 217fef2

[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ git reset --hard 8176e66
HEAD is now at 8176e66 add  test_reset1.md,test_reset2.md,test_reset3.md
[@wooluwalkerdeMacBook-Pro:wenjian (dev)]$ ll # 工作区中的三个文件还存在
total 48
drwxr-xr-x  13 walker  staff   416B  2  2 22:27 .
drwxr-xr-x   6 walker  staff   192B  2  2 22:14 ..
-rw-r--r--   1 walker  staff   6.0K  2  2 20:16 .DS_Store
drwxr-xr-x  15 walker  staff   480B  2  2 22:27 .git
-rw-r--r--   1 walker  staff    73B  2  2 22:10 README.md
-rw-r--r--   1 walker  staff     0B  2  2 13:08 __init__.py
drwxr-xr-x   7 walker  staff   224B  2  2 13:08 __pycache__
drwxr-xr-x   9 walker  staff   288B  2  2 13:08 data
drwxr-xr-x   6 walker  staff   192B  2  2 13:08 merge
-rw-r--r--   1 walker  staff    15B  2  2 22:27 test_reset1.md
-rw-r--r--   1 walker  staff    15B  2  2 22:27 test_reset2.md
-rw-r--r--   1 walker  staff    15B  2  2 22:27 test_reset3.md
drwxr-xr-x   8 walker  staff   256B  2  2 13:08 utils

 

以上是关于Git 基础 - 回滚撤销commit的主要内容,如果未能解决你的问题,请参考以下文章

持续集成 - git版本回滚

git commit/push后如何回退

Git重置代码的4种操作

git怎么将commit的数据 回滚

Git回滚操作的总结

Git 代码撤销回滚到任意版本(当误提代码到本地或master分支时)