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

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


当前回答

以下是网站:Oh shit, git!.

这里有许多解密的秘方 如何在Git 中解决事情。其中一些是:

哦,该死,我需要改变 信息在我的最后一次承诺!

git commit --amend
# follow prompts to change the commit message

哦,我无意中把什么东西 给了主人 本来该在全新的树枝上!

# Create a new branch from the current state of master
git branch some-new-branch-name
# Remove the commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
# Your commit lives in this branch now :)

其他回答

撤消一次承诺重做( 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?如果您想要解开多项承诺, 将会很有帮助 。

使用此命令

git checkout -b old-state 0d1d7fc32

替换您的本地版本, 包括您对服务器版本的更改。 这两行代码将迫使 Git 调用并覆盖本地版 。

打开命令提示并导航到 Git 工程根 。 如果您使用视觉工作室, 请单击小组小组, 同步同步并点击下面的“打开命令提示”(见图像) 。

Visual Studio

一旦进入CMD, 继续使用以下两个指示。

git fetch --all

那你就做

git reset --hard origin/master

这将覆盖 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 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 rebase --onto HEAD~1 HEAD

但如果你想放弃其中的 多项承诺,你确实说

- > b - > c - > > d - > 硕士

,然后您要放弃“ c” 承诺

git rebase --onto b c

这将使“ b” 成为“ d” 删除“ c” 的新基础