简体   繁体   English

删除最后一个Github提交,而不删除本地存储库中的代码

[英]Remove last Github commit, without removing code in local repository

In my unity3d game project, I'm using Atlassian SourceTree for committing code to my Github repository. 在我的unity3d游戏项目中,我使用Atlassian SourceTree将代码提交到我的Github存储库。 The last commit has wrong commit message, and I wanted to change commit message(as I googled, that's impossible). 最后一次提交有错误的提交消息,我想更改提交消息(在我用谷歌搜索时,这是不可能的)。 So the last chance for me is to revert the last commit and make a new one with the correct commit message. 因此,对我而言,最后的机会是还原上一次提交并使用正确的提交消息进行新的提交。 How could this be done using Atlassian SourceTree utility. 如何使用Atlassian SourceTree实用程序完成此操作。

Impossible? 不可能? I think not! 我想不是!

Just open up a cmd, go to your project and write git commit --amend -m "your new message" and then git push --force . 只需打开一个cmd,转到您的项目并编写git commit --amend -m "your new message" ,然后git push --force This will edit the last comment's commit message and then force push the branch to force your edit on history. 这将编辑最后评论的提交消息,然后强行按下分支以对历史进行强行编辑。 (force push is necessary since you're manipulating history and will overwrite/force the remote branch to look like your local branch. Use with care if you're in a project with more people) (强制推送是必要的,因为您要处理历史记录,并且将覆盖/强制远程分支看起来像本地分支。如果您在一个有更多人的项目中,请谨慎使用)

If you already pushed to the remote, I'd advise to forget about it. 如果您已经推送到遥控器,我建议您不要理会它。 It's dangerous to re-write git history that was pushed if you don't know git very well. 如果您不太了解git,则重写重写的git历史记录很危险。

If you didn't push and you didn't create any other commits on the top of it, you can do as the other answer says and amend a new commit with your last commit. 如果您没有推送并且没有在其顶部创建任何其他提交,则可以按照其他答案所述进行操作,并在上一次提交时修改一个新的提交。

To do it in SourceTree, unstage all changes that you may have and press the commit button (like if you were creating an empty commit). 要在SourceTree中执行此操作,请取消暂存所有可能的更改,然后按提交按钮(例如,如果您正在创建一个空的提交)。 Then, in the message tab click in Commit Options and select Amend last commit . 然后,在消息选项卡中,单击Commit Options并选择Amend last commit You should see your former message now, change it and commit. 您现在应该看到以前的消息,进行更改并提交。

Again, don't do it if you pushed this commit to the remote, don't use push --force . 同样,如果将此提交推送到远程,则不要这样做,不要使用push --force

推

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

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