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

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


当前回答

#1) $ git commit -m "Something terribly misguided" 
#2) $ git reset HEAD~                              

[必要时编辑文件]

 #3) $ git add .
#4) $ git commit -c ORIG_HEAD  

其他回答

如果你犯下了破案 但没有被逼

git reset --soft HEAD~1

总部~1是在头部前的一小节。或者,您可以参考SHA-1(SHA-1)。如果您要重置,则要重置。-- 软选项将删除承诺, 但它会保留您更改的所有文件“ 更改待执行” , 如 Git 状态所显示 。

如果您想要删除工作树中跟踪文件的任何更改, 因为在头使用前要执行此任务 " 。--- 硬" 相反。

如果你已经推了,有人拉了拉了 通常是我的案子,你不能用git 重设。但是,你可以做一个git 返回 git 返回,

git revert HEAD

这将产生新的承诺, 扭转意外行为带来的一切。

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

你可以随时使用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并且,还有。

git revert <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>

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

enter image description here

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

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

git log 

然后你们可以做:

git reset --hard <commit_id>

git push origin <branch_name> -f

有许多方法可以做到:

撤消上次执行/ 上一个承诺的 Git 命令 :

警告 :不使用 - 硬 如果你不知道自己在做什么, 硬就是太硬危险,它可能会删除您的文件 。

返回 Git 中的承诺的基本命令是 :

$ git reset --hard <COMMIT -ID>

$ git reset --hard HEAD~<n>

协调会 协调会:承诺的代号

n:您想要返回的上次承诺的数

您可以获取下列承诺代号:

$ **git log --oneline**

d81d3f1 function to subtract two numbers

be20eb8 function to add two numbers

bedgfgg function to multiply two numbers

何 地d81d3f1 d81d3f1贝多208执行 ID。

现在,让我们来看看一些案例:

假设您想要返回上次的“ d81d3f1 ” 。 这里有两个选项 :

$ git reset --hard d81d3f1

$ git reset --hard HEAD~1

假设您想要还原“ ebe20eb8” 承诺 :

$ git reset --hard be20eb8

欲了解更详细的信息,请参考并尝试其他命令,将头重置为指定状态:

$ git reset --help

典型基周期

在前几次答复中谈到与Git相关的命令时,我想向所有读者介绍我典型的Git周期,这可能是有益的。 这就是我如何与Git合作,

  1. 第一次从远程服务器克隆

    git clone $project

  2. 从远程拉动( 当我没有当地未完成的推动承诺) )

    git pull

  3. 将新的本地文件 1 添加到 $to_ be_ compent_ list (想象$to_ be_ compent_ list 意思是staged区域)

    git add $file1

  4. 删除从 $to_be_ composed_ list 错误添加的文件2( 假设文件2 与第 3 步一样添加, 我并不想这样做 )

    git reset $file2

  5. 正在提交 $to_ be_ compent_ list 中的文件1

    git commit -m "commit message description"

  6. 在推进前与远程仓库同步本地承诺

    git pull --rebase

  7. 冲突发生时的解决配置合并工具

    git mergetool #resolve merging here, also can manually merge

  8. 添加冲突解决文件file1:

    git add $file1

  9. 继续我先前的重新基地指挥

    git rebase --continue

  10. 启动已同步的本地最后一次任务已就绪

    git push origin head:refs/for/$branch # branch = master, dev, etc.