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

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


当前回答

如果您想要永久撤销它, 您克隆了一些仓库 。

犯罪身份可通过以下方式识别:

git log 

然后你们可以做:

git reset --hard <commit_id>

git push origin <branch_name> -f

其他回答

为了完整起见,我将提出前几个答复所忽略的一个明显明显的方法。

由于承诺没有被推,遥控器没有改变,因此:

  1. 删除本地仓库 。
  2. 克隆远程仓库 。

有时候,如果你的高贵的Git客户告别(看着你,伊吉特),这有时是必要的。

别忘了重新承诺保存已保存上次按键后的变化 。

enter image description here

假设您在视觉工作室工作, 如果您进入分支历史, 并查看您的所有承诺, 只需在承诺之前选择事件, 您想要撤销, 右单击它, 然后选择Revert轻而易举

在回答之前,我们补充一些背景,解释一下这是什么HEAD.

First of all what is HEAD?

HEAD仅指当前分支的当前承诺(最新承诺)。
只有一个,只有一个,只有一个HEAD在任何给定时间。 (不包括git worktree)

内容的内容HEAD存储在内部.git/HEAD并包含当前承诺的 40 字节 SHA-1 。


detached HEAD

如果你们不履行最近的承诺,那末,HEAD指在历史中先前承诺detached HEAD.

enter image description here

在命令线上,它会看起来像这个——SHA-1,而不是自HEAD不指向当前分支的一端

enter image description here

enter image description here

如何从独立的总部中恢复的几种选择:


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

这将检查指向想要的承诺的新分支 。
此命令将检出给给定的承诺 。
此时此刻,您可以创建一个分支,并从此开始工作。

# Checkout a given commit. 
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# create a new branch forked to the given commit
git checkout -b <branch name>

git reflog

你可以随时使用reflog并且,还有。
git reflog将显示更新HEAD并检查想要的 reflog 条目将设置HEAD返回到此任务。

总部总部每次修改时,将有一个新的条目。reflog

git reflog
git checkout HEAD@{...}

这样你就可以回到你想要的事业了

enter image description here


git reset --hard <commit_id>

"移动"你的总部 回到想要的承诺。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
  • 注:(a)自Git 2.7以来)
    您也可以使用git rebase --no-autostash并且,还有。

git revert <sha-1>

“ 撤消” 指定的承诺或承诺范围 。
重置命令将“ 撤消” 对给定任务所做的任何更改 。
将使用解析补丁进行新承诺, 而原始承诺也将保留在历史中 。

# add new commit with the undo of the original one.
# the <sha-1> can be any commit(s) or commit range
git revert <sha-1>

这个计划说明哪个指挥所做什么。
如你所见reset && checkout修改HEAD.

enter image description here

添加/删除文件, 以便按照您想要的方式获取文件 :

git rm classdir
git add sourcedir

然后修正承诺:

git commit --amend

之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样

请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。

为了消除(所有变化)最后承诺、最后2承诺和最后n承诺:

git reset --hard HEAD~1
git reset --hard HEAD~2
...
git reset --hard HEAD~n

在做出具体承诺后,要消除任何东西:

git reset --hard <commit sha>

例如,

git reset --hard 0d12345

谨慎使用硬选项 : 它会同时删除您回购的本地更改, 并返回到上一个提及的承诺 。 您只应该运行此选项, 前提是您确定您在最后一次承诺中搞砸了, 并且想要回到过去 。

作为副注释,大约7个承诺的散列字母就足够了,但在更大的项目中,你可能需要最多12个字母才能独有。如果您愿意,也可以使用整个承诺的 SHA 。

上述命令在GitHub也为Windows服务。