我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。

我如何从当地存储库撤销这些承诺?


当前回答

只要使用 git 重置它执行下面的命令 :

git reset --soft HEAD~1

解释 : git 重设做什么, 它基本上是重置到任何您想要返回的承诺, 然后如果它与 -- soft 键合并, 它会返回, 但保留您文件中的更改, 所以您回到刚刚添加文件的阶段, HEAD 是分支的头部, 如果您和 ~ 1 合并( 在此情况下, 您也使用 HEAD+) , 它只会返回一个您想要的...

我为您创建了以下图像中的步骤, 详情更多, 包括所有在真实情况下可能发生的步骤, 并承诺执行代码 :

其他回答

要重置上一个修订版, 请永久删除所有未承诺的更改 :

git reset --hard HEAD~1

通常情况下, 您想要撤销一项承诺, 因为您犯了一个错误, 您想要修正它, 基本上就是 OP 提问时所做的。 真的, 您真的想要重做一次承诺 。

这里的大多数答案都集中在命令行上。 虽然命令行是使用Git的最佳方法, 当你对它感到舒服的时候, 它可能与来自其他版本控制系统到 Git 的人有点不同。

使用 GUI 如何操作。 如果您安装了 Git, 您已经拥有了所有您需要遵守的指示 。

注意: 我在此假设您在推动前意识到承诺是错误的。 如果您不知道推力意味着什么, 那么您可能还没有被推动。 继续执行指令。 如果您推动了错误承诺, 最危险的方法就是跟踪错误承诺, 并做出新的修正, 而在版本控制系统中, 您会这样做, 不允许您重写历史 。

使用图形用户界面来修正你最近的过错:

导航到命令行的仓库, 并用 git gui 选择“ 修正最后一项承诺 ” 启动 GUI 。 您将会看到您上次的承诺信件、 您所准备的文件 以及您没有的文件 。 现在更改了您想要它们如何查看和单击 COD 。

使用 SourceTree ( Git 的图形工具) 查看您的承诺和树。 您可以单击右键手动重置它 。

如何编辑上一个承诺

通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,

我发现自己经常去修修过去的东西 以至于我写了剧本

以下是工作流程:

git exent- edit <commit- hash> 这将让您在您想要编辑的承诺时丢弃您。 承诺的更改将会被卸下, 将按您希望的第一次进行, 并准备按您希望的第一次进行。 固定并按您希望的, 并按您希望的原初阶段进行承诺 。 (您可能想要使用 git 隐藏保存 -- kep- index 来抓松任何您没有执行的文件) 重做承诺 -- amend, 例如: git 承诺 -- amend compult the rebase: git rebase -- continue


把这个调用在 Git- commit- edit 之后, 并把它放在您的 $PATH:

#!/bin/bash

# Do an automatic git rebase --interactive, editing the specified commit
# Revert the index and working tree to the point before the commit was staged
# https://stackoverflow.com/a/52324605/5353461

set -euo pipefail

script_name=${0##*/}

warn () { printf '%s: %s\n' "$script_name" "$*" >&2; }
die () { warn "$@"; exit 1; }

[[ $# -ge 2 ]] && die "Expected single commit to edit. Defaults to HEAD~"

# Default to editing the parent of the most recent commit
# The most recent commit can be edited with `git commit --amend`
commit=$(git rev-parse --short "${1:-HEAD~}")

# Be able to show what commit we're editing to the user
if git config --get alias.print-commit-1 &>/dev/null; then
  message=$(git print-commit-1 "$commit")
else
  message=$(git log -1 --format='%h %s' "$commit")
fi

if [[ $OSTYPE =~ ^darwin ]]; then
  sed_inplace=(sed -Ei "")
else
  sed_inplace=(sed -Ei)
fi

export GIT_SEQUENCE_EDITOR="${sed_inplace[*]} "' "s/^pick ('"$commit"' .*)/edit \\1/"'
git rebase --quiet --interactive --autostash --autosquash "$commit"~
git reset --quiet @~ "$(git rev-parse --show-toplevel)"  # Reset the cache of the toplevel directory to the previous commit
git commit --quiet --amend --no-edit --allow-empty  #  Commit an empty commit so that that cache diffs are un-reversed

echo
echo "Editing commit: $message" >&2
echo

在我的情况中,我承诺并推向错误的分支, 所以我想要的是把全部的改变都收回来, 这样我就可以把它们投向一个新的正确的分支, 所以我做了这个:

在您承诺和推动的同一分支上, 如果您输入“ gitt status” 键入“ git status” 状态, 您将不会看到任何新事物, 因为您承诺和推动, 现在键入 :

    git reset --soft HEAD~1

这将将您所有的更改( 文件) 重新回到舞台区域, 现在把它们重新回到工作目录( 非阶段) 中, 您只要输入 :

git reset FILE

“ 文件” 是您想要再次承诺的文件 。 现在, FILE 应该在工作目录中, 包含您所做的全部更改 。 现在您可以更改为您想要的分支, 并对该分支进行更改 。 当然, 您承诺的初始分支仍然有所有更改, 但对于我来说, 如果不是您的话, 您可以寻找恢复该分支承诺的方法 。