【git笔记】撤销修改

git笔记 撤销修改

说明

介绍几种git撤销修改的命令。环境:Ubuntu/Debian。

命令

1.撤销工作区修改

  1. 只是在工作区(working directory)中把代码修改了,但还没有把文件修改添加(git add)进暂存区(staging area)里。
git checkout -- <file>

执行后,会放弃工作区(working directory)中文件的更改,用暂存区(staging area)里该文件的内容覆盖工作区(working directory)中该文件的内容。

放弃工作区(working directory)中所有文件的更改,相当于用暂存区(staging area)里的内容覆盖工作区(working directory)中的内容。

git checkout -- .

或者是

git checkout .

2.撤销暂存区修改

  1. 代码已经添加到了暂存区(staging area),但还没提交(git commit)到本地仓库(local repository)里。
git reset HEAD <file>

执行后,会撤销暂存区(staging area)中文件的修改。这个时候暂存区(staging area)中该文件是处于当前版本的状态,而工作区(working directory)中的文件是处于有修改的状态。

撤销所有暂存区(staging area)的修改。这个时候整个暂存区(staging area)都是处于当前版本的状态,而工作区(working directory)是处于有修改的状态。

git reset HEAD -- .

或者是

git reset HEAD .

3.撤销本地仓库修改

  1. 代码已经提交(git commit)到本地仓库(local repository)里,但还没推送到远程仓库(remote repository)里。
  1. 只回退本地仓库(local repository)

    git reset --soft HEAD^

    执行后,本地仓库(local repository)回退到上一版本,暂存区(staging area)与工作区(working directory)保留不变。

  2. 回退本地仓库(local repository)和暂存区(staging area)

    git reset HEAD^

    或者

    git reset --mixed HEAD^

    执行后,本地仓库(local repository)回退到上一版本,清空暂存区(staging area),但是工作区(working directory)保留不变。

  3. 回退所有内容到上一个版本

    git reset --hard HEAD^

    执行后,回退所有内容到上一个版本,将暂存区(staging area)与工作区(working directory)都回到上一次版本,所有信息提交都被删除。

命令使用示例

1.撤销工作区修改示例

1.只是在工作区(working directory)中把代码修改了,但还没有把文件修改添加(git add)进暂存区(staging area)里。执行git checkout -- <file>后,会放弃工作目录中的更改,用暂存区(staging area)里内容覆盖工作区(working directory)中的内容。

下面具体操作两种情况,一种是从上次提交(git commit)到本地仓库(local repository)后,文件修改内容都没有再添加(git add)到暂存区(staging area)里,这个时候执行git checkout -- <file>,工作区(working directory)的内容会撤销恢复到和暂存区(staging area)一样,此时工作区(working directory),暂存区(staging area)和本地仓库(local repository)的内容都相同;另一种情况是之前修改文件有添加(git add)到暂存区(staging area)里,但没提交(git commit)到本地仓库(local repository)里,此时执行git checkout -- <file>,工作区(working directory)的内容会撤销恢复到和暂存区(staging area)一样,但和本地仓库(local repository)的内容不一样。

情况1

假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

add test

使用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:   README.md
#

这个时候执行git checkout -- README.md

然后再用git status命令查看一下,发现工作区(working directory)干净了

# On branch master
nothing to commit (working directory clean)

使用cat README.md查看下README.md这个文件的内容

test

发现内容恢复到了之前的状态。

情况2

假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

add test

使用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:   README.md
#

使用命令git add README.md将文件添加进暂存区(staging area)里。

使用git status命令查看一下

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#

已经将修改添加进了暂存区(staging area)里。

这个时候再修改文件README.md的内容,修改成

add test 2

使用git status命令查看一下

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

这个时候执行git checkout -- README.md

然后再用git status命令查看一下

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#

使用cat README.md查看下README.md这个文件的内容

add test

发现工作区(working directory)的内容恢复为刚才git add到暂存区(staging area)里的内容了。

2.撤销暂存区修改示例

2.代码已经添加到了暂存区(staging area),但还没提交(git commit)到本地仓库(local repository)里。执行git reset HEAD <file>后,会撤销暂存区(staging area)的修改。这个时候暂存区(staging area)是处于当前版本的状态,而工作区(working directory)是处于有修改的状态。

假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

add test

使用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:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

使用命令git add README.md将文件添加进暂存区(staging area)里。

使用git status命令查看一下

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#

已经将修改添加进了暂存区(staging area)里。

