Imagine I have a git branch like this, both on local and remote, HEAD is clean.
develop A - B - C - D - E
That commit B has some sensitive information that I got rid of on commit C. Now I realise people can still see the information though commit history. Is there a way to merge commit B and C into one so people can’t see what happend on commit B? Force push is allowed.
Desired outcome:
develop A - F - D - E
I was thinking using
git reset --soft (hash of B)
git commit -m "message"
git push -f # haven't done this
But it will get rid of all the history after B which I don’t want.
>Solution :
The easiest way is with a rebase interactive.
Something like (already having develop checked out):
git rebase -i A
It will list all commits starting with B. Keep B as pick, then C set it to squash. Save and exit…. and it should be fixed. And history should look like this:
A <- BC <- D <- E <- (develop)
BC is the squash of B and C.
If you want to do it by hand, then:
git checkout B
git restore --source C --worktree --staged -- . # get the contents just like it is in C
git commit --amend --no-edit # or edit if you think the message should be different now
git cherry-pick C..E
# if you like the result....
git checkout -B develop # set the branch over here, we know it exists already, we don't care
And then force-push.