⊗gtPmBrCSR 44 of 65 menu

Simple Rebasing of Commits in Git

The Git system provides two ways to make changes when working with branches: merging and rebasing.

The advantage of rebasing is that the commit history of both branches, unlike a merge, will look more linear and easier to understand for team members working on the same project.

In Git, a merge, performed with the merge command, is a three-way merge of the latest changes from the two branches being merged and the most recent snapshot of the parent branch common to the branches being merged. This merges only the final snapshots of the branches' commits, not their entire previous history.

Rebasing involves applying changes from one branch (the current one) on top of another branch (the one being merged into the current one). Let's create a branch test, in which we'll make a new file file.txt, stage it, and commit it:

git branch test git switch test git add file.txt git commit -m "commit from branch test"

Now switch to the test branch and rebase it relative to the master branch master:

git checkout test git rebase master

The rebasing process itself works like this: first, Git takes a snapshot of the parent branch master, which is saved in a temporary file. Then, the current branch test inserts its changes after the most recent commit of the branch master. Now the commit history of the main branch will look like this:

After rebasing, the commit history of the test branch will also look similar to the master branch.

You can then switch to the master branch and do a rewind merge, i.e. master will become master again and include all the changes from the test branch:

git merge test

Create two branches test1 and test2, each with three commits to different files, then merge them with a simple rebase.

enru