我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
在回答之前,让我们补充一些背景,解释一下这个总部是什么。
首先,什么是总部?
HEAD 只是引用当前分支的当前承诺( 最新承诺) 。 在任何特定时间( 不包括 git worktree) 只能有一个 HEAD 。
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 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。 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 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>
此图示显示哪个命令做什么。 正如您可以看到的, 重置 {} 检查退出 修改 HEAD 。
调
其他回答
如果您想要删除错误的文件, 您应该做
git 重置 -- s软 < your_ last_ good_commit_hash_ here> 在这里, 如果您做 Git 状态, 您可以在集结区看到文件。 您可以选择错误的文件, 从集结区拿下来 。
喜欢下面。
git 重置错误的File1 错误的File2 错误的File3
您现在可以添加您需要按键的文件,
添加好文件1 好文件2 git 添加好文件1 goodFile2
提交提交它们
git 承诺 -v 或 git 承诺 - am "Message"
推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推
git 推进源源主机
但是,如果您不关心已更改的文件, 您可以硬重置到先前的良好承诺, 并将所有文件都推到服务器 。
由
git reset --hard <your_last_good_commit_hash_here>
git 推进源源主机
如果您已经向服务器发布错误的文件, 您可以使用 -- 强制旗将文件推到服务器并编辑历史 。
git 推推 -- 原力硕士
取决于您是否已经公开了您的前一次承诺( 插入到您的远程仓库 ) :
如何撤销本地任务
比方说我承诺了当地, 但现在我想取消那个承诺。
git log
commit 101: bad commit # Latest commit. This would be called 'HEAD'.
commit 100: good commit # Second to last commit. This is the one we want.
要让一切恢复到上次承诺之前的状态 我们需要重置承诺 重置承诺
git reset --soft HEAD^ # Use --soft if you want to keep your changes
git reset --hard HEAD^ # Use --hard if you don't care about keeping the changes you made
现在 git 日志将显示我们最后的承诺已被删除 。
如何撤销公开承诺
如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 HEAD) 所做的更改 。
git revert HEAD
你们的更改将恢复,并准备好你们承诺:
git commit -m 'restoring the file I removed by accident'
git log
commit 102: restoring the file I removed by accident
commit 101: removing a file we don't need
commit 100: adding a file that we need
更多信息,请查看 Git Basics - 撤销一些事情 。
有许多方法可以做到:
撤消上次执行/ 上一个承诺的 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 和be20eb8 是实施代号的 。
现在,让我们来看看一些案例:
假设您想要返回上次的“ d81d3f1 ” 。 这里有两个选项 :
$ git reset --hard d81d3f1
或
$ git reset --hard HEAD~1
假设您想要还原“ ebe20eb8” 承诺 :
$ git reset --hard be20eb8
欲了解更详细的信息,请参考并尝试其他命令,将头重置为指定状态:
$ git reset --help
撤消上次承诺 :
git 重设 -- s软 HEADQ 或 git 重置 -- s软 HEAD~
这将撤销最后一项承诺 。
在这里 -- 软意味着重置中。
HEAD~ 或 HEAD_ 意思是移动 在 HEAD 之前承诺。
替换上次承诺的新承诺 :
git commit --amend -m "message"
它将以新承诺取代最后一项承诺 。
撤消一次承诺重做( R)
$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~ # (1)
[ edit files as necessary ] # (2)
$ git add . # (3)
$ git commit -c ORIG_HEAD # (4)
git 重新设置是撤销命令的责任。 它会解除您最后的承诺, 同时将您的工作树( 您在磁盘上的文件状态) 保留下来。 您需要重新添加它们才能再次执行 。 对工作树文件进行更正 。 git 添加您想要包含在您新承诺中的任何内容 。 提交修改, 重用旧承诺信件 。 重置旧头部复制到. git/ ORIG_ HEAD; 与 - c ORIG_ HEAD 承诺将打开编辑器, 该编辑器最初包含旧承诺的日志信息, 并允许您编辑它 。 如果您不需要编辑信件, 您可以使用 - C 选项 。
或者,为了编辑上一个承诺(或仅仅是其承诺信息),承诺 -- -- 修正将在当前索引中将修改添加到上一个承诺。
要删除( 不回覆) 被推进到服务器的承诺, 有必要重写历史, 使用 Git 推源主 -- force [- with- level] 重写历史。 使用 -- force; 偏好 -- force- with- level, 几乎总是一个坏主意。 正如 Git 手册中所指出的 :
如果你[重写历史]已经发表,你应该了解重写历史的意义。
继续阅读
您可以使用 git reflog 来确定您想要返回的 SHA-1 承诺的 SHA-1 。 一旦您拥有此值, 请使用上文解释的命令序列 。
HEAD~ 和 HEAD~ 1 相同。 如果您想要解开多次承诺, 文章“ HEAD ” 是否有帮助 。