我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
我验证了所提议的有效方法,这里就是使用这一方法的一个具体例子:
如果您想要永久撤销/取消您的最后一项承诺(等等, 一个一个一个, 尽可能多) , 有三个步骤 :
1: 获取您想要到达的承诺的 id = SHA, 当然
$ git log
2: 删除您先前的承诺
$ git reset --hard 'your SHA'
3: 强制使用 - f 选项( 最后一个承诺轨道将从 GitHub 历史中删除) 强制使用 GitHub 的本地新历史 。
$ git push origin master -f
示例示例示例示例
$ git log
最后一次承诺取消
commit e305d21bdcdc51d623faec631ced72645cca9131 (HEAD -> master, origin/master, origin/HEAD)
Author: Christophe <blabla@bla.com>
Date: Thu Jul 30 03:42:26 2020 +0200
U2_30 S45; updating files package.json & yarn.lock for GitHub Web Page from docs/CV_Portfolio...
现在就向总部提交我们想要的文件
commit 36212a48b0123456789e01a6c174103be9a11e61
Author: Christophe <blabla@bla.com>
Date: Thu Jul 30 02:38:01 2020 +0200
First commit, new title
删除最后一项, 达到承诺前, 删除最后一项
$ git reset --hard 36212a4
HEAD is now at 36212a4 First commit, new title
检查一下,没事的
$ git log
commit 36212a48b0123456789e01a6c174103be9a11e61 (HEAD -> master)
Author: Christophe <blabla@bla.com>
Date: Thu Jul 30 02:38:01 2020 +0200
First commit, new title
$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
更新 Git (Hub) 上的历史
$ git push origin master -f
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ GitUser bla bla/React-Apps.git
+ e305d21...36212a4 master -> master (forced update)
检查一下,没事的
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
其他回答
git push --delete (branch_name) //this will be removing the public version of your branch
git push origin (branch_name) //This will add the previous version back
在回答之前,让我们补充一些背景,解释一下这个总部是什么。
首先,什么是总部?
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 。
调
如果您想要永久撤销它, 您克隆了一些仓库 。
犯罪身份可通过以下方式识别:
git log
然后你们可以做:
git reset --hard <commit_id>
git push origin <branch_name> -f
作为以下步骤。
步骤1
Hit git 日志
从日志列表中找到最后的硬化散列代码, 然后输入 :
步骤2
git reset <hash code>
使用 git 返回 < commit- id > 。
要拿到身份证明,请使用 git 日志。