我无意中把错误的文件给了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 rm 来删除文件, 然后用 -- amend 进行 -- amend
git rm <pathToFile>
您也可以用 -r 来删除整个目录, 或甚至与其他 Bash 命令合并
git rm -r <pathToDirectory>
git rm $(find -name '*.class')
删除文件后, 您可以使用 -- amend 选项执行 -- amend 选项
git commit --amend -C HEAD # the -C option is to use the same commit message
这将重写您最近的本地承诺删除额外文件, 因此, 这些文件将不会被按键发送, 并且 GC 将会从您本地的. git 仓库中删除 。
您已经推进了任务
您可以对其它情景应用相同的解决方案, 然后用 -f 选项进行 Git 推, 但建议不推荐, 因为它以不同的变化覆盖了远程历史( 它会干扰您的仓库 ) 。
相反,您必须在不做 -- amend 的情况下做承诺(记住关于 -amend : 此选项重写上次承诺的历史 ) 。
在回答之前,让我们补充一些背景,解释一下这个总部是什么。
首先,什么是总部?
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
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 重置 -- 混集、 -- soft 和 -- hard 的区别
先决条件 : 当修改您存储库中的现有文件时, 最初将此项修改视为未阶段化的 。 为了实施更改, 需要分阶段进行, 这意味着使用 git 添加将其添加到索引中 。 在一次任务操作中, 被显示的文件会被添加到索引中 。
让我们举一个例子:
- A - B - C (master)
总部指向C,指数与C吻合。
-- 软
当我们执行 git 重设 -- soft B 时,我们执行 git 重设 -- soft B , 目的是删除承诺 C, 将主机/ HEAD 指向主机/ HEAD 指向 B。 主机/ HEAD 现在将指向 B, 但索引仍然与 C 。 当执行 git 状态时, 您可以看到在承诺 C 中索引的文件是分阶段的。 此时执行 git 承诺将产生与 C 相同变化的新承诺 。
-- 混合混合
执行 git 重新设置 -- mixed B 。 执行时, 主机/ HEAD 会指向 B , 索引也会因使用混合旗子而修改为 B 匹配 。 如果我们在此点运行 git 承诺, 自索引与 HEAD 匹配后, 就不会发生任何变化 。 工作目录中仍然有变化, 但是由于它们不在索引中, git 状态显示它们未显示为未显示的 。 要执行它们, 您会按常态添加并承诺 。
--- 硬
执行 git 重置 -- hard B 执行时, 主人/ HEAD 将指向 B 并修改您的工作目录 C 中添加的更改和所有未承诺的更改将被删除。 工作副本中的文件将与承诺 B 匹配, 这将导致永久删除所有在承诺 C 加上未承诺的更改中所做的更改
希望对可用用于 Git 重置命令的旗帜进行这种比较, 将有助于有人明智地使用这些旗帜。 请参考这些旗帜以获取进一步的详情链接1 (链接) 。
删除已被推到 Github 的错误行为
git push origin +(previous good commit id):(branch name)
请指定您想要在 Github 重置的最后一个好承诺代号 。
例如,如果最近的承诺 ID 错误, 请用分支名称在 Git 命令上方指定前一个承诺 ID 。
您可以使用 git 日志获得上一个承诺 ID 。