git

How to resolve messy Git project?


I have a project tracked in git but one of the branches was never kept up to date with changes on the master. How best to get back on track?

Current status is that I applied patches last week to the branch to align as much as possible with the master, without losing the changes that we want to bring back to the master. Since then, there have been a few commits on both the master and the branch.

We aren't precious about the history so what would be the best way to merge the latest & greatest commit of the branch onto the master branch without all the history? Is this where cherry-pick comes in handy (I've never used it)?


Solution

  • You don’t need cherry-pick for this mess unless you only want a few commits. Easiest way is just do a squash merge… grab the whole branch’s final state and drop it into master as one clean commit.

    git checkout master
    git pull
    git merge --squash your-branch
    git commit -m "bring feature branch in as one commit"
    git push
    

    Done. History stays tidy. If you really only need one or two commits then cherry-pick them, but if you just want “the latest and greatest” from that branch this squash trick is the way.