如何撤消“git commit --amend”而不是“git commit”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459150/
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 07:05:46  来源:igfitidea点击:

How to undo "git commit --amend" done instead of "git commit"

gitcommitundoamend

提问by Jesper R?nn-Jensen

I accidentally amended my previous commit. The commit should have been separate to keep history of the changes I made to a particular file.

我不小心修改了我之前的提交。提交应该是分开的,以保留我对特定文件所做更改的历史记录。

Is there a way to undo that last commit? If I do something like git reset --hard HEAD^, the first commit also is undone.

有没有办法撤消最后一次提交?如果我做类似的事情git reset --hard HEAD^,第一次提交也会被撤销。

(I have not yet pushed to any remote directories)

(我还没有推送到任何远程目录)

回答by CB Bailey

What you need to do is to create a new commit with the same details as the current HEADcommit, but with the parent as the previous version of HEAD. git reset --softwill move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.

您需要做的是创建一个新提交,其详细信息与当前HEAD提交相同,但父提交与HEAD. git reset --soft将移动分支指针,以便下一次提交发生在与当前分支头现在不同的提交之上。

# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before 
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}

# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}

回答by knittl

use the ref-log:

使用参考日志

git branch fixing-things HEAD@{1}
git reset fixing-things

you should then have all your previously amended changes only in your working copy and can commit again

然后,您应该只在您的工作副本中拥有所有先前修改的更改,并且可以再次提交

to see a full list of previous indices type git reflog

查看以前索引类型的完整列表 git reflog

回答by kenorb

Find your amended commits by:

通过以下方式查找修改后的提交:

git log --reflog

Note: You may add --patchto see the body of the commits for clarity. Same as git reflog.

注意:--patch为了清楚起见,您可以添加以查看提交的正文。一样git reflog

then reset your HEAD to any previous commit at the point it was fine by:

然后通过以下方式将您的 HEAD 重置为之前的任何提交:

git reset SHA1 --hard

Note: ReplaceSHA1 with your real commit hash. Also note that this command will loseany uncommitted changes, so you may stash them before. Alternatively, use --softinstead to retain the latest changesand then commit them.

注意:SHA1替换为您的真实提交哈希。另请注意,此命令将丢失任何未提交的更改,因此您可以先将它们隐藏起来。或者,使用--soft代替保留最新的更改,然后提交它们。

Then cherry-pick the other commit that you need on top of it:

然后挑选你需要的另一个提交:

git cherry-pick SHA1

回答by Arkaitz Jimenez

You can always split a commit, From the manual

您可以随时拆分提交,来自手册

  • Start an interactive rebase with git rebase -i commit^, where commit is the commit you want to split. In fact, any commit range will do, as long as it contains that commit.
  • Mark the commit you want to split with the action "edit".
  • When it comes to editing that commit, execute git reset HEAD^. The effect is that the HEAD is rewound by one, and the index follows suit. However, the working tree stays the same.
  • Now add the changes to the index that you want to have in the first commit. You can use git add (possibly interactively) or git-gui (or both) to do that.
  • Commit the now-current index with whatever commit message is appropriate now.
  • Repeat the last two steps until your working tree is clean.
  • Continue the rebase with git rebase --continue.
  • 使用 git rebase -i commit^ 启动交互式 rebase,其中 commit 是您要拆分的提交。事实上,任何提交范围都可以,只要它包含该提交。
  • 使用“编辑”操作标记要拆分的提交。
  • 在编辑该提交时,执行 git reset HEAD^。效果是HEAD倒退1,索引也跟着。但是,工作树保持不变。
  • 现在将更改添加到您希望在第一次提交中拥有的索引。您可以使用 git add (可能是交互式的)或 git-gui (或两者)来做到这一点。
  • 使用现在合适的任何提交消息提交现在当前的索引。
  • 重复最后两个步骤,直到您的工作树干净为止。
  • 使用 git rebase --continue 继续 rebase。

回答by Justin Schulz

Possibly worth noting that if you're still in your editor with the commit message, you can delete the commit message and it will abort the git commit --amendcommand.

