简体   繁体   English

git - 放弃先前的提交,强制推送到远程

[英]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.

相关问题 Git推送到远程存储库,丢弃最新提交并替换为本地提交 - Git push to a remote repository, discard the latest commit and replace with local commit 远程提交和推送时放弃本地git更改 - Discard local git changes when remote commit&push Git:丢弃远程分支上的所有更改并推送本地更改而无需强制推送 - Git: Discard all changes on remote branch and push local changes without force push 如果远程在特定的提交上,是否可以仅使用git force push? - Is it possible to only git force push if remote is on a specific commit? git 重置为先前的提交,然后推送 - git reset to previous commit and then push 如何将git head重置为先前的本地提交,然后将该提交推送到我的远程 - How do I reset git head to previous local commit and then push that commit to my remote Git“强制推送”从新的本地仓库到远程,但保持远程提交历史记录 - Git “force push” from new local repo to remote, but keeping remote commit history Git在某次提交后丢弃本地和远程的所有更改 - Git Discard all changes locally and remote after a certain commit git push不会将最新提交推送到远程 - git push does not push latest commit to remote 在Git中,如果不允许您强制推送到分支,如何更改远程提交消息 - In Git how to change the remote commit message if you are not allowed to force-push to a branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM