Git使用2:Git撤销操作

Git撤销操作

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

提交到版本库:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
nothing to commit, working tree clean

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1> this is a html file, version 1.0</h1>
</body>
</html>

:wq
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch 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:   index.html

no 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 status
On branch master
nothing to commit, working tree clean

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

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git log --oneline
3058e7a (HEAD -> master) version 1.0
1d6647a modify style.css
e922fd7 modify index.html
13d63f2 add style.css
039d4a6 first commit

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1>version 1.0</h1>
</body>
</html>

:wq
ONEFINE@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.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html


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

version 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 --oneline
f6af45a (HEAD -> master) version 1.0
1d6647a modify style.css
e922fd7 modify index.html
13d63f2 add style.css
039d4a6 first commit

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

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

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

version 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 --oneline
db28546 (HEAD -> master) version 1.0  , this is new!
1d6647a modify style.css
e922fd7 modify index.html
13d63f2 add style.css
039d4a6 first commit

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

接下来对误操作的撤销做介绍。
假设有小屁孩乱动了你的index.html文档,就像这样:

在这里插入图片描述

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ cat index.html
<!DOCTYdfaPE html>
<html lang="en">
<heaadfdafad>dafa
    d<madfdaeta charsetafdfaf="UTF-8">daf
    <title>hello wodafrldda</title>
</head>
<body>
    <p>onefdfadfine</p>
    <h1>vdersion 1.0</h1>
</body>
</html>
adfdfdasffdaf
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch 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:   index.html

no 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.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
nothing to commit, working tree clean

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1>version 1.0</h1>
</body>
</html>

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

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

git reset HEAD <file>

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1>version 2.0</h1>
</body>
</html>

:wq
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch 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:   index.html

no 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 status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html


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

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git reset HEAD index.html
Unstaged changes after reset:
M       index.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch 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:   index.html

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1>version 2.0</h1>
</body>
</html>

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

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git checkout -- index.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
nothing to commit, working tree clean

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
    <h1>version 1.0</h1>
</body>
</html>

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

HEAD介绍:

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git log
commit db28546bf7287af75f26f5367440b2f56bd2c4f0 (HEAD -> master)
Author: onefine <188302531@qq.com>
Date:   Sun Feb 10 08:28:37 2019 +0800

    version 1.0  , this is new!

commit 1d6647a03772dc0f155040585117437b9714d1e4
Author: 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.html
Unstaged changes after reset:
M       index.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html

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:   index.html


ONEFINE@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.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html


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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
</body>
</html>

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 status
On branch master
nothing to commit, working tree clean

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

原文地址:https://www.cnblogs.com/onefine/p/10499357.html