博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git使用2:Git撤销操作
阅读量:4623 次
发布时间:2019-06-09

本文共 8491 字,大约阅读时间需要 28 分钟。

Git撤销操作

  • git commit --amend 撤销上一次提交并将暂存区的文件重新提交
  • git checkout --filename 拉取暂存区的文件并将其替换工作区的文件
    • 注意与git checkout branchname区别,这个命令将用来切换分支
  • git reset HEAD --filename 拉取最近一次提交的版本库中的这个文件到暂存区,该操作不影响工作区

提交到版本库:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.html    
hello world

onefine

this is a html file, version 1.0

:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -am 'version 1.0'[master 3058e7a] version 1.0 1 file changed, 1 insertion(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

假设还有细微的bug需要修改:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --oneline3058e7a (HEAD -> master) version 1.01d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.html    
hello world

onefine

version 1.0

:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

此时,如果要重新提交就不能直接使用原来的版本。

这时使用git commit -amend 撤销上一次提交并将暂存区的文件重新提交。

git commit -amend

git commit -amend :撤销上一次提交并将暂存区的文件重新提交,形成一个新的版本。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit --amendversion 1.0# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date: Sun Feb 10 08:28:37 2019 +0800## On branch master# Changes to be committed:# modified: index.html#:q[master f6af45a] version 1.0 Date: Sun Feb 10 08:28:37 2019 +0800 1 file changed, 1 insertion(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

再次使用git log命令来查看一下:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --onelinef6af45a (HEAD -> master) version 1.01d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

如果想仅修改版本的描述而不对内容做任何的改变,也可以使用此命令:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit --amendversion 1.0  , this is new!# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Sun Feb 10 08:28:37 2019 +0800## On branch master# Changes to be committed:#       modified:   index.html#:wq[master db28546] version 1.0  , this is new! Date: Sun Feb 10 08:28:37 2019 +0800 1 file changed, 1 insertion(+) ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git log --onelinedb28546 (HEAD -> master) version 1.0  , this is new!1d6647a modify style.csse922fd7 modify index.html13d63f2 add style.css039d4a6 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

接下来对误操作的撤销做介绍。

假设有小屁孩乱动了你的index.html文档,就像这样:

在这里插入图片描述

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html
dafa d
daf
hello wodafrldda

onefdfadfine

vdersion 1.0

adfdfdasffdafONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

如果想回到之前的样子,可以怎么做呢?

git checkout

git checkout --filename 拉取暂存区的文件并将其替换工作区的文件。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html    
hello world

onefine

version 1.0

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

或者使用git checkout -- .来取消全部的错误操作。

git reset HEAD <file>

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim index.html    
hello world

onefine

version 2.0

:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add .ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD
..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

如果想撤销现在暂存区的信息,恢复到以前暂存区的内容,可以使用git reset HEAD <file>命令:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git reset HEAD index.htmlUnstaged changes after reset:M       index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html
hello world

onefine

version 2.0

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

注意此时工作区的文件还是被修改之后的,只是已经回到了被修改状态,想要恢复直接使用git checkout -- .就可以了。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html    
hello world

onefine

version 1.0

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

HEAD介绍:

头指针,一直指向最新一次的提交(Committed状态),所有可以使用版本号来替代HEAD,如:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit db28546bf7287af75f26f5367440b2f56bd2c4f0 (HEAD -> master)Author: onefine <188302531@qq.com>Date:   Sun Feb 10 08:28:37 2019 +0800    version 1.0  , this is new!commit 1d6647a03772dc0f155040585117437b9714d1e4Author: onefine <188302531@qq.com>Date:   Fri Feb 8 10:17:14 2019 +0800    modify style.css...省略...内容太多了ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

注意版本号可以不写全。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git reset 1d6647a03772dc0f15 index.htmlUnstaged changes after reset:M       index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) modified: index.htmlChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

这里出现两条提示

1、Changes to be committed,这是因为git reset只是拉回到暂存区,git在比较暂存区跟版本库的时候发现两个文件是不一样的。
2、Changes not staged for commit,这是因为暂存区和工作区的文件不一样。

这时使用git checkout将原暂存区文件拉回工作区:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git checkout -- index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat index.html
hello world

onefine

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

重新提交到版本库

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -m 'reset rewirte'[master f1e248e] reset rewirte 1 file changed, 1 deletion(-)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

转载于:https://www.cnblogs.com/onefine/p/10499357.html

你可能感兴趣的文章
wc命令
查看>>
CentOS下安装mysql及配置使用
查看>>
gevent拾遗
查看>>
Metro学习笔记+心得+体会(四)
查看>>
正则表达式
查看>>
Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
查看>>
C++源码的调用图生成
查看>>
滚动居中效果_带遮罩效果
查看>>
JSP学习总结(一)
查看>>
Sublime Text3配置Vue 语法
查看>>
验证控件:RegularExpressionValidator
查看>>
hdu1166 线段树单点修改与区间查询
查看>>
asp.net -mvc框架复习(7)-基于MVC搭建用户登录项目框架
查看>>
CSS background-clip 属性
查看>>
Windows下msysGit安装
查看>>
python中函数作用域
查看>>
C#版清晰易懂TCP通信原理解析(附demo)
查看>>
系统自带的粒子系统
查看>>
Laravel 框架的主要版本
查看>>
pandas学习笔记 - 常见的数据处理方式
查看>>