我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。

我如何解除那些承诺?当地当地仓库?


当前回答

每个人都以如此复杂的方式发表意见。

如果您想要从分支中删除最后一项承诺, 最简单的方法是:

git reset --hard HEAD~1

现在,为了摆脱你最后的承诺,你必须

git push --force

这就对了,这会消除你最后的承诺

其他回答

使用使用源树树(Git 的图形工具) 查看您的承诺和树。 您可以单击右键手动重置它 。

我发现这笔该网站描述如何撤销您在存储库中的承诺 。

一些命令 :

git commit --amend        # Change last commit
git reset HEAD~1 --soft   # Undo last commit

更改上次承诺

替换索引中的文件 :

git rm --cached *.class
git add *.java

那么,如果它是私人分支,修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正承诺:

git commit --amend

或者,如果它是共享分支, 做出新的承诺:

git commit -m 'Replace .class files with .java files'

(更改上一个承诺,使用真棒交互式重新基数.)


ProTipTM: 添加*.class至 a高光度( gitignore)阻止这种情况再次发生。


收回承诺

修改承诺是理想的解决办法,如果需要改变最后的承诺,但更普遍的解决办法是:reset.

您可以将 Git 重置为任何承诺 :

git reset @~N

何处处N之前承诺的数HEAD, 和@~重发到上一个任务 。

您不必修改承诺,而是可以使用:

git reset @~
git add *.java
git commit -m "Add .java files"

退 退 退git help reset,特别是:--soft --mixed--hard为了更好地了解它的作用

reflog 格式

如果你搞砸了,你总是可以使用折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式

$ git reset @~
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~
2c52489 HEAD@{1}: commit: added some .class files
$ git reset 2c52489
... and you're back where you started

使用此命令

git checkout -b old-state 0d1d7fc32

你总是可以做一个git checkout <SHA code>前一版本的版本, 然后再次承诺使用新代码 。