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

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


当前回答

使用 git 返回 < commit- id > 。

要拿到身份证明,请使用 git 日志。

其他回答

替换您的本地版本, 包括您对服务器版本的更改。 这两行代码将迫使 Git 调用并覆盖本地版 。

打开命令提示并浏览到 Git 工程根。 如果您使用视觉工作室, 请单击团队、 同步并点击下面的“ 打开命令提示 ” (见图像) 。

一旦进入CMD, 继续使用以下两个指示。

git fetch --all

那你就做

git reset --hard origin/master

这将覆盖 Git 服务器上的现有本地版本 。

取决于您是否已经公开了您的前一次承诺( 插入到您的远程仓库 ) :

如何撤销本地任务

比方说我承诺了当地, 但现在我想取消那个承诺。

git log
    commit 101: bad commit    # Latest commit. This would be called 'HEAD'.
    commit 100: good commit   # Second to last commit. This is the one we want.

要让一切恢复到上次承诺之前的状态 我们需要重置承诺 重置承诺

git reset --soft HEAD^     # Use --soft if you want to keep your changes
git reset --hard HEAD^     # Use --hard if you don't care about keeping the changes you made

现在 git 日志将显示我们最后的承诺已被删除 。

如何撤销公开承诺

如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 HEAD) 所做的更改 。

git revert HEAD

你们的更改将恢复,并准备好你们承诺:

git commit -m 'restoring the file I removed by accident'
git log
    commit 102: restoring the file I removed by accident
    commit 101: removing a file we don't need
    commit 100: adding a file that we need

更多信息,请查看 Git Basics - 撤销一些事情 。

我很久以前也曾写过这些话,

如何删除/ 反转 Git 承诺

基本上,你只需要做:

git 日志, 获取 SHA hash 的前七个字符, 然后进行 git return < sha> , 然后再进行 git push -- force 。

您也可以使用 Git 返回命令, 恢复此命令如下: git 返回 < sha> - m - 1, 然后按 git 键 。

如果存储库是在当地承诺的,但尚未被推到服务器上,那么另一种粗糙/佣人解决它的方法将是:

Git 在另一个位置克隆仓库。 将修改( 文件/ 目录) 从原始仓库复制到这个新的仓库。 然后承诺并按下新仓库的修改。 用这个新的仓库替换旧的仓库 。

git revert commit

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

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