...
- Make sure you don't have any uncommited changes
- Find the commit just before your work started
- An easy way to do this is using the command line git graph specified on our Git page
In this example, if I wanted to squash feature 123's commits, I need to use commit 4a25eee for the rebase
Code Block language bash * 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)
git rebase -i <commit>
- For the example above, <commit> = 4125eee4a25eee
Git will open a shell editor and show you the commits
Code Block language bash 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. #
- 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
Example Feature squash
Code Block language bash 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 ...
- Save and exit the editor.
- Git now brings up a comments editor.
- Delete all non commented lines and write a message like "Feature PLFM-123 work", which will be the commit message for the single commit
- Save and exit
The git graph history will now look like this
Code Block language bash * 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)
- Two things to remember
- If something goes wrong or you chose the wrong range, you can delete everything in the editors to abort the rebase
- 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.
...