本文翻译自:How to revert initial git commit?

I commit to a git repository for the first time; 我第一次提交了一个git存储库; I then regret the commit and want to revert it. 然后我后悔提交并想要还原它。 I try 我试试

# git reset --hard HEAD~1

I get this message: 我收到这条消息:

fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.

This commit is the first commit of the repository. 此提交是存储库的第一次提交。 Any idea how to undo git's initial commit? 知道如何撤消git的初始提交吗?


#1楼

参考:https://stackoom.com/question/RpKp/如何恢复初始git提交


#2楼

git reset --hard make changes, then do git reset --hard进行更改,然后执行

 git add -A git commit --amend --no-edit 

or 要么

 git add -A git commit --amend -m "commit_message" 

and then 然后

 git push origin master --force 

--force will rewrite that commit you've reseted to in the first step. --force将重写您在第一步中重置的提交。

Don't do this, because you're about to go against the whole idea of VCS systems and git in particular. 不要这样做,因为你不得不违背VCS系统和git的整个想法。 The only good method is to create new and delete unneeded branch. 唯一好的方法是创建新的并删除不需要的分支。 See git help branch for info. 有关信息,请参阅git help branch


#3楼

You can delete the HEAD and restore your repository to a new state, where you can create a new initial commit: 您可以删除HEAD并将存储库还原到新状态,您可以在其中创建新的初始提交:

git update-ref -d HEAD

After you create a new commit, if you have already pushed to remote, you will need to force it to the remote in order to overwrite the previous initial commit: 创建新提交后,如果已经推送到远程,则需要强制它到远程以覆盖先前的初始提交:

git push --force origin

#4楼

This question was linked from this blog post and an alternative solution was proposed for the newer versions of Git: 此问题与此博客文章相关联,并为新版本的Git提出了替代解决方案:

git branch -m master old_master
git checkout --orphan master
git branch -D old_master

This solution assumes that: 该解决方案假定:

  1. You have only one commit on your master branch 您的master分支上只有一个提交
  2. There is no branch called old_master so I'm free to use that name 没有名为old_master分支,所以我可以自由使用该名称

It will rename the existing branch to old_master and create a new, orphaned, branch master (like it is created for new repositories) after which you can freely delete old_master ... or not. 它会将现有分支重命名为old_master并创建一个新的孤立分支master (就像为新存储库创建的那样),之后您可以自由删除old_master ... Up to you. 由你决定。

Note: Moving or copying a git branch preserves its reflog (see this code ) while deleting and then creating a new branch destroys it. 注意:移动或复制git分支会在删除时保留其reflog(请参阅此代码 ),然后创建新分支会将其销毁。 Since you want to get back to the original state with no history you probably want to delete the branch, but others may want to consider this small note. 由于您想要恢复到没有历史记录的原始状态,您可能希望删除分支,但其他人可能想要考虑这个小记。


#5楼

All what you have to do is to revert the commit. 您所要做的就是恢复提交。

git revert {commit_id}'

Then push it 然后推它

git push origin -f

#6楼

I will throw in what worked for me in the end. 我会告诉最终对我有用的东西。 I needed to remove the initial commit on a repository as quarantined data had been misplaced, the commit had already been pushed. 我需要删除存储库上的初始提交,因为隔离的数据被错放了,提交已被推送。

Make sure you are are currently on the right branch. 确保您当前在正确的分支上。

git checkout master

git update-ref -d HEAD

git commit -m "Initial commit

git push -u origin master

This was able to resolve the problem. 这能够解决问题。

Important 重要

This was on an internal repository which was not publicly accessible, if your repository was publicly accessible please assume anything you need to revert has already been pulled down by someone else. 这是在一个无法公开访问的内部存储库中,如果您的存储库是公共可访问的,请假设您需要还原的任何内容已被其他人删除。

