Git Rebasing: Difference between revisions
Line 36: | Line 36: | ||
{{Internal|Git_commit#Apply_Extra_Changes_to_the_Last_Commit|Applying Extra Changes to the Last Commit}} | {{Internal|Git_commit#Apply_Extra_Changes_to_the_Last_Commit|Applying Extra Changes to the Last Commit}} | ||
==Reordering Commits== | ==Reordering Commits== |
Revision as of 23:40, 18 February 2020
External
- https://git-scm.com/book/en/v2/Git-Branching-Rebasing
- https://www.atlassian.com/git/tutorials/merging-vs-rebasing
Internal
Overview
Rebasing is one of the two main ways to integrate changes from a branch into another. The other way is merging.
Rebasing works as follows: the operation goes to the common ancestor of the head branch (the branch you are on) and base branch (the branch you're rebasing into), getting the diff introduced by each head branch commit, saving these diffs into temporary files, resetting the head branch to the same commit as the base branch, and the finally applying each change in turn. Intuitively, is equivalent with shifting and morphing each commit of the head branch, after the common ancestor, in top of the base branch. This modification allows us to merge the head branch into the base branch without having to create a merge commit - the base branch can simply be fast forwarded with the commits from the head branch.
Rebasing rewrites history, by removing the commits that are being rebased and creating new, equivalent ones.
Merging two branches as described above is the main use case for the rebase. However, rebase can also be used when only one branch is involved, to modify, delete or coalesce several logically-related commits into one commit (squash).
Rewriting History
Rebasing removes the commits that are being rebased and creating new, equivalent ones.
Also see:
Practical Use Cases
Squashing Commits
Applying Extra Changes to the Last Commit
Reordering Commits
The order of commits on a branch can be changed by initiating an interactive rebase, and reordering the commits with the help of the editor.
To Link To
Fixing a Merge Broken by Base Branch Rebase
Update the Commit Message of a Specific Commit
Moving a Branch Forward
You are working on a feature branch, named "A", created when the "develop" branch HEAD was commit "ef5". You committed work on the "A" branch, and your commit is "3ba". After a while, you want to apply your changes on the HEAD of "develop", since "develop" has evolved and you want to try
On branch task/of/PLAT-15252 Your branch and 'origin/task/of/PLAT-15252' have diverged, and have 164 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours)
Rebasing at the HEAD of the Base Branch
This procedure takes all commits from the head branch that diverged from the base branch, squashes them into one, and applies to the HEAD of the base branch.
You will be given the chance to squash extraneous commits. Change "pick" into "s" for the commits you want squashed into the previous (above) commits.
You will then be given the chance to edit comments.
If you don't want to squash, you can omit -i.
On branch topic/... Your branch and 'origin/topic/...' have diverged, and have 1 and 2 different commits each, respectively. (use "git pull" to merge the remote branch into yours)
git push --force
You can delete the branch (locally and remotely)
TODO
- https://www.atlassian.com/git/tutorials/merging-vs-rebasing
- “Rebase Commits” section from the O’Reilly book
- Understand merge really well, especially
- What happens if I have several commits on the topic branch?
- Read git rebase --help.
- Understand git pull --rebase.