本文翻译自:How to uncommit my last commit in Git [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • How do I undo the most recent local commits in Git? 如何撤消Git中的最新本地提交? 81 answers 81个答案

How can I uncommit my last commit in git? 如何取消对git的最后提交?

Is it 是吗

git reset --hard HEAD

or 要么

git reset --hard HEAD^

?


#1楼

参考:https://stackoom.com/question/BwIt/如何取消提交我在Git中的最后一次提交-重复


#2楼

To keep the changes from the commit you want to undo 为了避免更改要撤消的提交,

git reset --soft HEAD^

To destroy the changes from the commit you want to undo 要破坏要撤消的提交中的更改

git reset --hard HEAD^

You can also say 你也可以说

git reset --soft HEAD~2

to go back 2 commits. 返回2次提交。

Edit: As charsi mentioned, if you are on Windows you will need to put HEAD or commit hash in quotes. 编辑:如charsi所述,如果您在Windows上,则需要将HEAD或用引号引起来。

git reset --soft "HEAD^"
git reset --soft "asdf"

#3楼

Just a note - if you're using ZSH and see the error 请注意-如果您使用的是ZSH并看到错误

zsh: no matches found: HEAD^

You need to escape the ^ 您需要逃脱^

git reset --soft HEAD\^

#4楼

Be careful with that. 小心点。

But you can use the rebase command 但是您可以使用rebase命令

git rebase -i HEAD~2

A vi will open and all you have to do is delete the line with the commit. 将打开一个vi ,您要做的就是删除带有提交的行。 Also can read instructions that were shown in proper edition @ vi . 也可以阅读在正确版本@ vi中显示的说明。 A couple of things can be performed on this mode. 在此模式下可以执行几件事。


#5楼

If you haven't pushed your changes yet use git reset --soft [Hash for one commit] to rollback to a specific commit. 如果尚未推送更改,请使用git reset --soft [Hash for one commit]回滚到特定的提交。 --soft tells git to keep the changes being rolled back (ie, mark the files as modified). --soft告诉git保持回滚更改(即,将文件标记为已修改)。 --hard tells git to delete the changes being rolled back. --hard告诉git删除正在回滚的更改。


#6楼

If you commit to the wrong branch 如果您选择了错误的分支

While on the wrong branch: 在错误的分支上时:

  1. git log -2 gives you hashes of 2 last commits, let's say $prev and $last git log -2为您提供2次最后提交的哈希值,例如$prev$last
  2. git checkout $prev checkout correct commit git checkout $prev签出正确的提交
  3. git checkout -b new-feature-branch creates a new branch for the feature git checkout -b new-feature-branch branch为功能创建一个新分支
  4. git cherry-pick $last patches a branch with your changes git cherry-pick $last用您的更改修补分支

Then you can follow one of the methods suggested above to remove your commit from the first branch. 然后,您可以按照上面建议的方法之一从第一个分支中删除提交。

如何取消提交我在Git中的最后一次提交[重复]相关推荐

  1. 在Git中找到破坏测试的提交

    目录 设想 场景详解 解决方案的详细描述 案例1--手动验证提交 案例2--提交的自动验证 分步指南 案例1--手动验证提交 案例2--提交的自动验证 测试你的知识 额外的Git材料 Git在调试代码 ...

  2. 如何清除Git中的本地工作目录? [重复]

    本文翻译自:How do I clear my local working directory in Git? [duplicate] This question already has an ans ...

  3. git常用命令(克隆、分支、tag标签、推送、日志查看,常用命令总结)、将本地代码第一次提交到远程git仓库、过滤掉不提交的内容

    1.1 克隆Git代码 $ git clone http://xxxx.git --branch master 为自己的写新项目添加git版本控制,进入项目目录执行以下命令: git init . 2 ...

  4. vue提交mutation_为什么Vuex中必须要通过commit提交mutation?

    为什么不能直接调用mutation方法,而是必须得通过commit来提交mutation呢? 谢邀! 网上也有很多人问这个问题.但是这个问题要问作者为什么这么设计? 官方解释道: 更改 Vuex 的 ...

  5. 如何“取消恢复”恢复的Git提交?

    给定使用commit的更改,然后使用revert ,那么撤消该恢复的最佳方法是什么? 理想情况下,这应该使用新的提交来完成,以便不重写历史记录. #1楼 git cherry-pick <ori ...

  6. 如何撤消Git中的最新本地提交?

    我不小心将错误的文件提交给Git ,但是我还没有将提交推送到服务器. 如何撤消本地存储库中的那些提交? #1楼 我想撤消我们共享存储库中的最新五次提交. 我查找了要回滚的修订版ID. 然后我输入以下内 ...

  7. 如何在推送后压缩git中的提交?

    这给出了压缩多个提交的一个很好的解释: http://git-scm.com/book/en/Git-Branching-Rebasing 但它不适用于已被推送的提交. 如何在我的本地和远程回购中压缩 ...

  8. 如何在Git中更改多次提交的作者和提交者名称以及电子邮件?

    我当时正在学校计算机上编写一个简单的脚本,然后将更改提交到Git(在笔驱动器中的一个回购中,该回购是从家里的计算机中克隆的). 经过几次提交后,我意识到我正在以root用户身份提交东西. 有什么办法可 ...

  9. 彻底删除git中的较大文件(包括历史提交记录)

    场景 适用于从一个git项目中,将体积较大的资源彻底从git中删除,包括历史提交记录. 如果仅仅在目录中删除一个文件是不够的,只要在提交记录中有这个文件,那么 .git 中就会有这个文件的信息. 用 ...

最新文章

  1. python图标-python实现的简版iconv
  2. layui select下拉框改变之 change 监听事件
  3. ITK:获取PointSet的边界框
  4. 【QGIS入门实战精品教程】2.1:初识QGIS软件
  5. Elasticsearch -- Java High Level REST Client (RestHighLevelClient) 使用说明文档
  6. 学习索引结构的一些案例——Jeff Dean在SystemML会议上发布的论文(下)
  7. 校招刷题---java选择题笔记05
  8. 代码英雄之云间战争:寡头时代,路在何方?
  9. 嵊州D1T1 总统先生,一路走好!
  10. 有刷直流电机和无刷直流电机有什么区别?
  11. 标志Logo设计的起源和艺术特点
  12. 云主机跟VPS的4大区别
  13. 解决Thinkpad E450 外接显示器后没有声音的问题
  14. 新手必看--test link使用步骤
  15. 手机翻书效果html,移动端实现翻书效果
  16. 服务器账号登录失败怎么回事,远程连接云服务器登录失败是什么原因
  17. 1.layer弹窗在使用时,原本隐藏的div,在打开弹窗后竟然显示出来了
  18. 浙大博士求助:华为总包50万,oppo约70万,选谁?
  19. [学习] 08 提高背书速度新玩法
  20. 图像分类数据集 (INRIA)

热门文章

  1. 12.allegro环境设置[原创]
  2. 用aspnetpager实现datalist分页
  3. 自适应自旋锁--吞吐量和延迟以及管理开销的折中
  4. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
  5. Hyper-V 2012 R2 故障转移群集之建立域控(AD DS)与加入域
  6. ABP理论学习之NHibernate集成
  7. hdu 1728 逃离迷宫(dFS+优先队列)
  8. VMware Workstation无法提供该虚拟机所需的所有图形功能
  9. SpringBoot之HelloWorld
  10. FTP 中 「quote type b 1」的含义