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

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


当前回答

我更喜欢用git rebase -i对于这份工作来说, 因为一份不错的清单 出现在我可以选择摆脱承诺的地方。 它可能不如这里的其他答案直接直接, 但是它只是...感觉右右.

选择要列出多少个承诺, 然后这样引用( 加入最后 3 个)

git rebase -i HEAD~3

样本列表

pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support

然后 Git 将删除任何您删除的线条 。

其他回答

你需要做简单快的动作

    git commit --amend

是私人分行还是私人分行

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

如果它是共享的或公共的分支。

假设您在本地犯下错误, 并将其推到远程仓库。 您可以解开这两个命令的混乱 :

首先,我们需要纠正我们的地方储存库,回到我们所希望的承诺上来:

git reset --hard <previous good commit id where you want the local repository  to go>

现在,我们通过使用此命令,在远程仓库大力推动这项良好承诺:

git push --force-with-lease

强制选项的“ 租赁” 版本将防止意外删除您不知道的新承诺( 即上次拉动后来自其它来源 ) 。

更改上次承诺

替换索引中的文件 :

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 revert <commit-id>.

要拿到身份证明,请使用git log.

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