我无意中把错误的文件给了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 reset --soft HEAD~1
以上命令( 重置) 将把您当前的 HEAD 分支倒回指定的修改 。 在以上的例子中, 我们想回到当前修改前的修改中, 有效地取消我们上次的修改 。
注意 -- soft 旗号 : 这样可以确保未撤销修改的更改被保存。 在运行命令后, 您会发现您的工作副本中这些更改是未承诺的本地修改 。
如果您不想保留这些更改, 请使用 -- 硬旗 。 您确定不需要这些更改时, 一定要这样做 。
$ git reset --hard HEAD~1
调
视觉演播室代码使得这很容易。
调
您可以以两种方式撤销您的 Git 承诺 : 首先, 您可以使用 Git 返回, 如果您想要保留您的承诺历史 :
git revert HEAD~3
git revert <hashcode of commit>
第二是您可以使用 Git 重置, 这将删除您全部的委托历史, 并随心所欲地将您的头移到您想要的地方 。
git reset <hashcode of commit>
git reset HEAD~3
您也可以使用 -- hard 关键词, 如果有的话, 如果它开始有其他行为的话。 但是, 我建议在非常必要的时候使用它。
如果存储库是在当地承诺的,但尚未被推到服务器上,那么另一种粗糙/佣人解决它的方法将是:
Git 在另一个位置克隆仓库。 将修改( 文件/ 目录) 从原始仓库复制到这个新的仓库。 然后承诺并按下新仓库的修改。 用这个新的仓库替换旧的仓库 。
git reset --soft HEAD~1
重置将会将您当前的 HEAD 分支倒带到指定的修订版 。
注意 -- soft 旗号 : 这样可以确保未撤销修改的更改被保存。 在运行命令后, 您会发现您的工作副本中这些更改是未承诺的本地修改 。
如果您不想保留这些更改, 请使用 -- 硬旗 。 确定只有在您确定不再需要这些更改时才这样做 。
git reset --hard HEAD~1
撤消多个提交文件
git reset --hard 0ad5a7a6
但是,记住,使用重置命令可以解除所有在你返回后的承诺:
调