0

What I'm trying to do:

I completed a feature.

git add -A; git commit -m "feature A complete"

Then I realized I missed some things.

git add -A; git commit -m "feature A missed something 1"
git add -A; git commit -m "feature A missed something 2"
git add -A; git commit -m "feature A missed something 3"

Now my history looks like this:

A238ad1 feature A missed something 3
3238adX feature A missed something 2
1238ad7 feature A missed something 1
111AAA2 feature A complete

I want it to look like this:

111AAA2 feature A complete

With all the missed something commits merged into the feature A complete commit.

I know git rebase -i HEAD~4 is the right way to go, but it doesn't actually remove the bad commits to clean up the history.

I noticed in some other articles there's a git commit --fixup command and a git rebase -i --autosquash, but I'm not getting it to work properly. I'm sure this is super easy, I'm just out of it today.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alex Cory
  • 10,635
  • 10
  • 52
  • 62
  • The reason why they don't remove anything is probably because you haven't specified what to do with these commits during interactive rebase. You have to change `pick` which is the default with `squash`. This way it will squash commits on top of each other and you will be given a chance to edit a commit in the end – e.doroskevic Aug 01 '16 at 22:41

1 Answers1

1

In this case use interactive rebase

This can be initiated using

git rebase -i 

followed by number of heads you want to include

git rebase -i HEAD~5

where HEAD~5 indicates last 5 commits

Then you can use further options to squash your commits into one and edit the message. Just instead of pick option write squash instead

squash

So here you see, I change option pick for s which is a shortcut for squash. When I save and close this file, interactive rebase will be initiated and commits which I indicated for squashing will be squashed into 21b4e04 Fixed missing bad practice. After squashing, it will ask you to enter a new commit message. Enter your new message and hit enter and you are done.

Graham
  • 7,431
  • 18
  • 59
  • 84
e.doroskevic
  • 2,129
  • 18
  • 25
  • Hmmmm. I just created a test repo, tested this, and it worked, but it's not working in my other repo. This seems to be the correct answer even though it's not working in my other repo... :/ annoying. – Alex Cory Aug 01 '16 at 22:56
  • How strange, does it give an error message or any output at all? – e.doroskevic Aug 01 '16 at 23:01
  • Basically I think its because I already pushed them. Now I'm trying to figure out how to squash commits between a range of commits because other people have committed after me. i.e. `git rebase -i origin/master~5 origin/master~2`. Is this possible? – Alex Cory Aug 01 '16 at 23:03
  • This is the problem of changing history. It's always full of problems.Are you working in a large team? If yes, I would advise you not to do anything at this point since if you do change your history and then `git push -f` everyone on the project will need to update their copies to reflect on this change. If it's only you or just a small team of people, you could still do it using `git rebase -i HEAD~5`, but you'd need to force push it to your remote `git push -f` – e.doroskevic Aug 01 '16 at 23:07
  • gotcha. Not a huge team. Like 5 people, but it's all good. I just won't do anything. Thanks for the help! :D – Alex Cory Aug 01 '16 at 23:09
  • No worries, glad it was of any use! – e.doroskevic Aug 01 '16 at 23:10