[转载][GitHub]How to undo the most recent commits in Git?

原文地址:https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git/927386#927386

git reset最详尽介绍:https://git-scm.com/docs/git-reset

总结一下:

git reset --hard HEAD~1
git push --force

值得注意的是,这类操作比较比较危险,例如:在你的commit之后别人又提交了新的commit,那在你强制推送之后,那位仁兄的commit也跟着一起消失了。

Undoing a commit is a little scary if you don‘t know how it works. But it‘s actually amazingly easy if you do understand.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master

You want to nuke commit C and never see it again. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Ah, but suppose commit C wasn‘t a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master

You can do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven‘t lost a thing!

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you‘ll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you‘d be redoing the same commit you just had.

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there‘s still a way to get it back. Type git reflog and you‘ll see a list of (partial) commit shas that you‘ve moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You‘ve now resurrected that commit. Commits don‘t actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn‘t mean to get rid of.

原文地址:https://www.cnblogs.com/tuohai666/p/8960959.html

时间: 2024-08-01 17:55:13

[转载][GitHub]How to undo the most recent commits in Git?的相关文章

(转载)github简单使用教程

github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O~.下面是我总结的一些简单使用方法,供初学者参考. 1.注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之后会出现一些仓库的配置信息,这也是一个git

GitHub# fatal: 'origin' does not appear to be a git repository

1. fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 翻译: 致命:"origin"不是一个 git 存储库 致命:无法读取远程存储库 请确定你有正确的访问权力,并且 存储库存在

【转载】MySQL 日志 undo | redo

本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版本并发控制(简称:MVCC). - 事务的原子性(Atomicity)  事务中的所有操作,要么全部完成,要么不做任何操作,不能只做部分操作.如果在执行的过程中发生  了错误,要回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过. - 原理  Undo Log的原理很简单,为了满

[转载]github在线更改mysql表结构工具gh-ost

GitHub正式宣布以开源的方式发布gh-ost:GitHub的MySQL无触发器在线更改表定义工具! gh-ost是GitHub最近几个月开发出来的,目的是解决一个经常碰到的问题:不断变化的产品需求会不断要求更改MySQL表结构.gh-ost通过一种影响小.可控制.可审计.操作简单的方案来改变线上表结构. 在介绍gh-ost之前,我们先了解一下各种现有方案,以及为什么要自己开发一个新工具. 已有的在线修改表定义方案 目前,在线修改表定义的任务主要是通过这三种途径完成的: 在从库上修改表定义,修

转载:Github 简明教程

如果你是一枚Coder,但是你不知道Github,那么我觉的你就不是一个菜鸟级别的Coder,因为你压根不是真正Coder,你只是一个Code搬运工. 但是你如果已经在读这篇文章了,我觉的你已经知道Github了. 正是Github,让社会化编程成为现实. 什么是 Github? github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. Github 由Chris Wanstrath, PJ Hyett 与Tom Preston-

Github安全整理(转载)

刚好这两天对之前github上关注的一些比较有意思的项目进行了一下分类整理,在这里列出来分享给大家,希望能对大家寻找工具或者资源有所帮助. 大部分Repo是关于安全以及Python的,也有一些其他主题的项目,有很多我都没有用过,关于项目的功能概括如果写的有不对的地方,还请大家多多包涵,给予指正. 欢迎组团欢迎一起学习交流!转载请注明出处. 链接:http://blog.leanote.com/post/b0ss/Github%E5%AE%89%E5%85%A8%E6%95%B4%E7%90%86

【转载】从0开始学习 GITHUB 系列之「初识 GITHUB」

转载自http://stormzhang.com 1. 写在前面 我一直认为 GitHub 是程序员必备技能,程序员应该没有不知道 GitHub 的才对,没想到这两天留言里给我留言最多的就是想让我写关于 GitHub 的教程,说看了不少资料还是一头雾水,我转念一想,我当初接触 GitHub 也大概工作了一年多才开始学习使用,我读者里很多是初学者,而且还有很多是在校大学生,所以不会用 GitHub 也就不奇怪了,所以我觉得写一写关于 GitHub 的教程就非常有必要了! 2. 为什么还要造轮子 很

<转载>在Sublime Text 2/3 中使用Git插件连接GitHub

原文地址:<http://www.cnblogs.com/BobGo/p/5998339.html> 工欲善其事,必先利其器." 这是古人的教诲,也是一个高效率的工程师需要遵循的法则之一.从大学开始写java使用了JBuilder,Eclipse,后来写PHP用了Zend,写JavaScript的时候用了Aptana.这些都是非常优秀的IDE,在各自的语言和应用开发环境中给程序员们提供了极大的便利.而作为一个合格的程序员,也应该懂得选择合适的IDE针对不同的应用不同的语言进行开发.

【转载】GitHub详细教程

1 Git详细教程 1.1 Git简介 1.1.1 Git是何方神圣? Git是用C语言开发的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态).另一个状态可以是不同的文件,也可以是不同的文件内容.举个例子,你可以将文件集合转换到两天之前的状态,或者你可以在生产代码和实验性质的代码之间进行切换.文件集合往往被称作是“源代码”.在一个分布版本控制系统中,每个人都有一份完整的源代码(包括源代码所有的历史记录信息),而且可以对这个本地的数据进行操