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

问题描述

昨天我把代码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)

    原文作者:phenix_alice
    原文地址: https://blog.csdn.net/diyinqian/article/details/84346271
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