我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。

我如何解除那些承诺?当地当地仓库?


当前回答

使用此命令

git checkout -b old-state 0d1d7fc32

其他回答

这将添加一个新的承诺, 删除所添加的文件 。

git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"

或者您可以重写历史来撤销最后一项承诺 。

警告警告警告警告:此命令将永久删除对《京都议定书》的修改.java您承诺的文件( 和任何其他文件) -- 从工作目录中删除全部修改 :

git reset --hard HEAD~1

缩略hard resetHEAD-1您的工作副本将在您错误犯罪之前设定为犯罪状态 。

只要在下面执行命令时重置它, 使用git:

git reset --soft HEAD~1

解释:什么什么是git reset基本上reset任何承诺,如果你愿意 回去,如果你把它和--soft键,它将返回,但保留您文件中的更改,所以您可以回到刚刚添加文件的阶段,HEAD是分支的首脑,如果结合了~1(在这种情况下,你们也使用)HEAD^,它会回去 只有一个承诺 你想要什么...

我为您创建了以下图像中的步骤, 详情更多, 包括所有在真实情况下可能发生的步骤, 并承诺执行代码 :

How to undo the last commits in Git?

如果你还想背到最后承诺 并改过历史

在命令下方使用, 表示您想要跳到前一个任务, 因为前一个任务已经存在 ID SHA -71e2,e57458bde883a37b332035f784c6653ec509中,您可以指向此承诺,在此承诺之后不会显示任何日志消息,此后所有历史都将被删除。

git 推进源 + 71e2e57458bde883a37b332035f784c6653ec509: 技术主管

IntellelliJIDEA 您可以按下打开 Git 的 Git 仓库日志平平+9,鼠标右键单击承诺列表中的某些标记,并选择:重置当前分支到这里....

在回答之前,我们补充一些背景背景,解释一下HEAD是。 这是。

First of all what is HEAD?

HEAD仅指当前分支的当前承诺(最新承诺)。
只有一个,只有一个,只有一个HEAD在任何给定时间(不包括git worktree).

内容的内容HEAD存储在内部.git/HEAD并包含当前承诺的 40 字节 SHA-1 。


detached HEAD

如果你们不履行最近的承诺,那末,HEAD指向历史中的先前承诺 它被称作detached HEAD.

Enter image description here

在命令行上,它会看起来像这个 - SHA-1 而不是从HEAD不指向当前分支的端点 :

Enter image description here

Enter image description here


如何从独立的总部中恢复的几种选择:


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to 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

你可以随时使用reflog并且,还有。
git reflog 将显示更新HEAD并检查想要的 reflog 条目将设置HEAD返回到此任务。

总部总部每次修改时,将有一个新的条目。reflog

git reflog
git checkout HEAD@{...}

这样你就可以回到你想要的事业了

Enter image description here


git reset --hard <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.
  • 注:(a)自Git 2.7以来)您也可以使用git rebase --no-autostash并且,还有。

enter image description here


git revert <sha-1>

“ 撤消” 指定的承诺或承诺范围 。
重置命令将“ 撤消” 对给定任务所做的任何更改 。
将使用解析补丁进行新承诺, 而原始承诺也将保留在历史中 。

# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>

这个计划说明哪个指挥所做什么。
如你所见reset && checkout修改HEAD.

Enter image description here