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

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


当前回答

恢复和放弃承诺是最好的,如果你想在向公共部门等提出补丁时保持历史清洁有用的话。

如果你必须放下最上层的承诺, 那么以下的单行帮助

git rebase --onto HEAD~1 HEAD

但如果你想放弃其中的 多项承诺,你确实说

- > b - > c - > > d - > 硕士

,然后您要放弃“ c” 承诺

git rebase --onto b c

这将使“ b” 成为“ d” 删除“ c” 的新基础

其他回答

只需撤销最后一项承诺 :

git reset --soft HEAD~

或撤销上次承诺前的时间 :

git reset --soft HEAD~2

或撤销先前的任何承诺 :

git reset --soft <commitID>

(您可以使用git reflog)

当您撤销上一个任务时, 记住要用

git clean

更多信息,请查看以下文件:git- reseet 重置

在这种情况下,"重置"命令就是你最好的朋友:

git reset --soft HEAD~1

重置将会将您当前的 HEAD 分支倒带到指定的修改 。 在以上的例子中, 我们希望回到当前修改之前的修改 - 有效地取消我们最后的承诺 。

注意 -- soft 旗号 : 这样可以确保未撤销修改的更改被保存。 在运行命令后, 您会发现您的工作副本中这些更改是未承诺的本地修改 。

如果您不想保留这些更改, 请使用 -- 硬旗 。 确定只有在您确定不再需要这些更改时才这样做 。

git reset --hard HEAD~1

头:

在重置承诺之前, 我们应该知道关于 HEAD... HEAD 在您的工作目录中只是您目前的状况。 它由承诺编号代表 。

Git 承诺 :

在由独有标签代表的一承诺下指定的每一项更改。 无法删除提交 。 所以, 如果您想要您最后的承诺, 您可以简单地跳入其中 。git reset.

您可以使用两种方法跳入最后一个承诺 :

方法1:(如果你不知道承诺编号,但想移动到第一个)

git reset HEAD~1  # It will move your head to last commit

方法2:(如果你们知道自己所做的一切,那末,你们只须将自己所知道的(事情)重新置身于你们所认识的(事情)中)

git reset 0xab3# 提交编号

注:想要了解最近一项任务尝试git log -p -1

以下是图形表达式 :

Enter image description here

替换您的本地版本, 包括您对服务器版本的更改。 这两行代码将迫使 Git 调用并覆盖本地版 。

打开命令提示并导航到 Git 工程根 。 如果您使用视觉工作室, 请单击小组小组, 同步同步并点击下面的“打开命令提示”(见图像) 。

Visual Studio

一旦进入CMD, 继续使用以下两个指示。

git fetch --all

那你就做

git reset --hard origin/master

这将覆盖 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