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

Ved*_*Ved 6 git git-push git-commit

如何从一系列提交中排除特定提交.我的意思是如果我有5次提交,我只想推送4次提交.这该怎么做.请帮忙解决这个问题.

Cod*_*ard 8

您将需要一个具有所需提交的新分支.

你可以用几种方式做到这一点


推荐解决方案

git cherry-pick

签出一个新分支到您想要开始的特定sha-1:

git checkout <origin branch>
git checkout -b <new branch>

# now cherry-pick the desired commits onto the nee branch
git cherry-pick commit1 commit2 ... commitN

# now push to remote
git push origin remote <branch name>
Run Code Online (Sandbox Code Playgroud)

其他选择:

git revert

git revert SHA-1
Run Code Online (Sandbox Code Playgroud)

使用git revert 撤消在不需要的提交中所做的更改,结果将使用旧代码和新代码进行分支,但当前状态将是原始代码


git rebase -i

交互式变革.选择您不想要的提交并将其删除.

# X is the number of commits you wish to squash
git rebase -i HEAD~X
Run Code Online (Sandbox Code Playgroud)

一旦你压缩你的提交 - 选择e编辑并放置你想要的代码,添加和提交

在此输入图像描述


git filter-branch

过滤器分支可用于过滤所需的任何内容.

git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1
Run Code Online (Sandbox Code Playgroud)