回滚提交

撤销提交

  • 撤消上次提交,将所有内容放回暂存区域:

    git reset --soft HEAD^
    
  • 添加文件并更改消息:

    git commit --amend -m "New Message"
    
  • 撤消最后的提交并删除更改:

    git reset --hard HEAD^
    
  • 与上一个相同,但是撤销两次提交:

    git reset --hard HEAD^^
    

推送后请勿重置(reset)

重置工作流

  1. 再次编辑文件 edit_this_file.rb
  2. 查看状态
  3. 添加并提交错误信息
  4. 查看日志
  5. 修改提交
  6. 查看日志
  7. 软复位
  8. 查看日志
  9. 拉取更新
  10. 推送更改

命令

# Change file edit_this_file.rb
git status
git commit -am "kjkfjkg"
git log
git commit --amend -m "New comment added"
git log
git reset --soft HEAD^
git log
git pull origin master
git push origin master

注意

  • git revertgit reset 的区别
  • 重置删除提交,将恢复删除更改但保留提交
  • 支持把还原撤销,还原变得更安全
# Changed file
git commit -am "bug introduced"
git revert HEAD
# New commit created reverting changes
# Now we want to re apply the reverted commit
git log # take hash from the revert commit
git revert <rev commit hash>
# reverted commit is back (new commit created again)