[英]git - discard previous commit, force push to remote
I will explain my situation first: 我先解释一下我的情况:
I had one file script
in my newly created repository. 我在新创建的存储库中有一个文件script
。
Then I did: 然后我做了:
git add script
git commit -m 'initial commit'
git push -u origin master
..but then I realized I want comment-stripped version of my script
file, so I did: ..但后来我意识到我想要我的script
文件的评论剥离版本,所以我做了:
-- removed comments from my script
using sed
- 使用sed
从我的script
删除了注释
git rm script
git add script-no-comments
which git recognized as file being renamed, which is ok. 哪个git识别为重命名的文件,这没关系。
,then: ,然后:
git commit -am 'comments removed'
git push origin master
So now I have locally and on remote, two commits, first being file with comments, and another one without. 所以现在我有本地和远程,两个提交,第一个是带注释的文件,另一个没有。
My question is: how can I now remove that first commit locally, keeping my last version with no comments, and then I suppose I could do force-push to remote thus overwriting commits there. 我的问题是:我现在如何在本地删除第一个提交,保留我的上一个版本没有注释,然后我想我可以强制推送到远程,从而覆盖那里的提交。 I am not concerned in commit sha
changes, as no one will pull
but me? 我不关心在提交sha
变化,没有人会pull
而我呢?
Also would that remove that comment version for good, because I wouldn't like it to be recoverable? 也会删除该评论版本,因为我不希望它可以恢复?
You can't "remove" the first commit, but you can just squash the two commits you've made into a single commit. 你无法“删除”第一次提交,但你可以将你所做的两次提交压缩成一次提交。
You can accomplish this with a git rebase -i HEAD~2
, but I personally find this process easier: 你可以使用git rebase -i HEAD~2
来完成这个,但我个人觉得这个过程更容易:
Move you branch pointer back to the first commit: 将分支指针移回第一个提交:
git reset --hard HEAD~
Stage the changes from the previous commit: 暂存上一次提交的更改:
git merge --squash HEAD@{1}
Commit your changes, amending them into a single commit. 提交您的更改,将它们修改为单个提交。
git commit --amend
Force-push your new single commit to the server: 强制将新的单个提交推送到服务器:
git push -f
You could do an interactive rebase
, squashing the two commits: 你可以做一个交互式的rebase
,压缩两个提交:
git rebase -i HEAD~2
Follow the instructions in the editor where you can also change the commit message. 按照编辑器中的说明操作,您也可以在其中更改提交消息。
The original commits still exist (but aren't reachable), you can remove them by garbage collection: 原始提交仍然存在(但无法访问),您可以通过垃圾回收删除它们:
git gc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.