Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I merge two history commits into one and force push?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading