Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Make sure you don't have any uncommited changes
  2. Find the commit just before your work started
    1. An easy way to do this is using the command line git graph specified on our Git page
    2. In this example, if I wanted to squash feature 123's commits, I need to use commit 4a25eee for the rebase

      Code Block
      languagebash
      titleGit history
      * 073c346        (develop) feature work 123 - all done (David Burdick)
      * 23e1f7b        feature work 123 - small commit (David Burdick)
      * f4f4f99        feature work 123 - initial feature code (David Burdick)
      *   4a25eee      Some commit from someone else prior to your work (Jay Hodgson)
      |\  
      | * 0437009      blah2 (David Burdick)
      | *   ee3fc4e    blah1 (David Burdick)
  3. git rebase -i <commit>

    1. For the example above, <commit> = 4125eee
  4. Git will open a shell editor and show you the commits

    Code Block
    languagebashtitleInteractive rebase
    pick 073c346 feature work 123 - all done
    pick 23e1f7b feature work 123 - small commit
    pick f4f4f99 feature work 123 - initial feature code
    # Rebase 073c346..f4f4f99 onto 4a25eee
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #                                                                                                                                                                                                                                                                                                   
  5. change "pick" to "squash" (or just "s") to squash commits up into the previous commit. Don't worry about changing the comments, that comes later
    1. Example Feature squash

      Code Block
      languagebashtitleExample Feature Squash
      pick 073c346 feature work 123 - all done
      s 23e1f7b feature work 123 - small commit
      s f4f4f99 feature work 123 - initial feature code
      
      
      # Rebase 073c346..f4f4f99 onto 4a25eee
      #
      # Commands:
      #  p, pick = use commit
      #  r, reword = use commit, but edit the commit message
      ...
  6. Save and exit the editor. 
  7. Git now brings up a comments editor. 
  8. Delete all non commented lines and write a message like "Feature PLFM-123 work", which will be the commit message for the single commit
  9. Save and exit
    1. The git graph history will now look like this

      Code Block
      languagebash
      * b0f5732        Feature PLFM-123 work (David Burdick)
      *   4a25eee      Some commit from someone else prior to your work (Jay Hodgson)
      |\  
      | * 0437009      blah2 (David Burdick)
      | *   ee3fc4e    blah1 (David Burdick)
  10. Two things to remember
    1. If something goes wrong or you chose the wrong range, you can delete everything in the editors to abort the rebase
    2. As with any rebase, DO NOT rebase commits that have been pushed to public repositories (i.e. GitHub). This will mess up anyone who has forked the public repository. However, it is okay to push this type of rebase change up to your own GitHub fork as no one should be forking your forked origin repo. 

...