一.git commit —amend 可以push前更正最后一个commit

举例:

  1. git add part-of-the-fix.js
  2. git commit -m "fxed #11"

修改”fxed #11”

  1. git add source-should-have-been-committed.js
  2. git commit --amend -m "fixed #11"

使用场景:
commit log输入了错别字,想改掉;发现漏提交了一个文件
git log 查看git的commit记录

二.Git undo 从历史上删除某个文件

使用场景:
1.发现仓库里提交了不该出现的密码、密钥等敏感信息;
2.仓库里提交了不合适的文件,如VS生成的Debug/、Release/等编译输出.再添加了.gitignore条目后,还需要抹去历史上的痕迹;
3.上传了巨大的二进制文件,它们不应该是repo的一部分.
https://rtyley.github.io/bfg-repo-cleaner/借助这个工具来使用

  1. git checkout -- file #将file重置到最近一次提交的状态
  2. git reset --soft HEAD~1 #撤销最近一次commit,将其变更内容回退到staging(即会被保留)
  3. git reset --HARD HEAD~1 #撤销最近一次commit,放弃其变更内容
  4. git reset HEAD file #从staging移除一个文件,将file退回到working directory
  5. git add . #批量添加
  6. git revert commit_hash #撤销某个commit的变更
  7. git stash #做了一半,突然接到bug report,需要紧急修复.
  8. git stash pop #解决完bug,找回刚才的本地变更