我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。

我如何从当地存储库撤销这些承诺?


当前回答

git revert commit

这将产生与您想要返回的承诺相反的变化, 然后再进行这种改变。 我认为这是最简单的方式 。

http://git-scm.com/docs/git-revert https://git-scm.com/docs/git-revert

其他回答

作为以下步骤。

步骤1

Hit git 日志

从日志列表中找到最后的硬化散列代码, 然后输入 :

步骤2

git reset <hash code>

撤销上次承诺 :

git reset --soft HEAD^ or git reset --soft HEAD~

这将撤销最后一项承诺 。

在这里 -- 软意味着重置中。

HEAD~ 或 HEAD_ 意思是移动 在 HEAD 之前承诺。

替换上次承诺的新承诺:

git commit --amend -m "message"

它将以新承诺取代最后一项承诺 。

如果您只是想要丢弃所有本地更改/ 承诺, 使您的本地分支看起来像您从... 开始的源分支...

git reset --hard origin/branch-name

这里是网站: 哦,该死,混蛋!

这里有许多解密的秘方 如何在Git 中解决事情。其中一些是:

哦,该死,我需要改变 信息在我的最后一次承诺!

git commit --amend
# follow prompts to change the commit message

哦,我无意中把什么东西 给了主人 本来该在全新的树枝上!

# Create a new branch from the current state of master
git branch some-new-branch-name
# Remove the commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
# Your commit lives in this branch now :)

如果您想要撤销在回邮中的第一个承诺

你会遇到这个问题:

$ git reset HEAD~
fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

发生错误的原因是,如果最后一次承诺是存储处的初始承诺(或没有父/母),则不存在 HEAD~ 。

解决方案

如果您想要重置“ master” 分支上唯一的承诺

$ git update-ref -d HEAD
$ git rm --cached -r .