我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
这将添加一个新的承诺, 删除所添加的文件 。
git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"
或者您可以重写历史来撤销最后一项承诺 。
警告:此命令将永久删除对.java 文件( 和任何其他文件) 所做的修改 -- 从工作目录中删除所有修改 :
git reset --hard HEAD~1
硬重置为 HEAD-1 将把您的工作副本设定为您错误实施前的行为状态 。
其他回答
通过下列方式查看日志来查找上次承诺的散列代码 :
git log
然后
git reset <the previous co>
主要设想有两种情况:
你还没推动承诺呢
如果问题在于您所做的额外文件( 而您不希望存储库中的文件) , 您可以使用 git rm 来删除文件, 然后用 -- amend 进行 -- amend
git rm <pathToFile>
您也可以用 -r 来删除整个目录, 或甚至与其他 Bash 命令合并
git rm -r <pathToDirectory>
git rm $(find -name '*.class')
删除文件后, 您可以使用 -- amend 选项执行 -- amend 选项
git commit --amend -C HEAD # the -C option is to use the same commit message
这将重写您最近的本地承诺删除额外文件, 因此, 这些文件将不会被按键发送, 并且 GC 将会从您本地的. git 仓库中删除 。
您已经推进了任务
您可以对其它情景应用相同的解决方案, 然后用 -f 选项进行 Git 推, 但建议不推荐, 因为它以不同的变化覆盖了远程历史( 它会干扰您的仓库 ) 。
相反,您必须在不做 -- amend 的情况下做承诺(记住关于 -amend : 此选项重写上次承诺的历史 ) 。
每个人都以如此复杂的方式发表意见。
如果您想要从分支中删除最后一项承诺, 最简单的方法是:
git reset --hard HEAD~1
现在,为了摆脱你最后的承诺,你必须
git push --force
这就对了,这会消除你最后的承诺
添加/删除文件, 以便按照您想要的方式获取文件 :
git rm classdir
git add sourcedir
然后修正承诺:
git commit --amend
之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样
请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。
如何编辑上一个承诺
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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