我无意中修改了之前的承诺。提交应该是单独的,以保存我对特定文件所做的更改的历史记录。
有没有办法撤销最后的承诺?如果我执行类似gitreset-hardHEAD^的操作,第一次提交也会被撤销。
(我还没有推送到任何远程目录)
我无意中修改了之前的承诺。提交应该是单独的,以保存我对特定文件所做的更改的历史记录。
有没有办法撤销最后的承诺?如果我执行类似gitreset-hardHEAD^的操作,第一次提交也会被撤销。
(我还没有推送到任何远程目录)
当前回答
您需要做的是创建一个新的提交,其详细信息与当前HEAD提交相同,但父级与先前版本的HEAD相同。gitreset-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.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
其他回答
简单解决方案解决方案可行:如果您的HEAD提交与远程提交同步。
在本地工作区中再创建一个分支,并使其与远程分支保持同步。Cherry从新创建的分支上执行的分支(gitcommit--modify)中选择HEAD提交。
精心挑选的提交将只包含最新的更改,而不包含旧的更改。您现在可以重命名此提交。
您需要做的是创建一个新的提交,其详细信息与当前HEAD提交相同,但父级与先前版本的HEAD相同。gitreset-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.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
这晚了将近9年,但没有看到这个变体提到完成相同的事情(这是其中一些的组合,类似于顶级答案(https://stackoverflow.com/a/1459264/4642530).
搜索分支上所有分离的标头
git reflog show origin/BRANCH_NAME--date=相对
然后找到SHA1哈希
重置为旧SHA1
git reset—硬SHA1
然后将其向后推。
git推送原点BRANCH_NAME
完成。
这将使您完全恢复到原来的提交状态。
(包括先前覆盖的分离提交头的日期)
如果您将提交推到远程,然后错误地修改了对该提交的更改,这将解决您的问题。在提交之前发出git日志以查找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
使用ref日志:
git branch fixing-things HEAD@{1}
git reset fixing-things
然后,您应该只在工作副本中保存所有以前修改过的更改,然后可以再次提交
要查看以前索引的完整列表,请键入git-relog