# Git Rebase ## Why Rebase? https://medium.com/datadriveninvestor/git-rebase-vs-merge-cc5199edd77c Rebase streamlines the git history. Especially helpful to do an interactive rebase and squash commits before doing a rebase from develop on your feature branch. In affect you are moving your branch to the HEAD of develop and replaying your changes on top. This is why it's helpful to do and interactive rebase first, as you may end up replaying multiple changes to the same line or file multiple times. ## Process HEAD~(number of commits to go back to be included in the rebase) ```bash= git rebase -i HEAD~5 ``` Depending on your configured git editor, this command will open your editor with something like the following: ```bash= 1 git-rebase-todo X pick 89c8bdf SPECNET-12345 - Updated readme pick 8594096 pain points pick 839c432 notes update pick af522e4 deleted last line pick f0a2b79 notes updated with final change. # Rebase cb85503..f0a2b79 onto cb85503 (5 commands) # # 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 # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # NORMAL shared/task/SPECNET-12345-test git-rebase-todo 4% 1:1 "~/mono/mono-repo-summit/.git/rebase-merge/git-rebase-todo" 24L, 811C ``` From here you will want to squash your commits to the first commit. This will turn 5 commits into 1, making replaying your changes during a rebase much easier. ```bash= 1 git-rebase-todo + X pick 89c8bdf SPECNET-12345 - Updated readme squash 8594096 pain points squash 839c432 notes update squash af522e4 deleted last line squash f0a2b79 notes updated with final change. # Rebase cb85503..f0a2b79 onto cb85503 (5 commands) # # 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 # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # NORMAL shared/task/SPECNET-12345-test git-rebase-todo + 20% 5:6 ``` If using Vim ```:wq``` will write your changes and quit. At this point another vim session will open and you will be asked if the commit message is correct. Again unless you want to edit the commit message just ```:wq``` After that the rebase will complete and you will have a squashed commit ready to rebase on develop. ```bash= git rebase -i HEAD~5 [detached HEAD afd106f] SPECNET-12345 - Updated readme Date: Thu Aug 22 12:13:47 2019 -0600 2 files changed, 10 insertions(+) create mode 100644 Pain Points Successfully rebased and updated refs/heads/shared/task/SPECNET-12345-test. ``` To make sure you succesfuly squashed your commits, run ```git log``` ```bash= commit afd106f883091be0a3d643d5992b767769a1e35e (HEAD -> shared/task/SPECNET-12345-test) Author: Trevor Henke <trevor.henke@charter.com> Date: Thu Aug 22 12:13:47 2019 -0600 SPECNET-12345 - Updated readme pain points notes update deleted last line notes updated with final change. commit cb855035f297182881cb432396b2517340c81bf2 (origin/master, origin/HEAD, master) Merge: 22c25e6 b1141d9 Author: Jason Hodges <jason.hodges@charter.com> Date: Thu Aug 8 15:02:05 2019 +0000 Merge branch 'mobile/task/MOBSPAW-initial-setup' into 'master' Initial commit from Mobile : ``` As you can see all of the commits made on your branch are now in one commit and it is the HEAD of your branch. Enter ```q``` and the log will exit. Now comes the rebase on develop. The easiest way one command way to rebase your branch on ablsolute latest develop is to rebase it from remote develop. ```bash= git rebase origin develop ``` You are not rebased! If your branch has already been pushed up to gitlab you will need to force push your newly rebased branch as you have changed the git history for it. ```bash= git push -f ``` ## Warnings If you are working on the same branch as another developer it is not recomended to rebase the feature branch. Since you all have the same branch and history when you rebase you are rewritting the history on a rebase. This causes a lot of problems for others who have a different history checked out. It is recommended to do a merge in this scenario to get the lastest develop changes. Also you will not be able to rebase after doing a merge. Again this will cause major problems with the git history.