mauriblint
mauriblint

Reputation: 1707

Git revert merge commit and clear history

first of all, I readed a lot about this, but i'm not sure how to resolve, i don't want to screw up production or master branch

this is my situation:

enter image description here

Bassically, now i've in master branch approx 200 commits, i need to transform them into just one commit, to keep clear the history of master branch. Thanks in advance

PD: all this changes/operations/merges are already pushed

Upvotes: 0

Views: 546

Answers (1)

Sajib Khan
Sajib Khan

Reputation: 24136

If I understand you right, that you want to keep only the latest/last commit in master branch history?

If yes, then follow it. It will create a new master with only one commit in git log. Note that your working tree will be the same but master history will be cleaned (keep only the latest commit).

First backup your master branch for safety

$ git branch master.bac       # new branch called master.bac = master

Copy the latest commit-hash of master branch

$ git log   # copy the latest commit (top commit)

Back to one commit and create a new orphan (new branch with no commit history) branch.

$ git checkout HEAD~1                  # back to one commit 
$ git checkout --orphan orphan-branch  

Now take/cherry-pick the latest commit (you copied before) of master into orphan-branch.

$ git cherry-pick <commit-hash>

Now, orphan-branch branch has only one commit in git log. Delete the local master branch

$ git log
$ git branch -D master

Create a new local master branch with the history of orphan-branch history

$ git checkout master
$ git log                  # should show only one commit

Force Push to remote. Note: think twice it will replace remote/master with your local/master

$ git push -f origin HEAD  # replace your remote/master with local/master

Upvotes: 1

Related Questions