我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
撤消一次承诺重做( 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)
git 重新设置是撤销命令的责任。 它会解除您最后的承诺, 同时将您的工作树( 您在磁盘上的文件状态) 保留下来。 您需要重新添加它们才能再次执行 。 对工作树文件进行更正 。 git 添加您想要包含在您新承诺中的任何内容 。 提交修改, 重用旧承诺信件 。 重置旧头部复制到. git/ ORIG_ HEAD; 与 - c ORIG_ HEAD 承诺将打开编辑器, 该编辑器最初包含旧承诺的日志信息, 并允许您编辑它 。 如果您不需要编辑信件, 您可以使用 - C 选项 。
或者,为了编辑上一个承诺(或仅仅是其承诺信息),承诺 -- -- 修正将在当前索引中将修改添加到上一个承诺。
要删除( 不回覆) 被推进到服务器的承诺, 有必要重写历史, 使用 Git 推源主 -- force [- with- level] 重写历史。 使用 -- force; 偏好 -- force- with- level, 几乎总是一个坏主意。 正如 Git 手册中所指出的 :
如果你[重写历史]已经发表,你应该了解重写历史的意义。
继续阅读
您可以使用 git reflog 来确定您想要返回的 SHA-1 承诺的 SHA-1 。 一旦您拥有此值, 请使用上文解释的命令序列 。
HEAD~ 和 HEAD~ 1 相同。 如果您想要解开多次承诺, 文章“ HEAD ” 是否有帮助 。
其他回答
如何编辑上一个承诺
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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 应该在工作目录中, 包含您所做的全部更改 。 现在您可以更改为您想要的分支, 并对该分支进行更改 。 当然, 您承诺的初始分支仍然有所有更改, 但对于我来说, 如果不是您的话, 您可以寻找恢复该分支承诺的方法 。
在 SourceTree ( GitHub 的 GUI) 上, 您可以右键单击承诺并做一个“ 反向提交 ” 。 这将取消您的更改 。
在终点站上:
可选择使用:
git revert
或者:
git reset --soft HEAD^ # Use --soft if you want to keep your changes.
git reset --hard HEAD^ # Use --hard if you don't care about keeping your changes.
您可以使用:
git reset HEAD@{1}
此命令将删除您没有 Git 日志的错误执行 。
单一个命令 :
git reset --soft 'HEAD^'
成功推翻了当地最后的承诺!