我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。

我如何从当地存储库撤销这些承诺?


当前回答

在回答之前,让我们补充一些背景,解释一下这个总部是什么。

首先,什么是总部?

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 。

其他回答

撤销上次承诺 :

git reset --soft HEAD^ or git reset --soft HEAD~

这将撤销最后一项承诺 。

在这里 -- 软意味着重置中。

HEAD~ 或 HEAD_ 意思是移动 在 HEAD 之前承诺。

替换上次承诺的新承诺:

git commit --amend -m "message"

它将以新承诺取代最后一项承诺 。

如果您想要永久撤销它, 您克隆了一些仓库 。

犯罪身份可通过以下方式识别:

git log 

然后你们可以做:

git reset --hard <commit_id>

git push origin <branch_name> -f

如果您已经安装了 Git Explos, 您可以运行 Git 撤消以撤销最近的承诺 。 git 撤消 3 将撤销最后的 3 个承诺 。

如何修正上一个本地承诺

使用 git- gui (或类似) 执行 git- committee -- amend 。 您可以从 GUI 中添加或删除承诺中的单个文件。 您也可以修改承诺信件 。

如何撤销上一个本地任务

只需将分支重置到上一个位置( 例如, 使用 gitk 或 git 重新基准 ) 即可。 然后重新应用保存的副本中的更改 。 在本地仓库收集垃圾后, 它会像未发生不想要的操作一样。 要在单一个命令中完成所有这些, 请使用 git 重置 HEAD~ 1 。

警告的话: 粗心使用 Git 重置是一个很好的方法, 让你的工作副本陷入混乱状态 。 我建议Git Novice 尽量避免这种情况 。

如何撤销公开承诺

执行反向樱桃选取( git- revert) 以撤销更改 。

如果你还没有在你的分支上做其他的修改, 你可以简单地做。 。 。

git revert --no-edit HEAD

然后将更新的分支推至共享仓库 。

承诺历史将分别显示两个承诺。


高级:对公共储存库中的私营分支机构的更正

这可能很危险 -- -- 请确定您有该分支的本地副本要反射 。

备注:如果有其他人在为分支工作,您不想这样做。

git push --delete (branch_name) ## remove public version of branch

清理你的分行 在当地,然后冲...

git push origin (branch_name)

通常情况下, 您可能不需要担心您的私人部门 将历史变为原始历史。 只要催促后续承诺( 参见上面的“ 如何撤销公共承诺 ” ) , 然后再做壁球合并来隐藏历史 。

我想撤销我们共享存储库中最新的五个承诺。 我查了我想回溯到的订正代号 。 然后我输入了下面的内容 。

prompt> git reset --hard 5a7404742c85
HEAD is now at 5a74047 Added one more page to catalogue
prompt> git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: neoneye is allowed. accepted payload.
To git@bitbucket.org:thecompany/prometheus.git
 + 09a6480...5a74047 master -> master (forced update)
prompt>