Git: Rebasing tips
Git allows you to apply a rebase instead of a merge, which gives a cleaner set of commits and history. See git-rebase explained briefly here.
Chris and I ran into a few issues with rebasing this past week and I thought I'd share our learnings.
We created a fork of the master branch of jorgej/RapidFTR here: dlbock/RapidFTR and had to move our changes back to jorgej.
Lesson #1
Never do a rebase unless you're ready to push all changes to the master remote/branch that you forked/branched from. So if you've done a rebase, never continue pushing to your branch/fork - because when you have to do a merge or a rebase again in the future you will see the commits that were rebased previously more than once.
Lesson #2
Instead of doing a rebase by trolling through your commits one at the time, you could squash all your commits into one and perform the rebase with just that one commit that you cherry-picked.
Let's say that the last 4 commits from your local repository's HEAD are the ones you've done on top of jorgej/RapidFTR.
git rebase -i HEAD~4
This will output:
pick ca6e97c Fixed bug where it wasnt stripping all non a-z 0-9 chars and also got workng in IE6 pick 5b6964c Fixed server side function to remove non alphanum characters from field name when getting it from display name pick 7e68448 ... and fixed the javascript function pick 15483c6 First of IE fixes. Apparently IE sometimes sends blank accept header which was causing app to go into api mode. Now only does it when accept type is xml or json # Rebase e2d88ea..945647e onto e2d88ea # # 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 # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted.
Pick the commit you want, squash the rest and then save this.
pick ca6e97c Fixed bug where it wasnt stripping all non a-z 0-9 chars and also got workng in IE6 s 5b6964c Fixed server side function to remove non alphanum characters from field name when getting it from display name s 7e68448 ... and fixed the javascript function s 15483c6 First of IE fixes. Apparently IE sometimes sends blank accept header which was causing app to go into api mode. Now only does it when accept type is xml or json
Another editor will pop up to allow you to change the commit message based on the rest of the 4 commits. Once you're done with squashing all 4 commits into one, you're ready to do a rebase to jorgej. Make sure you have set up jorgej as an additional remote repository.
git remote -v jorgej https://github.com/jorgej/RapidFTR.git (fetch) jorgej https://github.com/jorgej/RapidFTR.git (push) origin http://github.com/dlbock/RapidFTR.git (fetch) origin http://github.com/dlbock/RapidFTR.git (push)
To add a remote repository:
git remote add jorgej https://github.com/jorgej/RapidFTR.git
Finally perform the rebase from jorgej:
git rebase jorgej/master