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

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


当前回答

如果你犯下了破案 但没有被逼

git reset --soft HEAD~1

总部~1是在头部前的一小节。或者,您可以参考SHA-1(SHA-1)。如果您要重置,则要重置。-- 软选项将删除承诺, 但它会保留您更改的所有文件“ 更改待执行” , 如 Git 状态所显示 。

如果您想要删除工作树中跟踪文件的任何更改, 因为在头使用前要执行此任务 " 。--- 硬" 相反。

如果你已经推了,有人拉了拉了 通常是我的案子,你不能用git 重设。但是,你可以做一个git 返回 git 返回,

git revert HEAD

这将产生新的承诺, 扭转意外行为带来的一切。

其他回答

作为以下步骤。

步骤1

命中git log

从日志列表中找到最后的硬化散列代码, 然后输入 :

步骤2

git reset <hash code>

撤消一次承诺重做( 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)
  1. git reset是负责取消对。它会解除您最后的承诺,而您的工作树( 您在磁盘上的文件状态) 尚未被移动 。在你再次犯下罪行之前,你需要再加一次)。
  2. 作出以下更正工作树文件。
  3. git add任何您想要包含在您新承诺中的内容 。
  4. 提交修改,重复使用旧的已发信件 。reset复制旧头头到.git/ORIG_HEAD; commit-c ORIG_HEAD将打开编辑器,该编辑器最初包含旧承诺的日志消息,并允许您编辑它。如果不需要编辑该消息,您可以使用该-C选项。

或者编辑上一个承诺( 或仅其承诺信件), commit --amend将在当前索引内对上一个承诺添加变化。

要删除( 不再返回) 被推到服务器的承付重写历史git push origin main --force[-with-lease]这是必要的。这是几乎几乎总是使用一个坏主意--force; 偏; 更优--force-with-lease改为,如以下所示:Git 手册:

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


继续阅读

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


HEAD~HEAD~1该条什么是头在Git?如果您想要解开多项承诺, 将会很有帮助 。

删除已被推到 Github 的错误行为

git push origin +(previous good commit id):(branch name)

请指定您想要在 Github 重置的最后一个好承诺代号 。

例如,如果最近的承诺 ID 错误, 请用分支名称在 Git 命令上方指定前一个承诺 ID 。

您可以使用git log

在回答之前,我们补充一些背景,解释一下这是什么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 reset --hard <last good SHA>重新设置修改并给予新的承诺。您也可以使用git checkout -- <bad filename>.