当前位置:   article > 正文

Git入门指南八:Git撤消操作_git push" to publish your local commits

git push" to publish your local commits

十二. Git撤消操作

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

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

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

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

commit 成功

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

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

  1. #新建两个文件
  2. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ touch 1txt
  3. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ touch 2txt
  4. #全部暂存
  5. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git add -A
  6. #查看文件状态
  7. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
  8. # On branch master
  9. # Your branch is ahead of 'origin/master' by 1 commit.
  10. # (use "git push" to publish your local commits)
  11. #
  12. # Changes to be committed:
  13. # (use "git reset HEAD <file>..." to unstage)
  14. #
  15. # new file: 1txt
  16. # new file: 2txt
  17. #
  18. #取消暂存 1txt
  19. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git reset HEAD 1txt
  20. #再次查看文件状态,1txt 已经被取消啦
  21. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
  22. # On branch master
  23. # Your branch is ahead of 'origin/master' by 1 commit.
  24. # (use "git push" to publish your local commits)
  25. #
  26. # Changes to be committed:
  27. # (use "git reset HEAD <file>..." to unstage)
  28. #
  29. # new file: 2txt
  30. #
  31. # Untracked files:
  32. # (use "git add <file>..." to include in what will be committed)
  33. #
  34. # 1txt

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

  1. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ ls
  2. README TEST android-package ios-package testamend
  3. #修改文件testmend
  4. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ vim testamend
  5. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
  6. # On branch master
  7. # Your branch is ahead of 'origin/master' by 1 commit.
  8. # (use "git push" to publish your local commits)
  9. #
  10. # Changes not staged for commit:
  11. # (use "git add <file>..." to update what will be committed)
  12. # (use "git checkout -- <file>..." to discard changes in working directory)
  13. #
  14. # modified: testamend
  15. #
  16. no changes added to commit (use "git add" and/or "git commit -a”)
  17. #撤消文件的修改
  18. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git checkout -- testamend
  19. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git status
  20. # On branch master
  21. # Your branch is ahead of 'origin/master' by 1 commit.
  22. # (use "git push" to publish your local commits)
  23. #
  24. nothing to commit, working directory clean

12.4 Git撤消commit

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

2. git reset --hard commit_id

12.5 Git版本回退

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

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

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

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

  1. #再次查看log
  2. bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git log
  3. commit fa7fd8d49f3789d39aa3cc52cd81e09e6d061719
  4. Author: 小朋 <xiaopeng.bxp@****.com>
  5. Date: Tue Dec 31 13:29:45 2013 +0800
  6. delete test2
  7. commit 746f92258e2bc65c46f77f37315f577091192885
  8. Author: 小朋 <xiaopeng.bxp@****.com>
  9. Date: Tue Dec 31 13:22:07 2013 +0800
  10. test git commit -a
  11. commit e301c4e185b0937d1ce9484ea86ab401e95c976c
  12. Author: 小朋 <xiaopeng.bxp@****.com>
  13. Date: Tue Dec 31 13:14:42 2013 +0800
  14. just test
  15. ………..

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

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

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

「举个例子」

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

  1. #清除所有未跟踪文件,包括纳入ignored的文件
  2. bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~/workspace2/spark$ git clean -dxf
  3. 正删除 .idea/
  4. 正删除 .package.sh.swp
  5. 正删除 assets/sparklog/
  6. 正删除 backup-config/AndroidManifest.xml
  7. 正删除 backup-config/channel_config.xml
  8. 正删除 backup-res/assets/
  9. 正删除 backup-res/res/
  10. 正删除 debug
  11. 正删除 package/spark_2.4_L95_91zhuomian.apk
  12. 正删除 res/values/channel_config.xmlg
  13. 正删除 target/
  14. 正删除 xiamimusic.iml
  15. #再查看一下未跟踪的文件已经被撤消了
  16. bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~/workspace2/spark$ git status
  17. # 位于分支 test
  18. # 尚未暂存以备提交的变更:
  19. # (使用 "git add <file>..." 更新要提交的内容)
  20. # (使用 "git checkout -- <file>..." 丢弃工作区的改动)
  21. #
  22. # 修改: pom.xml
  23. #
  24. 修改尚未加入提交(使用 "git add"/"git commit -a")`

订阅

微信搜索“毕小烦”或者扫描下面的二维码,即可订阅我的文章。

image.png

如果文章对你有帮助,请随手点个赞吧!

(完)

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/644968
推荐阅读
相关标签
  

闽ICP备14008679号