我无意中把错误的文件给了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 。

其他回答

如果您是和SourceTree合作, 这会帮助您 。

在承诺上右键点击, 然后选择“ 重置( 当前分支) / master 到此承诺 ” , 最后选择“ 软” 重置 。

认为我们已经有了 code. txt 文件。 我们对其做了一些修改并承诺。 我们可以以三种方式撤销此承诺, 但首先, 您应该知道什么是预置文件... 一个预设文件是一个可以承诺的文件。 如果您运行 git 状态, 此文件将会显示为绿色颜色, 如果它没有被预设用于承诺, 将会显示为红色颜色 :

意思是如果您承诺更改, 此文件中的更改没有被保存 。 您可以在您的舞台上添加此文件, 添加 code. txt , 然后进行更改 :

撤消上次承诺 :

现在,如果我们想要在不做其他任何更改的情况下撤销承诺, 我们可以使用 git 重设 -- soft HEAD * 如果我们想要撤销承诺及其更改( THIS 是危险的, 因为您的更改将会丢失) , 我们可以使用 git 重置 -- 硬 HEAD * 如果我们想要撤销承诺并删除舞台上的更改, 我们可以使用 git 重置 -- mixed HEAD* 或短格式的 git 重置 HEAD***

你需要做简单快的动作

    git commit --amend

是私人分行还是私人分行

    git commit -m 'Replace .class files with .java files'

如果它是共享的或公共的分支。

我很久以前也曾写过这些话,

如何删除/ 反转 Git 承诺

基本上,你只需要做:

git 日志, 获取 SHA hash 的前七个字符, 然后进行 git return < sha> , 然后再进行 git push -- force 。

您也可以使用 Git 返回命令, 恢复此命令如下: git 返回 < sha> - m - 1, 然后按 git 键 。

如何使用,重置 -- 软的或重置 -- 硬的?

@Kyralessa的回答:

如果您不确定该使用什么 -- Soft (我用这个公约来记住它 -- 安全性 -- 软性) 。

为什么?

如果您选择 -- 错误地选择 -- 错误地选择, 您将会失去以前没有的更改。 如果您选择 -- 错误地选择 -- 错误地选择 -- 软, 您可以通过应用额外命令而实现同样的结果 -- 硬

git reset HEAD file.html
git checkout -- file.html

完整示例

echo "some changes..." > file.html
git add file.html
git commit -m "wrong commit"

# I need to reset
git reset --hard HEAD~1 (cancel changes)
# OR
git reset --soft HEAD~1 # Back to staging
git reset HEAD file.html # back to working directory
git checkout -- file.html # cancel changes

感谢@Kyralessa。