• [转]Git 撤销操作


    二. Git撤消操作

    12.1 修改最后一次提交 git commit --amend

    1.新建一个文件 2.提交一个之前的更改

    3.跟踪这个文件 4.跟前一次一起提交

    提示你是否重新编辑提交说明,如果不编辑退出后还是跟之前一样提交

    commit 成功

    或 git commit -m “” 可以直接提交

    12.2 撤消已暂存的文件 git reset HEAD

    #新建两个文件
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ touch 1txt
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ touch 2txt
    #全部暂存
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git add -A
    #查看文件状态
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #     new file:   1txt
    #     new file:   2txt
    # 
    
    
    #取消暂存 1txt
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git reset HEAD 1txt
    #再次查看文件状态,1txt 已经被取消啦
    
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #     new file:   2txt
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #     1txt 
    
    
    

    12.3 撤消对文件的修改 git checkout -- <file>

    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ ls
    README          TEST            android-package ios-package     testamend
    #修改文件testmend
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ vim testamend
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    # 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:   testamend
    #
    no changes added to commit (use "git add" and/or "git commit -a”)
    #撤消文件的修改
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git checkout -- testamend
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    nothing to commit, working directory clean 
    

    12.4 Git撤消commit

    1. git log查看日志,找到需要回退的那次commit的 哈希值

    2. git reset --hard commit_id

    12.5 Git版本回退

    #查看log
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git log
    commit 047cd2d2f6bd1ecdcdb4854b728300aeaa314b80
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Thu Jan 2 22:26:50 2014 +0800
    
    
        1test
    
    
    commit fa7fd8d49f3789d39aa3cc52cd81e09e6d061719
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Tue Dec 31 13:29:45 2013 +0800
    
    
        delete test2
    
    
    commit 746f92258e2bc65c46f77f37315f577091192885
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Tue Dec 31 13:22:07 2013 +0800
    
    
        test git commit -a 
    
    …..
    
    

    HEAD是指向最新的提交,上一次提交是HEAD^,上上次是HEAD^^,也可以写成HEAD~2 ,依次类推

    #放弃本地所有修改,回退到上一个版本
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git reset --hard HEAD^
    HEAD is now at fa7fd8d delete test2
    

    注: --hard 表示放弃所有本地改动

    #再次查看log
    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git log
    commit fa7fd8d49f3789d39aa3cc52cd81e09e6d061719
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Tue Dec 31 13:29:45 2013 +0800
    
    
        delete test2
    
    
    commit 746f92258e2bc65c46f77f37315f577091192885
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Tue Dec 31 13:22:07 2013 +0800
    
    
        test git commit -a
    
    
    commit e301c4e185b0937d1ce9484ea86ab401e95c976c
    Author: 小朋 <xiaopeng.bxp@****.com>
    Date:   Tue Dec 31 13:14:42 2013 +0800
    
    
        just test 
    
    ………..
    
    

    回退到指定的版本 git reset --hard <哈希值>

    bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git reset --hard 746f92258e2bc65c46f77f37315f577091192885
    HEAD is now at 746f922 test git commit -a 
    

    12.6 撤消未跟踪文件 git clean -dxf

    「举个例子」

    清除所有未跟踪文件,包括纳入ignored的文件。如果要保留ignored的文件修改,使用参数-df

    #清除所有未跟踪文件,包括纳入ignored的文件
    bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~/workspace2/spark$ git clean -dxf
    正删除 .idea/
    正删除 .package.sh.swp
    正删除 assets/sparklog/
    正删除 backup-config/AndroidManifest.xml
    正删除 backup-config/channel_config.xml
    正删除 backup-res/assets/
    正删除 backup-res/res/
    正删除 debug
    正删除 package/spark_2.4_L95_91zhuomian.apk
    正删除 res/values/channel_config.xmlg
    正删除 target/
    正删除 xiamimusic.iml
    #再查看一下未跟踪的文件已经被撤消了
    bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~/workspace2/spark$ git status
    # 位于分支 test
    # 尚未暂存以备提交的变更:
    # (使用 "git add <file>..." 更新要提交的内容)
    # (使用 "git checkout -- <file>..." 丢弃工作区的改动)
    #
    # 修改: pom.xml
    #
    修改尚未加入提交(使用 "git add" 和/或 "git commit -a")`
  • 相关阅读:
    POJ 3695 Rectangles
    POJ 2002 Squares
    linux 查看磁盘空间大小
    keymaster 快捷键管理器
    Pxloader
    as3数据类型检查写法(is/as/typeof/in/instanceof)用法介绍
    javascript的dom选择器
    javascript音频管理方案:SoundManager2
    Morris.js – 画折线图、面积图和环状图的统计图表库
    jsuri 让你方便的处理url
  • 原文地址:https://www.cnblogs.com/hilaryqs/p/9212292.html
Copyright © 2020-2023  润新知