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

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


当前回答

使用此命令

git checkout -b old-state 0d1d7fc32

其他回答

您可以使用:

git reset HEAD@{1}

此命令将删除您没有 Git 日志的错误执行 。

撤消上次承诺 :

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

这将撤销最后一项承诺 。

--soft意思是重置为中位 。

HEAD~HEAD^意思是移动以在 HEAD 之前承诺 。


替换上次承诺的新承诺 :

git commit --amend -m "message"

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

撤销一项承诺是有点吓人,如果你不知道它是如何运作的。 但如果你理解的话,它其实很容易。我会告诉你4种不同的方式, 你可以解除一项承诺。

说你们有这个,C是你们的总部,(F)是你们档案的状态。

   (F)
A-B-C
    ↑
  master

备选1:git reset --hard

您想要销毁C国罪行,并抛弃任何未承诺的变更。你这样做:

git reset --hard HEAD~1

结果是:

 (F)
A-B
  ↑
master

现在B是总部,因为你用了--hard中,您的文件在承诺 B 时被重置为状态。

备选案文2:git reset

也许C不是灾难,只是有点不对劲取消承诺,但保留您的更改在您做更好的承诺之前需要编辑一点。 从这里重新开始, C 做为您的总部 :

   (F)
A-B-C
    ↑
  master

做这个,离开--hard:

git reset HEAD~1

在这种情况下,结果是:

   (F)
A-B-C
  ↑
master

在这两种情况下,HEAD都只是最新承诺的指针。git reset HEAD~1,您告诉 Git 将 HEAD 指针移回一个承诺。但(除非您使用)--hard你把你的档案和以前一样留在原样,所以现在git status显示您在 C 中检查过的更改。 您没有丢失任何东西 !

备选3:git reset --soft

最轻轻的触碰,你甚至可以取消您的承诺, 但要离开您的文件和文件指数指数指数指数:

git reset --soft HEAD~1

这不仅留下你的档案, 甚至离开你的指数指数指数指数当你独自一个人去的时候,git status,你会看到相同的文件在索引中和以前一样。事实上,在这个命令之后,你可以做git commit你会重蹈覆辙 做你刚做过的同样的事

备选方案4:你确实做了git reset --hard需要把密码拿回来

还有一件事: 假设你销毁如在第一个示例中,但后来发现你需要它运气不好,对不对?

不,有仍为键入此

git reflog

你将看到一份(部分)承诺的清单,沙沙即你们所迁居的地方。你们要找出你们所摧毁的罪状,然后你们要这样做:

git checkout -b someNewBranchName shaYouDestroyed

承诺在Git不会在90天内被摧毁 所以你通常可以回去 拯救一个你本不想除掉的人

在回答之前,我们补充一些背景,解释一下这是什么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 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.

要让一切恢复到上次承诺之前的状态 我们需要reset之前的承诺HEAD:

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 log将显示我们最后的承诺已被删除 。

如何撤销公开承诺

如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 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 - 撤销事情.