这个时候执行git reset HEAD README.md来撤销暂存区(staging area)的修改。

这个时候会显示

Unstaged changes after reset:
M       README.md

然后再用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:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

使用cat README.md查看下README.md这个文件的内容

add test

发现工作区(working directory)的内容还是保持与之前修改后一样的状态。

3.撤销本地仓库修改示例1

3.代码已经提交(git commit)到本地仓库(local repository)里,但还没推送到远程仓库(remote repository)里。如果只想回退本地仓库(local repository)的内容,执行git reset --soft HEAD^。执行后,本地仓库(local repository)回退到上一版本,暂存区(staging area)与工作区(working directory)保留不变。

假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

commit test

使用命令git add README.md将文件添加进暂存区(staging area)里。

使用命令git commit -m "commit test"将文件提交到本地仓库(local repository)里。

使用命令git log可以查看提交的日志

commit cabd632eb1bc51abd8ffb2841bf2f19d2d70fcae
Author: root <root@Linux-host.(none)>
Date:   Mon Jan 18 22:13:12 2021 +0800

    commit test

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候执行git reset --soft HEAD^来回退版本。

然后再用命令git log可以查看提交的日志,发现版本回退了

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候使用git status命令查看一下,发现修改还存在于暂存区(staging area)。

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#

使用cat README.md查看下README.md这个文件的内容

commit test

发现工作区(working directory)的内容还是保持与之前修改后一样的状态。

4.撤销本地仓库修改示例2

4.代码已经提交(git commit)到本地仓库(local repository)里,但还没推送到远程仓库(remote repository)里。如果想回退本地仓库(local repository)和暂存区(staging area),执行git reset HEAD^或者git reset --mixed HEAD^。执行后,本地仓库(local repository)回退到上一版本,清空暂存区(staging area),但是工作区(working directory)保留不变。

假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

commit test

使用命令git add README.md将文件添加进暂存区(staging area)里。

使用命令git commit -m "commit test"将文件提交到本地仓库(local repository)里。

使用命令git log可以查看提交的日志

commit 41d2532eb1bc51abd8ffb2841bf2f19db5d0fc32
Author: root <root@Linux-host.(none)>
Date:   Mon Jan 18 22:22:53 2021 +0800

    commit test

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候执行git reset HEAD^来回退版本。
显示

Unstaged changes after reset:
M       README.md

然后再用命令git log可以查看提交的日志,发现版本回退了

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候使用git status命令查看一下,发现暂存区(staging area)没有东西了,已被清空,但是工作区(working directory)有保持修改的状态。

# 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:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")

使用cat README.md查看下README.md这个文件的内容

commit test

发现工作区(working directory)的内容还是保持与之前修改后一样的状态。

5.撤销本地仓库修改示例3

5.代码已经提交(git commit)到本地仓库(local repository)里,但还没推送到远程仓库(remote repository)里。如果想回退所有内容到上一个版本,执行git reset --hard HEAD^。执行后,回退所有内容到上一个版本,将暂存区(staging area)与工作区(working directory)都回到上一次版本,所有信息提交都被删除。
假设目前仓库中有一个文件README.md,里面内容为

test

现在把内容修改为

commit test

使用命令git add README.md将文件添加进暂存区(staging area)里。

使用命令git commit -m "commit test"将文件提交到本地仓库(local repository)里。

使用命令git log可以查看提交的日志

commit e2d8532eb1bc51abd8ffb2841bf2f19db5d094a5
Author: root <root@Linux-host.(none)>
Date:   Mon Jan 18 22:30:44 2021 +0800

    commit test

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候执行git reset --hard HEAD^来回退版本。
显示

HEAD is now at 3dd9b7f README

然后再用命令git log可以查看提交的日志,发现版本回退了

commit 3dd9b7f84bb85568b01cded73d978172e670fcae
Author: root <root@Linux-host.(none)>
Date:   Sun Jan 18 21:58:59 2021 +0800

    git test

这个时候使用git status命令查看一下,发现暂存区(staging area)和工作区(working directory)都被清空了。

# On branch master
nothing to commit (working directory clean)

使用cat README.md查看下README.md这个文件的内容

test

发现工作区(working directory)的修改也都没有了,回到了上次提交版本的状态。


[参考资料]

git Documentation

撤销修改

Git Reset 三种模式

git checkout 检出命令


本文链接:https://blog.csdn.net/u012028275/article/details/113819520

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值