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

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


当前回答

在回答之前,我们补充一些背景背景,解释一下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

其他回答

我更喜欢用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 外加已安装, 您可以运行git undo撤消最近的承诺。git undo 3将撤销最后三项承诺。

认为我们有code.txt 代码转换器。我们对其文件进行一些修改并做出承诺。我们可以以三种方式撤销这一承诺,但首先你应该知道什么是阶段文件...git status此文件将以绿色颜色显示, 如果此文件没有被预设用于承诺, 则会以红色显示 :

enter image description here

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

enter image description here

撤消上次承诺 :

  1. 如果我们想要在不做任何其他更改的情况下撤销承诺,

    git reset --soft HEAD^

    enter image description here

  2. 如果我们想要撤销承诺及其变化( ) 。这是危险的,因为你的改变会失去),我们可以使用

    git reset --hard HEAD^

    enter image description here

  3. 如果我们想要撤销承诺,从舞台上删除变化, 我们可以使用

    git reset --mixed HEAD^或以短或短形式git reset HEAD^

    enter image description here

(2015年等)

如果你在视觉工作室无法同步, 因为不允许你按到像“开发”那样的分支, 那么和我想的一样多, 在视觉工作室NEITHE莱弗特( REVERT)努尔战 地 方(硬的或软的)将有效。

答案是用数字标注的:

使用此命令命令 根 你的项目根 核武器 任何试图 被推推:

git reset --hard HEAD~1

备份或拉链你的文件 只是以防你不想失去任何工作,等等...

我拿到了身份证明bitbucket并随后:

git checkout commitID .

示例:

git checkout 7991072 .

它又把它恢复到那个承诺的工作副本上。