我无意中把错误的文件给了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 HEAD~
fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
发生错误的原因是,如果最后一次承诺是存储处的初始承诺(或没有父/母),则不存在 HEAD~ 。
解决方案
如果您想要重置“ master” 分支上唯一的承诺
$ git update-ref -d HEAD
$ git rm --cached -r .
撤销一项承诺是有点吓人,如果你不知道它是如何运作的。 但如果你理解的话,它其实很容易。我会告诉你4种不同的方式, 你可以解除一项承诺。
说你们有这个,C是你们的总部,(F)是你们档案的状态。
(F)
A-B-C
↑
master
选项1: git 重设 -- hard
您想要销毁C实施者, 并丢弃任何未承诺的更改 。 您这样做 :
git reset --hard HEAD~1
结果是:
(F)
A-B
↑
master
B现在是 HEAD 。 因为您使用 -- hard 键, 您的文件被重置为在承诺 B 状态 。
备选2:Git重置
实施 C 可能不是一场灾难, 只是有点不对劲 。 您想要撤销此承诺, 但要在做出更好的承诺之前先保留您的更改, 然后再进行编辑 。 从这里重新开始, 以 C 为主机 :
(F)
A-B-C
↑
master
做到这一点, 离开 -- hard :
git reset HEAD~1
在这种情况下,结果是:
(F)
A-B-C
↑
master
在两种情况下, HEAD 都只是最新承诺的指针。 当您重置 HEAD~ 1 时, 您会告诉 Git 将 HEAD 指针移回一个。 但是( 除非使用 -- hard) 您会离开文件原样。 因此, Git 状态显示您检查到 C 的更改 。 您没有丢失任何东西 !
备选方案3: git 重设 -- 软
对于最轻的触摸,你甚至可以撤销你的承诺,但留下你的文件和索引:
git reset --soft HEAD~1
这不仅留下您的文件, 甚至留下您的索引 。 当您做 Git 状态时, 您就会看到相同的文件在索引中 。 事实上, 在此命令之后, 您就可以做 Git 承诺, 您就会重做您刚刚做的同样的事情 。
选项 4: 您确实重置了 git -- hard 重置, 并且需要将代码调回
还有一件事:假设你像第一个例子一样摧毁一个承诺,但发现你毕竟需要它?运气不好,对吧?
不,还有办法把它拿回来,按这个键
git reflog
你将看见一份你所迁入的沙子(部分),你将看见它。你应当发现你所破坏的,你应当做这件事:
git checkout -b someNewBranchName shaYouDestroyed
承诺在Git不会在90天内被摧毁 所以你通常可以回去 拯救一个你本不想除掉的人
第一运行 :
git reflog
这将显示您在存储库中实施的所有可能的动作, 例如, 承诺、 合并、 调用等 。
那么,请:
git reset --hard ActionIdFromRefLog
取决于您是否已经公开了您的前一次承诺( 插入到您的远程仓库 ) :
如何撤销本地任务
比方说我承诺了当地, 但现在我想取消那个承诺。
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 - 撤销一些事情 。
在回答之前,让我们补充一些背景,解释一下这个总部是什么。
首先,什么是总部?
HEAD 只是引用当前分支的当前承诺( 最新承诺) 。 任何时候只能有一个 HEAD 。 (不包括 git worktree)
HEAD的内容存放在.git/HEAD内,包含当前承诺的40字节SHA-1。
独立头部
如果您没有在最新的承诺 - 意指HEAD指向 先前在历史中的承诺 它被称为独立的HEAD。
调
在命令行上,它会看起来像这个- SHA-1, 而不是分支名称, 因为 HEAD 没有指向当前分支的端端点 。
调
调
如何从独立的总部中恢复的几种选择:
git 检出
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
这将取出指向想要的承诺的新分支 。 此命令会取出给定的承诺 。 在此点上, 您可以创建一个分支, 从此开始工作 。
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# create a new branch forked to the given commit
git checkout -b <branch name>
git 重新格式
您也可以同时使用reflog。 git reflog 将显示任何更新 HEAD 的更改, 并检查想要的 reflog 条目, 将会将 HEAD 设置回至此任务 。
每次对总部领导部进行修改时,在重新格式中将有一个新的条目
git reflog
git checkout HEAD@{...}
这样你就可以回到你想要的事业了
调
git 重置 - 硬设置 < commit_ id>
"移动"你的总部 回到想要的承诺。
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
注: (自Git 2. 7) 您也可以使用 Git rebase -- no- autostash 。
git 返回 <sha-1>
“ 撤消” 指定的承诺或承诺范围。 重置命令将“ 撤消” 对给定承诺所做的任何更改 。 在原始承诺也将保留在历史中时, 将使用撤消补丁的新承诺 。
# add new commit with the undo of the original one.
# the <sha-1> can be any commit(s) or commit range
git revert <sha-1>
此 schema 显示哪个命令能做什么 。 正如您可以看到的, 重置 {}\ 检查退出 修改 HEAD 。
调