简体   繁体   English

如何恢复初始 git 提交?

[英]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 的初始提交吗?

You just need to delete the branch you are on.你只需要删除你所在的分支。 You can't use git branch -D as this has a safety check against doing this.您不能使用git branch -D因为它有一个安全检查来防止这样做。 You can use update-ref to do this.您可以使用update-ref来执行此操作。

git update-ref -d HEAD

Do not use rm -rf.git or anything like this as this will completely wipe your entire repository including all other branches as well as the branch that you are trying to reset.不要使用rm -rf.git或类似的东西,因为这将完全擦除您的整个存储库,包括所有其他分支以及您尝试重置的分支。

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

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

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.由于您想回到没有历史记录的原始 state,您可能想要删除分支,但其他人可能想要考虑这个小注释。

Under the conditions stipulated in the question:在问题规定的条件下:

  • The commit is the first commit in the repository.提交是存储库中的第一个提交。
  • Which means there have been very few commands executed:这意味着执行的命令很少:
    • a git init ,一个git init
    • presumably some git add operations,大概是一些git add操作,
    • and a git commit ,git commit
    • and that's all!就这样!

If those preconditions are met, then the simplest way to undo the initial commit would be:如果满足这些先决条件,那么撤消初始提交的最简单方法是:

rm -fr .git

from the directory where you did git init .从您git init的目录中。 You can then redo the git init to recreate the Git repository, and redo the additions with whatever changes are sensible that you regretted not making the first time, and redo the initial commit.然后,您可以重做git init以重新创建 Git 存储库,并使用您后悔没有第一次进行的任何合理更改来重做添加,并重做初始提交。

DANGER!危险! This removes the Git repository directory.这将删除 Git 存储库目录。

It removes the Git repository directory permanently and irrecoverably, unless you've got backups somewhere.它会永久且不可恢复地删除 Git 存储库目录,除非您在某处有备份。 Under the preconditions, you've nothing you want to keep in the repository, so you're not losing anything.在前提条件下,您没有想要保留在存储库中的任何内容,因此您不会丢失任何内容。 All the files you added are still available in the working directories, assuming you have not modified them yet and have not deleted them, etc. However, doing this is safe only if you have nothing else in your repository at all.您添加的所有文件在工作目录中仍然可用,假设您尚未修改它们并且尚未删除它们等。但是,只有在您的存储库中没有任何其他文件时,这样做才是安全的。 Under the circumstances described in the question 'commit repository first time — then regret it', it is safe.在“第一次提交存储库 - 然后后悔”问题中描述的情况下,它是安全的。 Very often, though, it is not safe.但是,很多时候,它并不安全。

It's also safe to do this to remove an unwanted cloned repository;这样做也可以安全地删除不需要的克隆存储库; it does no damage to the repository that it was cloned from.它不会损坏从中克隆它的存储库。 It throws away anything you've done in your copy, but doesn't affect the original repository otherwise.它会丢弃您在副本中所做的任何事情,但不会影响原始存储库。

Be careful, but it is safe and effective when the preconditions are met.小心点,但前提条件满足的情况下是安全有效的。

If you've done other things with your repository that you want preserved, then this is not the appropriate technique — your repository no longer meets the preconditions for this to be appropriate.如果您已经对想要保留的存储库进行了其他操作,那么这不是合适的技术——您的存储库不再满足使其合适的先决条件。

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.这是在一个不可公开访问的内部存储库上,如果您的存储库可公开访问,请假设您需要恢复的任何内容已被其他人拉下。

I wonder why "amend" is not suggest and have been crossed out by @damkrat, as amend appears to me as just the right way to resolve the most efficiently the underlying problem of fixing the wrong commit since there is no purpose of having no initial commit.我想知道为什么没有建议“修改”并且已被@damkrat 删除,因为在我看来,修改是最有效地解决修复错误提交的潜在问题的正确方法,因为没有初始的目的犯罪。 As some stressed out you should only modify "public" branch like master if no one has clone your repo...正如一些人强调的那样,如果没有人克隆您的仓库,您应该只修改像 master 这样的“公共”分支......

git add <your different stuff>
git commit --amend --author="author name <author.name@email.com>"-m "new message"

You can't.你不能。 So:所以:

rm -rf .git/
git init
git add -A
git commit -m 'Your new commit message'

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.不要这样做,因为您即将 go 反对 VCS 系统的整个理念,尤其是 git。 The only good method is to create new and delete unneeded branch.唯一好的方法是创建新的并删除不需要的分支。 See git help branch for info.有关信息,请参阅git help branch

For me:为了我:

git update-ref -d HEAD
git rm --cached . -r

First line was as suggested by CB Bailey (not sure why the second line was necessary, but git didn't uncommit the files until I did that - I could tell by running git status before and after the second line above)第一行是CB Bailey 建议的(不知道为什么需要第二行,但 git 直到我这样做才取消提交文件 - 我可以通过在上面第二行之前和之后运行git status来判断)

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

git revert {commit_id}'

Then push it然后推一下

git push origin -f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM