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

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


当前回答

我验证了所提议的有效方法,这里就是使用这一方法的一个具体例子:

如果您想要永久撤销/取消您的最后一项承诺(等等, 一个一个一个, 尽可能多) , 有三个步骤 :

1: 获取您想要到达的承诺的 id = SHA, 当然

$ git log

2: 删除您先前的承诺

$ git reset --hard 'your SHA'

3: 将新的本地历史强制在您的原籍 GitHub 与-f选项( 将从 GitHub 历史中删除最后的磁轨)

$ git push origin master -f

示例示例示例示例

$ git log

最后一次承诺取消

commit e305d21bdcdc51d623faec631ced72645cca9131 (HEAD -> master, origin/master, origin/HEAD)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 03:42:26 2020 +0200

U2_30 S45; updating files package.json & yarn.lock for GitHub Web Page from docs/CV_Portfolio...

现在就向总部提交我们想要的文件

commit 36212a48b0123456789e01a6c174103be9a11e61
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

First commit, new title

删除最后一项, 达到承诺前, 删除最后一项

$ git reset --hard 36212a4

HEAD is now at 36212a4 First commit, new title

检查一下,没事的

$ git log

commit 36212a48b0123456789e01a6c174103be9a11e61 (HEAD -> master)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

    First commit, new title

$ git status

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
 (use "git pull" to update your local branch)

nothing to commit, working tree clean

更新 Git (Hub) 上的历史

$ git push origin master -f

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ GitUser bla bla/React-Apps.git
 + e305d21...36212a4 master -> master (forced update)

检查一下,没事的

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

其他回答

参考:如何撤销在 Git 的最后一次承诺 ?

如果您已经安装了 Git 扩展名, 您可以很容易撤销/ 撤销任何连接( 您可以下载 Git 扩展名)在这里).

打开 Git 扩展名, 右键单击您想要返回的承诺, 然后选择“ 回溯承诺 ” 。

Git Extensions screen shot

将打开弹出( 见下文截图)

Revert commit popup

选择“自动创建一项承诺”,如果您想要直接进行已恢复的更改,或者如果您想要手动进行已恢复的更改,则保留未选中的框,单击“撤销此承诺”按钮。

在回答之前,我们补充一些背景背景,解释一下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 reset HEAD@{n}将重置您上一个n行动。

用于重设, 对于上一个动作, 使用git reset HEAD@{1}.

如果您想要还原最后一个任务, 但仍要保留在任务中本地作出的更改, 请使用此命令 :

git reset HEAD~1 --mixed

在视觉工作室中,只需返回您的承诺, 然后将它与自动创建的返回承诺一起推。 全部显示在下面的屏幕镜头中 。 @ info: status

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here