- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain rebasing in Git
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
- Related Articles
- Explain squash merging in Git
- Explain cherry picking in Git
- Explain Git collaboration workflow
- Explain how reset command works in Git
- Explain BLOB object and tree object in Git.
- Explain hard reset with an example in Git
- Explain mixed reset with an example in Git
- Explain soft reset with an example in Git
- Difference between Git Fetch and Git Pull
- Why should you use git add command before using git commit command?
- How to tag a commit in git?
- How to compare two branches in Git?
- How do we stash changes in Git?
- What is the short status in Git?
- How to Install Git in Vs Code?

Advertisements