简体   繁体   English

在Git中撤消本地提交

[英]Undo local commit in Git

I know this question has been asked thousands of time and I tried all solutions and none worked. 我知道这个问题已经被问了数千次了,我尝试了所有解决方案,但都没有成功。

I committed to my local 2-3 files and now I want to undo the commit. 我提交了本地2-3个文件,现在我想撤消提交。 How can I do that: This is also the first ever commit. 我该怎么做:这也是有史以来的第一次提交。

git reset HEAD^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

UPDATE: 更新:

git reset --soft HEAD~1
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

UPDATE 2: 更新2:

git reset --hard HEAD~1
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

90% of the relevant info is already here in other answers, but I think a little clarification is in order: 90%的相关信息已经在其他答案中了,但是我认为需要进行一些澄清:

The things you've tried, that you think don't work, aren't working only because of the special case that this is the only commit in the repo. 您尝试过的事情(您认为不起作用)并不是唯一有效的,因为特殊情况是这是回购中的唯一提交。 For example 例如

git reset HEAD^

means to take the index and current branch (assuming there is a current branch) back to the previous commit. 意味着将索引和当前分支(假设有当前分支)返回到先前的提交。 But there is no previous commit , so git doesn't know what you mean. 但是没有先前的提交 ,因此git不知道您的意思。

There are still several ways to undo the commit. 仍然存在几种撤消提交的方法。 Several people have suggested that deleting the .git directory and rerunning git init might be the simplest. 一些人建议删除.git目录并重新运行git init 可能是最简单的。 This assumes there's nothing of value in the .git directory. 假设.git目录中没有任何价值。 Certainly the database is empty (except for the commit you don't want); 当然,数据库是空的(除了不需要的提交之外); but maybe there's something you care about in the index; 但是也许索引中有您需要关注的事情; or maybe you have something set up with hooks; 或者也许您用挂钩设置了某些东西; or you don't want to re-do configuration, remotes, etc. Whatever. 或者您不想重新进行配置,远程操作等。 There may be reasons why deleting and re- init ing isn't the solution for you. 某些原因可能导致您不适合删除和重新init

The next suggestion, then, would be to use 那么,下一个建议是使用

git commit --amend

for your next commit. 为您的下一次提交。 This will tell git that instead of making a new commit with the current HEAD commit as its parent, it should replace the current HEAD commit with the new commit. 这会告诉git的,与其做一个新的与当前提交HEAD提交作为其母公司,就应更换当前HEAD新提交提交。 The new commit will get the same parent as the current HEAD commit had (which in this case is "none"). 新提交将获得与当前HEAD提交相同的父对象(在本例中为“ none”)。 The current branch ref will move to the new commit, effectively removing the current HEAD commit from history. 当前分支引用将移至新提交,从而有效地从历史记录中删除当前HEAD提交。

But maybe for some reason you really want to fix this now instead of waiting for the next commit. 但也许出于某种原因你真的想立即解决,而不是等待下一次提交这个。 Honestly there's not much reason, but for the sake of argument... Well, you can do that. 老实说,没有太多理由,但是为了争辩……好吧,你可以做到这一点。 It might be something like 可能是这样的

git checkout --detach
git branch -d master
git checkout --orphan master

What we're doing here is going into "detached HEAD " state so that we don't have master checked out. 我们在这里要做的是进入“分离的HEAD ”状态,这样我们就不必再检查master了。 This allows us to delete master , and then recreate it as an "orphan" branch - ie one with no history. 这使我们可以删除master ,然后将其重新创建为“孤立”分支-即没有历史的分支。 This again knocks the bad commit out of the history. 这再次使错误的提交退出了历史。

It may be worth noting that none of these techniques will immediately delete the old commit; 可能值得注意的是,这些技术都不会立即删除旧提交。 but they do ensure that it won't be included in any push, or default log output, etc. The commit will eventually go away, once it rolls off the reflog and the garbage collector catches up. 但它们确实确保不会将其包含在任何推送或默认log输出等中。一旦提交脱离reflog并且垃圾回收器赶上了,提交最终将消失。 If you don't want to wait, there are steps you can take to immediately delete the commit. 如果您不想等待,可以采取一些步骤立即删除提交。 (Since your repo is basically empty, one option there - again the simplest unless it has negative side effects - would be to delete the .git directory and re- init .) (由于您的存储库基本上是空的,因此,一种选择-除非有负面影响,否则也是最简单的选择-将删除.git目录并重新init 。)

if it's your first commit then there is nothing to revert to. 如果这是您的第一次提交,则没有任何内容可恢复。 there is no state that git knows before this commit. 在此提交之前,git没有知道任何状态。

Maybe you can edit the commit with https://www.atlassian.com/git/tutorials/rewriting-history 也许您可以使用https://www.atlassian.com/git/tutorials/rewriting-history编辑提交

Otherwise you can delete the .git Folder in your Directory and start again with git init 否则,您可以删除目录中的.git文件夹,然后以git init重新开始

Since this is first commit best approach is update your index to the state you what to achieve and then 由于这是第一个提交最佳方法,因此将索引更新为要实现的状态,然后

git commit --amend

https://nathanhoad.net/git-amend-your-last-commit https://nathanhoad.net/git-amend-your-last-commit

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

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