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

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


当前回答

在回答之前,我们补充一些背景背景,解释一下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 to 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并且,还有。

enter image description here


git revert <sha-1>

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

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

为了得到身份证明 只需使用git log.

如何修正上一个本地承诺

使用 git- gui (或类似) 执行git commit --amend。从 GUI 中,您可以从承诺中添加或删除单个文件。您也可以修改承诺信件。

如何撤销上一个本地任务

仅将分支重新设置为上一个位置(例如,使用gitkgit rebase) 。然后从保存的副本中重新应用您的更改。在您本地仓库的垃圾回收收集后,它会像没有发生不想要的操作一样。要用一个命令来完成所有这些,请使用git reset HEAD~1.

警告单词: 粗心使用git reset我建议基特新手们尽可能避免这样做。

如何撤销公开承诺

表演a逆向樱桃摘 (git- rever 反转)来撤销更改。

如果你还没有在你的分支上做其他的修改, 你可以简单地做。 。 。

git revert --no-edit HEAD

然后将更新的分支推至共享仓库 。

承诺历史将分别显示两个承诺.


高级:校正私立公共存储库分支

这可能很危险 -- -- 请确定您有该分支的本地副本要反射 。

备注:如果有其他人在为分支工作,您不想这样做。

git push --delete (branch_name) ## remove public version of branch

清理你的分行 在当地,然后冲...

git push origin (branch_name)

通常情况下,你可能不必担心你的私人部门 将历史变成原始历史。只是催促后续承诺(见上文“如何撤销公共承诺”),然后,做一个壁球- 合并隐藏历史。

你需要做简单快的动作

    git commit --amend

是私人分行还是私人分行

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

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

只需撤销最后一项承诺 :

git reset --soft HEAD~

或撤销上次承诺前的时间 :

git reset --soft HEAD~2

或撤销先前的任何承诺 :

git reset --soft <commitID>

(您可以使用git reflog)

当您撤销上一个任务时, 记住要用

git clean

更多信息,请查看以下文件:git- reseet 重置

这将添加一个新的承诺, 删除所添加的文件 。

git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"

或者您可以重写历史来撤销最后一项承诺 。

警告警告警告警告:此命令将永久删除对《京都议定书》的修改.java您承诺的文件( 和任何其他文件) -- 从工作目录中删除全部修改 :

git reset --hard HEAD~1

缩略hard resetHEAD-1您的工作副本将在您错误犯罪之前设定为犯罪状态 。