如何撤消“git commit –amend”而不是“git commit”
我不小心修改了我以前的提交。 提交应该是分开的,以保持对特定文件所做的更改的历史logging。
有没有办法撤消最后一次提交? 如果我做了像git reset --hard HEAD^ ,第一个提交也是撤消。 
(我还没有推到任何远程目录)
 你需要做的是创build一个与当前HEAD提交相同的细节的新提交,但是将父项作为HEAD的前一个版本。  git reset --soft将移动分支指针,以便下一个提交发生在当前分支头所在的不同提交之上。 
 # Move the current head so that it's pointing at the old commit # Leave the index intact for redoing the commit. # HEAD@{1} gives you "the commit that HEAD pointed at before # it was moved to where it currently points at". Note that this is # different from HEAD~1, which gives you "the commit that is the # parent node of the commit that HEAD is currently pointing to." git reset --soft HEAD@{1} # commit the current tree using the commit details of the previous # HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the # previous command. It's now pointing at the erroneously amended commit.) git commit -C HEAD@{1} 
使用ref-log :
 git branch fixing-things HEAD@{1} git reset fixing-things 
那么您应该只在您的工作副本中拥有您之前修改的所有更改,然后再次提交
 查看以前的索引typesgit reflog的完整列表 
您可以随时分割提交,从手册
- 用git rebase -i commit ^开始一个交互式rebase,其中commit是你想要分割的提交。 实际上,只要包含该提交,任何提交范围都会执行。
- 使用“编辑”操作标记要分割的提交。
- 当编辑提交时,执行git reset HEAD ^。 其效果是HEAD被倒回一个,索引也是如此。 但是,工作树保持不变。
- 现在将更改添加到您想要在第一次提交中使用的索引中。 你可以使用git add(可能交互式地)或者git-gui(或者两者)来做到这一点。
- 用现在提交的任何提交消息来提交当前的索引。
- 重复最后两个步骤,直到你的工作树干净。
- 用git rebase继续rebase – 继续。
通过以下方式查找修改的提交:
 git log --reflog 
  注意:您可以添加--patch来查看提交的主体以确保清晰。  和git reflog 。 
然后重置你的HEAD到以前提交的任何地方:
 git reset SHA1 --hard 
注意:用你的真实提交哈希代替 SHA1。 另外请注意,这个命令将会丢失任何未提交的更改,所以您可能会隐藏它们。
然后樱桃挑选你需要的其他提交:
 git cherry-pick SHA1 
 也许可以使用git reflog在修改之前和修改之前得到两个提交。 
 然后用git diff before_commit_id after_commit_id > d.diff得到修改前和修改后的差异。 
 接下来使用git checkout before_commit_id回到提交之前 
 最后使用git apply d.diff来应用你所做的真正改变。 
这解决了我的问题。
 可能值得注意的是,如果你仍然在编辑器中提交消息,你可以删除提交消息,它会中止git commit --amend命令。 
- 
使用上次提交签出到临时分支 git branch temp HEAD@{1}
- 
重置上次提交 git reset temp
- 
现在,您将拥有所有文件以及之前的提交。 检查所有文件的状态。 git status
- 
从git阶段重置您的提交文件。 git reset myfile1.js(如此)
- 
重新提交这个提交 git commit -C HEAD@{1}
- 
添加并提交你的文件到新的提交。