0

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

Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108

1 Answers1

2
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.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
  • I should have mentioned I have five additional commits since the merge (`0942142`). Does that change something? – Big McLargeHuge Jan 14 '14 at 19:11
  • Yes, but they should be minor adjustments. You could `git reset` back to your safe zone and then `git cherry-pick` the individual commits (again passing `--no-commit` option to simply apply the changes to the current state). – TheBuzzSaw Jan 14 '14 at 19:13
  • Sorry, I am pretty confused. Can you look at the updated log in my post and tell me what exactly I need to do? – Big McLargeHuge Jan 14 '14 at 19:28
  • Updated my answer. I assume you want everything squished back to `3c0392f`. Experiment with the commands I posted. It should put it into a state that works for you. Don't do the `git push` until it all looks right. Use `git log`. – TheBuzzSaw Jan 14 '14 at 19:51
  • I would like to keep the commits `8f8308b..6f4d0c2` if possible. I tried using `git reset --hard HEAD~7`, followed by `git cherry-pick -n 0a1837e..c265825` and `git cherry-pick -m 1 0942142` but now I'm not sure if I need to merge, squash or what. This history rewriting business is complex. – Big McLargeHuge Jan 14 '14 at 20:22
  • If you do cherry picks, you do **not** need the merge anymore. – TheBuzzSaw Jan 14 '14 at 21:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45226/discussion-between-davidkennedy85-and-thebuzzsaw) – Big McLargeHuge Jan 14 '14 at 21:14