我做了一个拉请求,但之后我在本地对项目进行了一些提交,最终污染了我的拉请求,我试图删除它,但没有任何运气。
我在StackOverflow上找到了一些类似的问题,但我不能应用那里的内容。 这是我在GitHub上的第一个拉请求,所以对我来说这一切是如何工作的有点奇怪。
突出显示的提交是我需要保留并删除所有其他内容的提交。 它变成了历史上的第四次提交因为我做了一些合并的东西。
我的git日志
有人能解释一下发生了什么以及如何解决这个问题吗?
我做了一个拉请求,但之后我在本地对项目进行了一些提交,最终污染了我的拉请求,我试图删除它,但没有任何运气。
我在StackOverflow上找到了一些类似的问题,但我不能应用那里的内容。 这是我在GitHub上的第一个拉请求,所以对我来说这一切是如何工作的有点奇怪。
突出显示的提交是我需要保留并删除所有其他内容的提交。 它变成了历史上的第四次提交因为我做了一些合并的东西。
我的git日志
有人能解释一下发生了什么以及如何解决这个问题吗?
当前回答
如果不小心推送了不必要的文件,并希望将它们从PR中删除
1. git reset <commit ID wherever you want to jump>
2. git restore <file name you want to remove> or git add <all the file
names you want to add to commit>
3. git commit -m “new commit message”
4. git push -f //necessary to push forcefully (if anything is there to pull)
其他回答
从你的分支开火以下命令
git reset --soft origin/master
git commit -m "Detail of Your last commit that you want to see"
git push -f
如果你正在删除一个提交,并且不想保留它的更改,@ferit有一个很好的解决方案。
如果你想把这个commit添加到当前分支,但是作为当前pr的一部分没有意义,你可以做下面的事情:
使用git rebase -i HEAD~n 将要删除的提交交换到底部(最近的)位置 保存并退出 使用git reset HEAD^——soft来取消提交更改,并将它们恢复到阶段性状态。 使用git push——force在不删除提交的情况下更新远程分支。
现在您已经从远程删除了提交,但在本地仍然有更改。
人们不希望看到一个错误的提交和一个恢复提交来撤销错误提交的更改。这玷污了历史。
下面是一种简单的方法,用于删除错误的提交,而不是使用恢复提交来撤销更改。
Git checkout my-pull-request-branch git rebase -i HEAD~n //其中n是你想要包含在交互中的最后一次提交的数量 变基。 对于要丢弃的提交,将pick替换为drop。 保存并退出。 Git push—force
如果不小心推送了不必要的文件,并希望将它们从PR中删除
1. git reset <commit ID wherever you want to jump>
2. git restore <file name you want to remove> or git add <all the file
names you want to add to commit>
3. git commit -m “new commit message”
4. git push -f //necessary to push forcefully (if anything is there to pull)
你有好几种方法可以做到。
这篇文章-阅读关于恢复的部分将详细解释我们想做什么以及如何做。
这里有一个最简单的解决方法:
# Checkout the desired branch
git checkout <branch>
# Undo the desired commit
git revert <commit>
# Update the remote with the undo of the code
# The force is a tricky flag since it will force the push but
# your administrator can block it, so if it's not an option you
# can delete the old branch and push it again
git push origin <branch> --force
revert命令将使用原始提交的undo命令创建一个新的提交。