Explain rebasing in Git

GitGitHubOpen SourceSoftware & Coding

Rebasing alters a sequence of commits. It moves or relocates a sequence of commits from current branch to the target branch. By default, the commits from the current branch that are not already on the other branch are rebased. Rebasing technique allows us to keep a linear history.

Let us understand from this from the diagram below.

To rebase we need to be in the branch which needs to be rebased into the target. In our scenario, we need to execute the rebase command on the feature branch. After executing the rebase command we will get a linear history.

After executing the rebase command, the commits on feature F1 and F2 are rebased to the master branch, making it appear as if the branch was created from Commit C3 as shown in the below diagram.

Example

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo
$ git init
Initialized empty Git repository in E:/tut_repo/.git/

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello>hello.txt


$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'c1'
[master (root-commit) 46736ad] c1
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git branch feature

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo world>world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git add world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -m 'world'
[feature b95055e] world
1 file changed, 1 insertion(+)
create mode 100644 world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo world again>>world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -am 'F2'
[feature 2d954e6] F2
1 file changed, 1 insertion(+)

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git log --oneline --all --graph
* 2d954e6 (HEAD -> feature) F2
* b95055e world
* 46736ad (master) c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git switch master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello again>>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -am 'c2'
[master c99c97a] c2
1 file changed, 1 insertion(+)

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* c99c97a (HEAD -> master) c2
| * 2d954e6 (feature) F2
| * b95055e world
|/
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git rebase master
Successfully rebased and updated refs/heads/feature.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git log --oneline --all --graph
* 67dfc66 (HEAD -> feature) F2
* c4a8dc7 world
* c99c97a (master) c2
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git checkout master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 67dfc66 (feature) F2
* c4a8dc7 world
* c99c97a (HEAD -> master) c2
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git rebase feature
Successfully rebased and updated refs/heads/master.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 67dfc66 (HEAD -> master, feature) F2
* c4a8dc7 world
* c99c97a c2
* 46736ad c1
raja
Published on 28-Apr-2021 14:18:43
Advertisements