| 1. |
How does ‘git rebase’ work? When should you rebase your work instead of a ‘git merge’? |
|
Answer» There are scenarios wherein one would like to merge a quickfix or feature branch with not a huge commit history into another ‘dev’ or ‘uat’ branch and yet maintain a linear history. A non-fast forward ‘GIT merge’ would result in a diverged history. Also when one wants the feature merged commits to be the latest commits; ‘git rebase’ is an appropriate way of merging the two BRANCHES. ‘git rebase’ replays the commits on the current branch and place them over the tip of the rebased branch.Since it replays the commit ids, rebase rewrites commit objects and create a new object id(SHA-1). Word of caution: Do not use it if the history is on release/production branch and being shared on the central server. Limit the rebase on your local repository only to rebase quickfix or feature branches. Steps: Say there is a ‘dev’ branch that needs a quick feature to be added along with the TEST cases from ‘uat’ branch.
Develop the new feature and make commits in ‘new-feature’ branch. [dev ] $git CHECKOUT -b new-feature [new-feature ] $ git add lib/commonLibrary.sh && git commit -m “Add commonLibrary file” Divya1@Divya:rebase_project [new-feature] $git add lib/commonLibrary.sh && git commit -m 'Add commonLibrary file'Divya1@Divya:rebase_project [new-feature] $git add feature1.txt && git commit -m 'Add feature1.txt' Divya1@Divya:rebase_project [new-feature] $git add feature2.txt && git commit -m 'Add feature2.txt'
this will result in linear history with ‘new-feature’ results being at the top and ‘dev’ commits being pushed later.
NOTE: ‘dev’ will SHOW a diverged commit history for ‘uat’ merge and a linear history for ‘new-feature’ merge. |
|