简体   繁体   English

Git从Pull Request中删除不需要的提交

[英]Git Remove unwanted commits from a Pull Request

I have started working on a project and I made some unwanted commits which I pushed to origin master. 我已经开始研究一个项目了,我做了一些不需要的提交,我把它推到了原始的主人。 Now, when I try to do a pull request, Github wants to commit all of the previous commits. 现在,当我尝试执行拉取请求时,Github想要提交所有先前的提交。

My question is, how do I remove the unwanted commits and commit the changes that I want to commit so that I am up to date with master? 我的问题是,如何删除不需要的提交并提交我想要提交的更改,以便我与master保持同步?

I assume your origin is your own fork of some project that you want to do a pull request too? 我假设您的起源是您自己的某个项目的分支,您也想要执行拉取请求吗?

Since you will be changing history (by resetting the head) you will need to push with --force flag. 由于您将更改历史记录(通过重置头部),您需要使用--force标志。 Locate the hash of your last good commit using git log . 使用git log找到上次提交的哈希值。

Now run 现在跑

git reset SHA

This will change your head to that sha and preserve the changes in the files since that last good commit, your index will also be reset. 这将改变你的头,并保留自上次良好提交以来文件中的更改,您的索引也将被重置。

Now you can change your code and do the commits you want. 现在,您可以更改代码并执行所需的提交。 But you have to do git push --force since you changed the history of the repository. 但是你必须做git push --force因为你改变了存储库的历史。 This means that anyone who forked your repository won't be able to pull changes from you anymore. 这意味着任何分叉您的存储库的人将无法再从您那里获取更改。 But you will be able to do a pull request to your upstream. 但是您可以向上游执行拉取请求。

If you are using git gui, Goto git gui and visualize your branch history. 如果您正在使用git gui,请转到git gui并可视化您的分支历史记录。 (before doing the next step, take a backup of the local changes that you want to push) Right click to the point where the branch wants to reset to master and click reset. (在执行下一步之前,请备份要推送的本地更改)右键单击分支要重置为主控的点,然后单击“重置”。 After resetting, in command line, type git push -f Now make the necessary changes in the branch , commit n push again. 重置后,在命令行中键入git push -f现在在分支中进行必要的更改,再次提交n push。 If you create a pull request now, it will have only the new commit after the branch reset. 如果现在创建一个pull请求,它将在分支重置后只有新的提交。

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

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