git - 从 merge 分支中删除提交

标签 git git-rebase git-revert

我不小心推送了一些大文件的提交,然后又恢复了它。但这会导致任何 pull 此分支的人都在历史记录中获取这些文件,因此我决定删除或压缩这两个提交。然而,一些分支已经 merge 进来。我不知道如何让“git rebase -i”保持分支结构。

历史现在看起来是这样的:

H - new commits
|
G - merge
| \
|  F - commits on another branch
|  |
E  | - some other commits
|  |
D  | - corrected B
|  |
C  | - revert B
|  |
B  | - huge files
| /
A - early commit

可以改成关注吗?

h - new commits
|
g - merge
| \
|  F - commits on another branch
|  |
e  | - some other commits
|  |
d  | - corrected B
| /
A - early commit

最佳答案

是的你可以但你不应该

它会要求你重写历史并替换服务器上已经推送的历史(这需要强制推送并且通常会导致每个人都对你大喊大叫)

但是如果你真的想要那么 git filter-branch 就是你想要使用的,就像在 this SO answer 中一样.所以你会做这样的事情:

git filter-branch --commit-filter '
    if [ "$GIT_COMMIT" = "<your commit to remove here>" ]
    then
        skip_commit "$@";
    else
        git commit-tree "$@";
    fi'  HEAD

还有几个例子here .

关于git - 从 merge 分支中删除提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530965/

相关文章:

git - "Auto packing the repository for optimum performance"是什么意思?

git - 如何将 'git checkout' 别名为 'git co'

git - 在重新定位许多提交时如何防止许多 git 冲突?

git - 从巨大的 Git 存储库中删除历史记录

git - 这是撤消 git 东西的好策略吗?

gitignore 多个子文件夹中的给定文件夹

git - 从 git 存储库中 pull 所有未获取的更改

git - rebase 废墟 merge

git - Azure Devops Repos - 恢复到以前的提交,就像最近的提交从未存在过一样

git - 撤消 Git 中已推送到远程仓库的特定提交