Xiao Xiao - 2 months ago 15
Git Question

git - remove commits from merged branch

I accidentally pushed a commit with some huge files, and then reverted it. But it causes anyone pulling this branch to fetch these files in history, so I decide to remove or squash these two commits. However, some branch has been merged in. I don't know how to make "git rebase -i" keep the branches structure.

The history now looks like:

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


Can I change it to following?

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

Answer

Yes you can but you shouldn't.

It will require you to rewrite the history and replace the already pushed history on the server (which requires force push and most often results in everyone yelling at you).

But if you really want to then git filter-branch is what you want to use, much like in this SO answer. So you would do something like this:

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

There are a few more examples here.