我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
为了消除(所有变化)最后承诺、最后2承诺和最后n承诺:
git reset --hard HEAD~1
git reset --hard HEAD~2
...
git reset --hard HEAD~n
在做出具体承诺后,要消除任何东西:
git reset --hard <commit sha>
例如,
git reset --hard 0d12345
谨慎使用硬选项 : 它会同时删除您回购的本地更改, 并返回到上一个提及的承诺 。 您只应该运行此选项, 前提是您确定您在最后一次承诺中搞砸了, 并且想要回到过去 。
作为副注释,大约7个承诺的散列字母就足够了,但在更大的项目中,你可能需要最多12个字母才能独有。如果您愿意,也可以使用整个承诺的 SHA 。
上述命令在GitHub也为Windows服务。
其他回答
如何编辑上一个承诺
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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 rm classdir
git add sourcedir
然后修正承诺:
git commit --amend
之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样
请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。
取决于您是否已经公开了您的前一次承诺( 插入到您的远程仓库 ) :
如何撤销本地任务
比方说我承诺了当地, 但现在我想取消那个承诺。
git log
commit 101: bad commit # Latest commit. This would be called 'HEAD'.
commit 100: good commit # Second to last commit. This is the one we want.
要让一切恢复到上次承诺之前的状态 我们需要重置承诺 重置承诺
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 the changes you made
现在 git 日志将显示我们最后的承诺已被删除 。
如何撤销公开承诺
如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 HEAD) 所做的更改 。
git revert HEAD
你们的更改将恢复,并准备好你们承诺:
git commit -m 'restoring the file I removed by accident'
git log
commit 102: restoring the file I removed by accident
commit 101: removing a file we don't need
commit 100: adding a file that we need
更多信息,请查看 Git Basics - 撤销一些事情 。
我更喜欢用Git rebase - i 来做这份工作,因为一个不错的名单会出现在我可以选择摆脱承诺的地方。它可能不像这里其他答案那样直接,但它只是感觉是正确的。
选择要列出多少个承诺, 然后这样引用( 加入最后 3 个)
git rebase -i HEAD~3
样本列表
pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support
然后 Git 将删除任何您删除的线条 。
简单, 在命令行运行此操作 :
git reset --soft HEAD~