【git】将未提交的代码恢复到上次commit的状态

本文详细介绍了如何使用Git撤销在工作区中进行的一系列修改,包括恢复被修改的文件到最近一次commit的状态,并删除未跟踪的新文件,以实现完全回退至commit前状态的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

昨天我把代码commit并push到了github上,然后做了一系列修改。现在我想把这一系列修改删掉,即恢复到commit的状态。

基本思路:

使用git diff 配合 git checkout – 文件名(带路径的)将每个改动文件的恢复。

解决步骤

1.首先查看仓库的状态:

显示我们在上次commit之后并未add 和 commit.

$git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   onlyofficeeditor/WebContent/index.html
        modified:   onlyofficeeditor/WebContent/paperList.html
        modified:   onlyofficeeditor/WebContent/scripts/myScript/onlineWriting.js
        modified:   onlyofficeeditor/WebContent/scripts/myScript/paperList.js
        deleted:    onlyofficeeditor/WebContent/scripts/myScript/uploadFile.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        onlyofficeeditor/WebContent/css/img/arrow-down.gif
        onlyofficeeditor/WebContent/css/jquery.editable-select.css
        onlyofficeeditor/build/classes/.gitignore

no changes added to commit (use "git add" and/or "git commit -a")
2. 使用git diff 查看工作区和版本库的最新版本的区别

参考:廖雪峰-git教程之撤销修改

$ git diff 
diff --git a/onlyofficeeditor/WebContent/index.html b/onlyofficeeditor/WebContent/index.html
index aa852b9..0a39d1d 100644
--- a/onlyofficeeditor/WebContent/index.html
+++ b/onlyofficeeditor/WebContent/index.html
@@ -7,8 +7,10 @@
 ...
diff --git a/onlyofficeeditor/WebContent/paperList.html b/onlyofficeeditor/WebContent/paperList.html
index 5fb3869..f442947 100644
--- a/onlyofficeeditor/WebContent/paperList.html
+++ b/onlyofficeeditor/WebContent/paperList.html
@@ -20,22 +20,22 @@
 ...
3.使用git check out – 文件名 丢弃工作区的修改,直到git diff的输出结果为空

该命令可以让文件回到最近一次git commit或git add时的状态。

$ git checkout -- onlyofficeeditor/WebContent/index.html
$ git checkout -- onlyofficeeditor/WebContent/paperList.html
4.使用git status查看当前工作区的状态(比如untracked的文件等)

可以看出我们昨天还添了一些文件。为了恢复,我们删掉这些文件。

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        onlyofficeeditor/WebContent/css/img/arrow-down.gif
        onlyofficeeditor/WebContent/css/jquery.editable-select.css
        onlyofficeeditor/build/classes/.gitignore

nothing added to commit but untracked files present (use "git add" to track)
5.最后检查一下

ok了。

12556@DESKTOP-F6GNNOQ MINGW64 /e/Workspaces/eclipse2018-09/github (master)
$ git diff

12556@DESKTOP-F6GNNOQ MINGW64 /e/Workspaces/eclipse2018-09/github (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        onlyofficeeditor/build/classes/.gitignore

nothing added to commit but untracked files present (use "git add" to track)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值