可能值得注意的是,如果您仍然在带有提交消息的编辑器中,您可以删除提交消息,它将中止git commit --amend命令。

回答by utzcoz

Maybe can use git reflogto get two commit before amend and after amend.

也许可以用来git reflog在修改前和修改后获得两次提交。

Then use git diff before_commit_id after_commit_id > d.diffto get diff between before amend and after amend.

然后用于git diff before_commit_id after_commit_id > d.diff获取修改前和修改后的差异。

Next use git checkout before_commit_idto back to before commit

下一次使用git checkout before_commit_id回到提交前

And last use git apply d.diffto apply the real change you did.

最后一次用于git apply d.diff应用您所做的实际更改。

That solves my problem.

那解决了我的问题。

回答by David Sopko

If you have pushed the commit to remote and then erroneously amended changes to that commit this will fix your problem. Issue a git logto find the SHA before the commit. (this assumes remote is named origin). Now issue these command using that SHA.

如果您已将提交推送到远程,然后错误地修改了对该提交的更改,这将解决您的问题。git log在提交之前发出 a以查找 SHA。(这假设 remote 名为 origin)。现在使用该 SHA 发出这些命令。

git reset --soft <SHA BEFORE THE AMMEND>
#you now see all the changes in the commit and the amend undone

#save ALL the changes to the stash
git stash

git pull origin <your-branch> --ff-only
#if you issue git log you can see that you have the commit you didn't want to amend

git stash pop
#git status reveals only the changes you incorrectly amended

#now you can create your new unamended commit

回答by Pratik

You can do below to undo your git commit —amend

您可以执行以下操作来撤消您的 git commit —amend

  1. git reset --soft HEAD^
  2. git checkout files_from_old_commit_on_branch
  3. git pull origin your_branch_name
  1. git reset --soft HEAD^
  2. git checkout files_from_old_commit_on_branch
  3. git pull origin your_branch_name

====================================

====================================

Now your changes are as per previous. So you are done with the undo for git commit —amend

现在您的更改与以前一样。所以你完成了撤销git commit —amend

Now you can do git push origin <your_branch_name>, to push to the branch.

现在您可以执行git push origin <your_branch_name>, 推送到分支。

回答by garrettmac

Almost 9 years late to this but didn't see this variation mentioned accomplishing the same thing (it's kind of a combination of a few of these, similar to to top answer (https://stackoverflow.com/a/1459264/4642530).

差不多晚了 9 年,但没有看到提到的这种变化可以完成同样的事情(它是其中一些的组合,类似于顶级答案(https://stackoverflow.com/a/1459264/4642530) .

Search all detached heads on branch

搜索分支上的所有分离头

git reflog show origin/BRANCH_NAME --date=relative

git reflog show origin/BRANCH_NAME --date=relative

Then find the SHA1 hash

然后找到SHA1哈希

Reset to old SHA1

重置为旧的 SHA1

git reset --hard SHA1

git reset --hard SHA1

Then push it back up.

然后再向上推。

git push origin BRANCH_NAME

git push origin BRANCH_NAME

Done.

完毕。

This will revert you back to the old commit entirely.

这将使您完全恢复到旧提交。

(Including the date of the prior overwritten detached commit head)

(包括之前覆盖的分离提交头的日期)

回答by Priyanshu Chauhan

  1. Checkout to temporary branch with last commit

    git branch temp HEAD@{1}

  2. Reset last commit

    git reset temp

  3. Now, you'll have all files your commit as well as previous commit. Check status of all the files.

    git status

  4. Reset your commit files from git stage.

    git reset myfile1.js(so on)

  5. Reattach this commit

    git commit -C HEAD@{1}

  6. Add and commit your files to new commit.

  1. 使用最后一次提交结帐到临时分支

    git branch temp HEAD@{1}

  2. 重置上次提交

    git reset temp

  3. 现在,您将拥有提交和先前提交的所有文件。检查所有文件的状态。

    git status

  4. 从 git stage 重置您的提交文件。

    git reset myfile1.js(很快)

  5. 重新附加此提交

    git commit -C HEAD@{1}

  6. 添加文件并将其提交到新提交。