Search code examples
gitbitbucketgit-commitgit-loggit-squash

How do I fix this git commit log?


I have a repository on Bitbucket. I kinda screwed up the commit log when I tried to squash the commits c265825 and 0a1837e:

enter image description here

Trying to squash these commits resulted in 0942142, which is basically empty. What I mean is if I try and view that commit it shows no files changed, which could be normal for a "merge" commit - I don't know.

Obviously I need to fix things like this before pushing to Bitbucket, but how can I fix it after the fact? I'd like to squash 0942142, c265825 and 0a1837e if possible so it looks like a single commit called "Removed unused files."

PS: This is a private repo (for now), so nobody has pulled any of these changes (yet).

Edit: TheBuzzSaw's answer below is correct for squashing every commit since 0a1837e. I wanted to just squash those three commits and keep the rest of the history, however (the screenshot is a mockup - not representative of actual changes). So I had to do this:

git reset --hard HEAD~7
git cherry-pick -n 0a1837e..c265825
git cherry-pick -n -m 1 0942142
git commit -m "Removed unused files"
git cherry-pick 8f8308b
git cherry-pick f7b14f5
git cherry-pick bb90ff9
git cherry-pick 976985d
git cherry-pick 6f4d0c2

Now my commit log is beautified:

enter image description here


Solution

  • git branch area51
    git reset --hard HEAD~7
    git merge --squash --no-commit area51
    git commit -m "My new commit message."
    git push -f
    git branch -D area51
    

    Adjust as necessary.