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

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


当前回答

您可以使用您不想播放的文件重置不想要的文件命令。 通过使用此命令, 我们可以将更改的文件从中位区域移到工作目录 。

我如何在本地及远程解析 Git 承诺?

其他回答

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

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

    git reset --soft HEAD~1

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

git reset FILE

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

通过下列方式查看日志来查找上次承诺的散列代码 :

git log

然后

git reset <the previous co>

引用: 如何撤销 Git 中的最后一次承诺 ?

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

打开 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

如何修正上一个本地承诺

使用 git- gui (或类似) 执行 git- committee -- amend 。 您可以从 GUI 中添加或删除承诺中的单个文件。 您也可以修改承诺信件 。

如何撤销上一个本地任务

只需将分支重置到上一个位置( 例如, 使用 gitk 或 git 重新基准 ) 即可。 然后重新应用保存的副本中的更改 。 在本地仓库收集垃圾后, 它会像未发生不想要的操作一样。 要在单一个命令中完成所有这些, 请使用 git 重置 HEAD~ 1 。

警告的话: 粗心使用 Git 重置是一个很好的方法, 让你的工作副本陷入混乱状态 。 我建议Git Novice 尽量避免这种情况 。

如何撤销公开承诺

执行反向樱桃选取( git- revert) 以撤销更改 。

如果你还没有在你的分支上做其他的修改, 你可以简单地做。 。 。

git revert --no-edit HEAD

然后将更新的分支推至共享仓库 。

承诺历史将分别显示两个承诺。


高级:对公共储存库中的私营分支机构的更正

这可能很危险 -- -- 请确定您有该分支的本地副本要反射 。

备注:如果有其他人在为分支工作,您不想这样做。

git push --delete (branch_name) ## remove public version of branch

清理你的分行 在当地,然后冲...

git push origin (branch_name)

通常情况下, 您可能不需要担心您的私人部门 将历史变为原始历史。 只要催促后续承诺( 参见上面的“ 如何撤销公共承诺 ” ) , 然后再做壁球合并来隐藏历史 。