我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
当地承诺
git reset --soft HEAD~1
或者如果你们记不起来,那末,你们可以使用
git rm --cached <file>
用于被推动的 承诺
从存储库历史中删除文件的适当方式是使用 git 过滤器- blanch 。 也就是说,
git filter-branch --index-filter 'git rm --cached <file>' HEAD
但我建议您谨慎使用此命令 。 更多信息请访问 git- filter- branch- blanch- 1 手册页面 。
其他回答
如何编辑上一个承诺
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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
在 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 --hard HEAD~1
现在,为了摆脱你最后的承诺,你必须
git push --force
这就对了,这会消除你最后的承诺
使用此命令
git checkout -b old-state 0d1d7fc32
另一种方式:
检查您想要返回的分支, 然后将本地工作副本重置到您想要在远程服务器上成为最新一份的承诺( 它将在离别后进行的一切) 。 要做到这一点, 请在 SupportTree I右键点击并选中的“ 重置 BranchNAME 以进行此承诺 ” 。
然后导航到仓库的本地目录并运行此命令 :
git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME
这将删除您本地仓库中当前一个后的所有承诺, 但只删除该分支 。