• git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog


    第二种情况 改变工作目录 ,又改变暂存区

    把ccc追加到a文件

    [root@ci-node1 git_test]# echo "ccc" >> a
    [root@ci-node1 git_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:   a
    
    no changes added to commit (use "git add" and/or "git commit -a")

    现在本地工作目录改变了 ,git add  提交到缓存区

    现在本地工作目录和缓存区是一致的,唯一不一致是和本地仓库有区别

    [root@ci-node1 git_test]# git add a
    [root@ci-node1 git_test]# git diff --cached a
    diff --git a/a b/a
    index 126f52b..26a3227 100644
    --- a/a
    +++ b/a
    @@ -1,2 +1,3 @@
     test
     bbb
    +ccc

    这时候可以拿本地仓库内容 覆盖暂存区,覆盖完暂存区,用暂存区覆盖本地工作目录

    仓库状态覆盖暂存区,使仓库和暂存区是一样的

    //撤销上一次的提交  
    git reset HEAD
    //撤销上一次的提交 
    [root@ci-node1 git_test]# git reset HEAD a
    Unstaged changes after reset:
    M    a
    // 查看工作区内容
    [root@ci-node1 git_test]# cat a
    test
    bbb
    ccc

    // 暂存区和本地工作目录不一致

    [root@ci-node1 git_test]# git diff a
    diff --git a/a b/a
    index 126f52b..26a3227 100644
    --- a/a
    +++ b/a
    @@ -1,2 +1,3 @@
     test
     bbb
    +ccc

    // 没有输出说明暂存区和本地仓库是一致的,

    把本地仓库状态覆盖到暂存区

    [root@ci-node1 git_test]# git diff --cached a
    [root@ci-node1 git_test]# 

    然后使用git checkout 把暂存区覆盖本地工作目录

    [root@ci-node1 git_test]# git checkout -- a
    [root@ci-node1 git_test]# git status
    On branch master
    nothing to commit, working tree clean

    第三种情况

    [root@ci-node1 git_test]# echo "ccc" >>a
    // 加到暂存区
    [root@ci-node1 git_test]# git add a
    [root@ci-node1 git_test]# cat a
    test
    bbb
    ccc

    现在我发现改错了 不应该加ccc和 bbb  ,要回退加上test的时候,跳过了bbb,跨commit

    回退到某一次commit

    git reset --hard 后面接要回到某一次commit 的唯一标识

    commit 标识不需要加那么多 能标识到唯一就行 复制5 6 个字母

    要通过 git log 查看提交日志,找到commit唯一标识,也可直接指定提交编号或序

    [root@ci-node1 git_test]# git log 
    commit ac31205c363e10a2860d836f22442a7ac8e6a6c0
    Author: mingerlcm <892287990@qq.com>
    Date:   Sat Mar 14 19:47:58 2020 +0800
    
        commit a
    
    commit 353666de3969c9423cdc11a8f1046d62d11ace2c
    Author: mingerlcm <892287990@qq.com>
    Date:   Sat Mar 14 19:43:18 2020 +0800
    
        a
    
    commit af5856a1ebc7f2242ea0ec2a130ee90047a13531
    Author: mingerlcm <892287990@qq.com>
    Date:   Mon Aug 26 21:12:31 2019 +0800
    
        modify a

    要回到第一次修改a文件时候

    // 加上要回到的commit id 标识 ,要跳到哪次commit 就加上哪次commit id
    [root@ci-node1 git_test]# git reset --hard af5856a1ebc
    HEAD is now at af5856a modify a
    
    // 回到刚开始加test的时候
    [root@ci-node1 git_test]# cat a
    test
    [root@ci-node1 git_test]# git status
    On branch master
    nothing to commit, working tree clean

    再看看git log

    之前加上bbb ,ccc的commit id  log没有了

    [root@ci-node1 git_test]# git log
    commit af5856a1ebc7f2242ea0ec2a130ee90047a13531
    Author: mingerlcm <892287990@qq.com>
    Date:   Mon Aug 26 21:12:31 2019 +0800
    
        modify a
    
    commit 52e89813dff7c9e1261a6bdc94f284705cb8b4fb
    Author: mingerlcm <892287990@qq.com>
    Date:   Mon Aug 26 17:55:44 2019 +0800
    
        rename a.txt to a
    
    commit cc8bd8044be923aae7d44641fb99d25f574db8a5
    Author: mingerlcm <892287990@qq.com>
    Date:   Mon Aug 26 17:49:50 2019 +0800
    
        rename a to a.txt
    
    commit 73d723093b88edda997936aa1fa7cc5ff1175e98

    如果有这种需求,我们回退操作完,发现这个回退不是我们需要的,跳错了

    原来想跳到加上bbb 那一次commit,跳到了加上test 那一次commit了

    发现 git log 记录 没有那一次commit 没有那一次id

    解决:看之前执行历史, 使用git reflog

    查看本地历史操作 git reflog

    git log 仅能查看终在仓库存在的提交信息,无法查看被删除的提交,以及在本地具体 Git 命令操作记录,这时候你需要使用 git reflog。

    git reflog 可以查看所有在仓库所有做过commit历史记录

    记录了每一次操作,这样就可以找到commit id

    [root@ci-node1 git_test]# git reflog
    af5856a HEAD@{0}: reset: moving to af5856a1ebc
    ac31205 HEAD@{1}: commit: commit a
    353666d HEAD@{2}: commit: a
    af5856a HEAD@{3}: commit: modify a
    52e8981 HEAD@{4}: commit: rename a.txt to a
    cc8bd80 HEAD@{5}: commit: rename a to a.txt
    73d7230 HEAD@{6}: commit (initial): commit a

    现在想回到 加上bbb那一次commit

    [root@ci-node1 git_test]# git reset --hard ac31205
    HEAD is now at ac31205 commit a
    [root@ci-node1 git_test]# cat a
    test
    bbb

    git log 只列出当前仓库状态提交自己之前commit ,但是回退到某一天点之后记录是没有了

    git log 列出仓库到当前状态为止的所有日志

    git reflog 列出所有历史操作的commit id , 每一次历史操作可以查到要去的commit  配合git reset --hard  commit id使用

    git 的撤销 和回退

  • 相关阅读:
    使用aws和tomcat搭建服务器过程中的一些坑.
    10
    9
    8
    7
    6
    5
    hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
    hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
    hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/12495491.html
Copyright © 2020-2023  润新知