如何从一系列提交中排除特定提交。我的意思是如果我有 5 次提交,而我只想推送 4 次提交。这该怎么做。请帮助解决这个问题。
最佳答案
您需要有一个包含所需提交的新分支。
你可以通过多种方式做到这一点
推荐方案:
git cherry-pick
checkout 一个新分支到您要从中开始的特定 sha-1:
git checkout <origin branch>
git checkout -b <new branch>
# now cherry-pick the desired commits onto the new branch
git cherry-pick commit1 commit2 ... commitN
# now push to remote
git push origin remote <branch name>
其他选项:
git revert
git revert SHA-1
使用 git revert 来撤消您在不需要的提交中所做的更改,结果将是旧代码和新代码的分支,但当前状态将是原始代码
git rebase -i
交互式 rebase 。选择您不想要的提交并将其删除。
# X is the number of commits you wish to squash
git rebase -i HEAD~X
压缩提交后 - 选择 e
进行编辑并放置您想要的代码,添加并提交
git filter-branch
过滤分支可以用来过滤任何你想要的内容。
git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1
关于Git - 排除特定的提交和推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112517/