如何恢复初始git提交?相关推荐

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

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

  2. 回滚到公共回购中的旧Git提交

    我该如何回滚到git中的特定提交? 有人可以给我的最佳答案是使用git revert X次,直到达到所需的提交. 假设我想恢复为20提交的旧提交,我必须运行20次. 有没有更简单的方法可以做到这一点? ...

  3. 不用copy代码--eclipse使用git提交项目-转

    原文地址:http://blog.csdn.net/u014079773/article/details/51595127 准备工作: 目的:eclipse使用git提交本地项目,提交至远程githu ...

  4. eclipse使用git提交本地项目,提交至远程github上

    准备工作: 目的:eclipse使用git提交本地项目,提交至远程github上 eclipse版本:eclipse4.5  64位 jdk版本:jdk-1.7 64位 项目类型:maven web项 ...

  5. Git 提交规范-Java程序员收藏必备

    你可能会忽略的 Git 提交规范 规范是建立在程序开发者与程序阅读者一个沟通的桥梁,是一个团队必须要严格遵守的约定 --动力节点Java学院 一.为什么需要规范? 无规矩不成方圆,编程也一样. 如果你 ...

  6. Git 提交常见问题总结

    使用git的时候,总会碰到一些奇怪的报错,这里汇总一下git常见的报错信息以及解决方法 文章目录 一.git 提交代码从建立本地仓库到代码提交步骤 二 拉取远程仓库部分文件 三 git 案例分析 3. ...

  7. git 提交两次commit到同一分支,被糅合为一次Marge Request的解决方法:cherry-pick

    简 述: git 提交两次commit到同一分支,且也push到同一个远程仓库的分支,会被糅合为一次Marge Request的解决方法:使用cherry-pick解决 文章目录 同步博文: 需求背景 ...

  8. Git提交指定的文件

    Git提交指定的文件 提交步骤: git提交指定文件,如果此时修改了其他代码,可先提交指定代码,然后忽略不想提交的代码,最后push之后,恢复之前忽略的代码 1 git status 查看修改的所有内 ...

  9. 从 Git 提交中删除文件

    问: 如何从最新提交中删除文件? 答1: huntsbot.com – 程序员副业首选,一站式外包任务.远程工作.创意产品分享订阅平台. 我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上 ...

最新文章

  1. Spring事务管理的底层逻辑—源码解析
  2. Science:语言可能并不是推理能力所必需的
  3. 两台笔记本怎么连接局域网
  4. Hive的基本操作-创建内部表
  5. jquery去掉数组最后一个元素_从数组中删除最后一项
  6. 【Leetcode | 13】56. 合并区间
  7. 10 个最佳的支持触摸操作的 JavaScript 框架
  8. 数据结构-----双向链表笔记
  9. java.lang.IllegalStateException: Only fullscreen activities can request orientation
  10. Scratch青少年编程能力等级测试模拟题
  11. 欧姆龙plc学习笔记(七)
  12. VMware 虚拟机开启Ip地址自动更换解决
  13. 【JavaScript】9.基本引用类型-原始值包装类型
  14. java实现等待程序响应_java – Ning:连接到websocket并等待响应
  15. 飞飞CMS最新版本DC04电脑端网站模板
  16. 调用开放接口(多个系统间的数据传输)
  17. 分组Top N问题(二) - Hadoop MapReduce实现
  18. JavaScrip学习
  19. 如何解决win7资源管理器不断重启和停止工作
  20. 过滤器、vue内置指令

热门文章

  1. css设置background图片的位置实现居中
  2. Vision GUI programming products
  3. 2016-5-31 项目结构调整
  4. ACM学习历程—HDU5668 Circle(数论)
  5. MongoDB Sharding 机制分析
  6. 如何设计自适应屏幕大小的网页(转)
  7. hdu5141 线段树
  8. 路由器下接路由器设置方法(路由器级联)
  9. Media Player 嵌套网页中播放上传视频记录
  10. java中的网络入门2(J2SE入门19)