Git使用2:Git撤销操作
发布日期:2021-07-01 05:19:53 浏览次数:2 分类:技术文章

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

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://onefine.blog.csdn.net/article/details/86777341 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Django ORM与Scrapy集成
下一篇:Git使用1:Git基本命令

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年05月01日 10时03分31秒