我无意中把错误的文件给了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 重置 -- s软 < your_ last_ good_commit_hash_ here> 在这里, 如果您做 Git 状态, 您可以在集结区看到文件。 您可以选择错误的文件, 从集结区拿下来 。

喜欢下面。

git 重置错误的File1 错误的File2 错误的File3

您现在可以添加您需要按键的文件,

添加好文件1 好文件2 git 添加好文件1 goodFile2

提交提交它们

git 承诺 -v 或 git 承诺 - am "Message"

推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推

git 推进源源主机

但是,如果您不关心已更改的文件, 您可以硬重置到先前的良好承诺, 并将所有文件都推到服务器 。

git reset --hard <your_last_good_commit_hash_here>

git 推进源源主机

如果您已经向服务器发布错误的文件, 您可以使用 -- 强制旗将文件推到服务器并编辑历史 。

git 推推 -- 原力硕士

撤消一次承诺重做( R)

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)

git 重新设置是撤销命令的责任。 它会解除您最后的承诺, 同时将您的工作树( 您在磁盘上的文件状态) 保留下来。 您需要重新添加它们才能再次执行 。 对工作树文件进行更正 。 git 添加您想要包含在您新承诺中的任何内容 。 提交修改, 重用旧承诺信件 。 重置旧头部复制到. git/ ORIG_ HEAD; 与 - c ORIG_ HEAD 承诺将打开编辑器, 该编辑器最初包含旧承诺的日志信息, 并允许您编辑它 。 如果您不需要编辑信件, 您可以使用 - C 选项 。

或者,为了编辑上一个承诺(或仅仅是其承诺信息),承诺 -- -- 修正将在当前索引中将修改添加到上一个承诺。

要删除( 不回覆) 被推进到服务器的承诺, 有必要重写历史, 使用 Git 推源主 -- force [- with- level] 重写历史。 使用 -- force; 偏好 -- force- with- level, 几乎总是一个坏主意。 正如 Git 手册中所指出的 :

如果你[重写历史]已经发表,你应该了解重写历史的意义。


继续阅读

您可以使用 git reflog 来确定您想要返回的 SHA-1 承诺的 SHA-1 。 一旦您拥有此值, 请使用上文解释的命令序列 。


HEAD~ 和 HEAD~ 1 相同。 如果您想要解开多次承诺, 文章“ HEAD ” 是否有帮助 。

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

这里有许多解密的秘方 如何在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 push --delete (branch_name) //this will be removing the public version of your branch

git push origin (branch_name) //This will add the previous version back
#1) $ git commit -m "Something terribly misguided" 
#2) $ git reset HEAD~                              

[必要时编辑文件]

 #3) $ git add .
#4) $ git commit -c ORIG_HEAD