如何恢复初始 git 提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6632191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:19:34  来源:igfitidea点击:

How to revert initial git commit?

git

提问by Chau Chee Yang

I commit to a git repository for the first time; I then regret the commit and want to revert it. I try

我第一次提交到 git 存储库;然后我后悔提交并想恢复它。我试试

# 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 的初始提交吗?

回答by CB Bailey

You just need to delete the branch you are on. You can't use git branch -Das this has a safety check against doing this. You can use update-refto do this.

你只需要删除你所在的分支。您不能使用,git branch -D因为这有针对这样做的安全检查。您可以使用它update-ref来执行此操作。

git update-ref -d HEAD

Do notuse rm -rf .gitor 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或类似这样的,因为这将事情彻底消灭整个仓库在内的所有其他部门以及分公司您尝试重置。

回答by frazras

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

回答by nonsensickle

This question was linked from this blog postand 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 masterbranch
  2. There is no branch called old_masterso I'm free to use that name
  1. 你的master分支上只有一个提交
  2. 没有分支叫,old_master所以我可以自由使用这个名字

It will rename the existing branch to old_masterand create a new, orphaned, branch master(like it is created for new repositories)after which you can freely delete old_master... or not. Up to you.

它会将现有分支重命名为old_master并创建一个新的孤立分支master(就像为新存储库创建的那样),之后您可以自由删除old_master……或不删除。由你决定。

Note: Moving or copying a git branch preserves its reflog (see this code) while deleting and then creating a new branch destroys it. 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.

注意:移动或复制 git 分支会保留其 reflog(请参阅此代码),同时删除然后创建新分支会破坏它。由于您想回到没有历史记录的原始状态,您可能想删除分支,但其他人可能想考虑这个小笔记。

回答by Jonathan Leffler

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,
    • presumably some git addoperations,
    • and a git commit,
    • and that's all!
  • 提交是存储库中的第一个提交。
  • 这意味着执行的命令很少:
    • git init
    • 大概是一些git add操作,
    • git commit,
    • 就这样!

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. You can then redo the git initto 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 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. 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.

它会永久且不可恢复地删除 Git 存储库目录,除非您在某处有备份。在先决条件下,您没有任何想要保留在存储库中的内容,因此您不会丢失任何内容。您添加的所有文件在工作目录中仍然可用,假设您还没有修改它们并且没有删除它们等。但是,只有当您的存储库中没有任何其他文件时,这样做才是安全的。在问题“第一次提交存储库 - 然后后悔”中描述的情况下,这是安全的。但很多时候,它并不安全。

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.

如果你对你的存储库做了其他想要保留的事情,那么这不是合适的技术——你的存储库不再满足合适的先决条件。

回答by ma11hew28

You can't. So:

你不能。所以:

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

回答by edward

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 checkout master

git update-ref -d HEAD

git update-ref -d HEAD

git commit -m "Initial commit

git commit -m "Initial commit

git push -u origin master

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.

这是在一个不可公开访问的内部存储库中,如果您的存储库可公开访问,请假设您需要恢复的任何内容已被其他人删除。

回答by Richard

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. As some stressed out you should only modify "public" branch like master if no one has clone your repo...

我想知道为什么不建议“修正”并被@damkrat 划掉,因为修正在我看来是最有效地解决修复错误提交的潜在问题的正确方法,因为没有初始犯罪。正如一些人强调的那样,如果没有人克隆你的仓库,你应该只修改像 master 一样的“公共”分支......

git add <your different stuff>
git commit --amend --author="author name <[email protected]>"-m "new message"

回答by abdallah Nofal

All what you have to do is to revert the commit.

您所要做的就是还原提交。

git revert {commit_id}'

Then push it

然后推一下

git push origin -f

回答by damkrat

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
git add -A
git commit --amend --no-edit 

或者

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

进而

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. The only good method is to create new and delete unneeded branch. See git help branchfor info.

不要这样做,因为您将违背 VCS 系统的整体理念,尤其是 git。唯一好的方法是创建新的并删除不需要的分支。有关git help branch信息,请参阅。