Git 从拉取请求中删除不需要的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8939808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:46:59  来源:igfitidea点击:

Git Remove unwanted commits from a Pull Request

gitgithubpull-request

提问by user1152142

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 保持同步?

回答by Simon Stender Boisen

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. Locate the hash of your last good commit using git log.

由于您将更改历史记录(通过重置头部),您需要使用 --force 标志推送。使用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.

这会将您的头部更改为该 sha 并保留自上次良好提交以来文件中的更改,您的索引也将被重置。

Now you can change your code and do the commits you want. But you have to do git push --forcesince you changed the history of the repository. 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.

现在您可以更改您的代码并执行您想要的提交。但是您必须这样做,git push --force因为您更改了存储库的历史记录。这意味着分叉您的存储库的任何人都将无法再从您那里提取更改。但是您将能够向上游发出拉取请求。

回答by vanitha

If you are using git gui, Goto git gui and visualize your branch history. (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. If you create a pull request now, it will have only the new commit after the branch reset.

如果您正在使用 git gui,请转到 git gui 并可视化您的分支历史记录。(在进行下一步之前,备份要推送的本地更改)右键单击分支要重置为 master 的点,然后单击重置。重置后,在命令行中输入 git push -f 现在在分支中进行必要的更改,再次提交 n push 。如果您现在创建拉取请求,则在分支重置后它将只有新的提交。