From 74258ff0f7a216c6dd88cc442a0174f37cd09d34 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Fri, 24 Mar 2023 11:36:55 +0000 Subject: [PATCH] Autosave: 2023-03-24 11:36:55 --- DevOps/Git/Rebasing.md | 90 ++++++++++++++++++++++++++++++ _img/combined-merge-hist.svg | 4 ++ _img/merged-history.svg | 4 ++ _img/normal-merge-again.svg | 4 ++ _img/normal-merge.svg | 4 ++ _img/rebase-tip-chage.svg | 4 ++ _img/rebased-history.svg | 4 ++ _img/single-git-history-rebase.svg | 4 ++ _img/standard-merge.svg | 4 ++ 9 files changed, 122 insertions(+) create mode 100644 DevOps/Git/Rebasing.md create mode 100644 _img/combined-merge-hist.svg create mode 100644 _img/merged-history.svg create mode 100644 _img/normal-merge-again.svg create mode 100644 _img/normal-merge.svg create mode 100644 _img/rebase-tip-chage.svg create mode 100644 _img/rebased-history.svg create mode 100644 _img/single-git-history-rebase.svg create mode 100644 _img/standard-merge.svg diff --git a/DevOps/Git/Rebasing.md b/DevOps/Git/Rebasing.md new file mode 100644 index 0000000..d9c5c0a --- /dev/null +++ b/DevOps/Git/Rebasing.md @@ -0,0 +1,90 @@ +--- +categories: + - DevOps +tags: + - git +--- + +# Rebasing + +Rebasing is a way to integrate changes from one branch into another. In this regarding it is like merging a branch B into another branch A. However rebasing differs from normal merging in the way in which it modifies the Git history. + +In a normal merge of branch B into branch A, Git creates a new commit that combines the changes of branches B and A. This is known as the merge commit and is evident from the following automatic commit message that is generated: + +``` +Merge branch B of github.com:thomasabishop/remote-repository into A +``` + +![](/_img/normal-merge-again.svg) + +In this scenario the merge commit has two or more parent commits each representing the history of the merged branches. The resulting history of A will include the commits of B. Basically the two histories are combined. + +This would give us a history that looks like the following, with different colours for the separate SHAs of each merged branch: + +![](/_img/combined-merge-hist.svg) + +If we were to create a rebase branch of A from B, there would be a new singular history without distinguishing multiple parents that combines the commits of A and B. The rebased branch's commits are recreated with new commit IDs and new timestamps, based on the current state of the branch that you are rebasing onto. This is obviously potentially destructive because it does not preserve the respective branch's separate history. It rewrites history as a continuous stream of commits in a single branch. + +When a rebase is applied, it will put the diverging B commits at the tip of A like so: + +![](/_img/rebase-tip-chage.svg) + +And then rebrand the previous A commits to be continuous with B presenting a flat and linear Git history like the following: + +![](/_img/single-git-history-rebase.svg) + +## Benefits, use-cases + +> The purpose of rebasing is to ensure that the commit history of a project is as linear and simple as possible, by incorporating changes from one branch into another, as if the changes had been made on the other branch all along. + +A common use-case is with feature branches as it makes the features fit more seamlessly with the `main` or `develop` branch. + +It is also a technique that can be used to integreate recent commits without merging. + +## Syntax + +For example if we are working on a feature branch off of `main`, we would run the following from this feature branch: + +``` +git rebase main +``` + +This will move the feature branch to the tip of `main`. + +If this completes successfully, we would then go on to merge the feature with main. + +If conflicts occur, the rebase will halt. You fix the conflicts and then run: + +``` +git rebase --continue +``` + +Or if you don't want to resolve the conflict (not advised): + +``` +git rebase --skip +``` + +Or stop the whole process: + +``` +git rebase --abort +``` + +### Isolate the point at which one branch diverges from another + +This can be useful to check before you create a rebase: + +``` +git merge-base main feature/some-feature + +c33acc84f06fcb94e0e87d9adb240c038da6d71c +``` + +## Golden rule of rebasing + +## Difference from cherry-picking + +The main difference between the two approaches is that [cherry-picking](/DevOps/Git/Cherry_picking_a_branch.md) is a more selective process, where you can pick and choose specific commits that you want to include in another branch. This can be useful when you only want to apply specific changes or fixes from one branch to another, without including all the changes made in the original branch. + +On the other hand, rebasing is a more comprehensive process that can include all the changes from one branch to another, but it modifies the commit history, making it more linear and easier to understand. Unlike rebasing, cherry-picking does not modify the commit history of either branch. diff --git a/_img/combined-merge-hist.svg b/_img/combined-merge-hist.svg new file mode 100644 index 0000000..30e5259 --- /dev/null +++ b/_img/combined-merge-hist.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/_img/merged-history.svg b/_img/merged-history.svg new file mode 100644 index 0000000..2a1cf97 --- /dev/null +++ b/_img/merged-history.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/_img/normal-merge-again.svg b/_img/normal-merge-again.svg new file mode 100644 index 0000000..273eeb4 --- /dev/null +++ b/_img/normal-merge-again.svg @@ -0,0 +1,4 @@ + + + +
A
A
B
B
Text is not SVG - cannot display
\ No newline at end of file diff --git a/_img/normal-merge.svg b/_img/normal-merge.svg new file mode 100644 index 0000000..464c50e --- /dev/null +++ b/_img/normal-merge.svg @@ -0,0 +1,4 @@ + + + +
A
A
B
B
Text is not SVG - cannot display
\ No newline at end of file diff --git a/_img/rebase-tip-chage.svg b/_img/rebase-tip-chage.svg new file mode 100644 index 0000000..94769c3 --- /dev/null +++ b/_img/rebase-tip-chage.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/_img/rebased-history.svg b/_img/rebased-history.svg new file mode 100644 index 0000000..83b0f60 --- /dev/null +++ b/_img/rebased-history.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/_img/single-git-history-rebase.svg b/_img/single-git-history-rebase.svg new file mode 100644 index 0000000..eadbe25 --- /dev/null +++ b/_img/single-git-history-rebase.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/_img/standard-merge.svg b/_img/standard-merge.svg new file mode 100644 index 0000000..6b9e208 --- /dev/null +++ b/_img/standard-merge.svg @@ -0,0 +1,4 @@ + + + +
A
A
B
B
Text is not SVG - cannot display
\ No newline at end of file