[转]Git 撤销操作

Posted hilaryqs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]Git 撤销操作相关的知识,希望对你有一定的参考价值。

二. Git撤消操作

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

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

技术分享图片

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

技术分享图片

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

技术分享图片

技术分享图片

commit 成功

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

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

#新建两个文件
[email protected] wirelessqa$ touch 1txt
[email protected] wirelessqa$ touch 2txt
#全部暂存
[email protected] wirelessqa$ git add -A
#查看文件状态
[email protected] 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
[email protected] wirelessqa$ git reset HEAD 1txt
#再次查看文件状态,1txt 已经被取消啦

[email protected] 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>

[email protected] wirelessqa$ ls
README          TEST            android-package ios-package     testamend
#修改文件testmend
[email protected] wirelessqa$ vim testamend
[email protected] 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”)
#撤消文件的修改
[email protected] wirelessqa$ git checkout -- testamend
[email protected] 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
[email protected] wirelessqa$ git log
commit 047cd2d2f6bd1ecdcdb4854b728300aeaa314b80
Author: 小朋 <[email protected]****.com>
Date:   Thu Jan 2 22:26:50 2014 +0800


    1test


commit fa7fd8d49f3789d39aa3cc52cd81e09e6d061719
Author: 小朋 <[email protected]****.com>
Date:   Tue Dec 31 13:29:45 2013 +0800


    delete test2


commit 746f92258e2bc65c46f77f37315f577091192885
Author: 小朋 <[email protected]****.com>
Date:   Tue Dec 31 13:22:07 2013 +0800


    test git commit -a 

…..

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

#放弃本地所有修改,回退到上一个版本
[email protected] wirelessqa$ git reset --hard HEAD^
HEAD is now at fa7fd8d delete test2

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

#再次查看log
[email protected] wirelessqa$ git log
commit fa7fd8d49f3789d39aa3cc52cd81e09e6d061719
Author: 小朋 <[email protected]****.com>
Date:   Tue Dec 31 13:29:45 2013 +0800


    delete test2


commit 746f92258e2bc65c46f77f37315f577091192885
Author: 小朋 <[email protected]****.com>
Date:   Tue Dec 31 13:22:07 2013 +0800


    test git commit -a


commit e301c4e185b0937d1ce9484ea86ab401e95c976c
Author: 小朋 <[email protected]****.com>
Date:   Tue Dec 31 13:14:42 2013 +0800


    just test 

………..

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

[email protected] wirelessqa$ git reset --hard 746f92258e2bc65c46f77f37315f577091192885
HEAD is now at 746f922 test git commit -a 

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

「举个例子」

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

#清除所有未跟踪文件,包括纳入ignored的文件
[email protected]:~/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
#再查看一下未跟踪的文件已经被撤消了
[email protected]:~/workspace2/spark$ git status
# 位于分支 test
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: pom.xml
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")`

以上是关于[转]Git 撤销操作的主要内容,如果未能解决你的问题,请参考以下文章

转如何在Git中撤销一切

转!!git如何撤销上一次commit(或已push)

如何在git里撤销任何操作

git上怎么把commit的代码撤销

git操作——git pull 撤销误操作,恢复本地代码

git commit 之后,撤销commit操作