Search code examples
gitrebase

How to squash with git rebase -i


I am sort of new to rebasing and definitely haven't squashed commits before.

While checked out to my local branch called 'whatever', I then do a git add and git commit, then rebase.

I guess this is the right way to rebase or at least one way:

git rebase -i development

development is our mainline branch that we rebase our commits on top of. When I do that command I get: enter image description here

I have to scroll down to see the latest commits that I just tried to rebase on top of the development branch:enter image description here

I'm no sure what to do at this point. I've obviously committed my changes, and then did a rebase. Is there a command to squash and what do I squash?? I don't want to squash the entire history of the development branch. Is squash something you do before you rebase? I'm lost a bit.


Solution

  • You should just be able to change the word "pick" to "squash" in order to squash the commit marked "squash" into the preceding commit.

    Git Documentation: Rewriting History

    An example:

    $ git log --oneline
    acb05b3 Some final commit.
    4968dbd Fixing an error in commit df89a81.
    df89a81 Something committed too early.
    c365eab Another commit...
    625889f A commit...
    70f29fd The beginning.
    

    I want to rebase onto 3 commits before the most recent:

    $ git rebase -i HEAD~3
    

    This gives the following text in a text editor:

    pick df89a81 Something committed too early.
    pick 4968dbd Fixing an error in commit df89a81.
    pick acb05b3 Some final commit.
    
    # Rebase c365eab..acb05b3 onto c365eab (3 command(s))
    

    Which I change to this:

    pick df89a81 Something committed too early.
    squash 4968dbd Fixing an error in commit df89a81.
    pick acb05b3 Some final commit.
    
    # Rebase c365eab..acb05b3 onto c365eab (3 command(s))
    

    Upon exiting, I then get another editor containing this:

    # This is a combination of 2 commits.
    # The first commit's message is:
    
    Something committed too early.
    
    # This is the 2nd commit message:
    
    Fixing an error in commit df89a81.
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    

    Which I change to this:

    # This is a combination of 2 commits.
    # The first commit's message is:
    
    This is the squashed commit. It and everything after it get new commit hashes.
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    

    Now if I look at my new history:

    $ git log --oneline
    8792fef Some final commit.
    df775c4 This is the squashed commit. It and everything after it get new commit hashes.
    c365eab Another commit...
    625889f A commit...
    70f29fd The beginning.