Git - 排除特定的提交和推送

标签 git git-push git-commit

如何从一系列提交中排除特定提交。我的意思是如果我有 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 进行编辑并放置您想要的代码,添加并提交

enter image description here


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/

相关文章:

git - 删除未推送的 git commit

windows - 如何在 WIndows 8 中将 "git"设置为 PhoneGap 构建的路径

git - 查找推送的大小

git - 如何在本地和远程删除 Git 分支?

git - 推送到 GitHub 时出现 2FA 问题

git-commit - 恢复在主线上意外提交的更改,回到 git 上主存储库中的内容

源代码行的 git 历史

git push --atomic - 没有失败

git - 运行 git commit 时与编辑器相关的错误?

git - 在 Jenkins 电子邮件通知中获取多行提交消息