Git Rebase Command
Git Rebase Command
Git is a distributed version control system that helps developers track changes in their codebase. It allows multiple people to collaborate on projects, maintain different versions of code, and merge changes efficiently.
The git rebase
command is used to integrate changes from one branch into another by rewriting commit history. Unlike merging, which creates a new merge commit, rebase moves or combines commits to create a linear project history.
Syntax of Git Rebase
The general syntax of the command is as follows:
$ git rebase <base_branch> $ git rebase -i <commit_hash>
Parameters:
<base_branch>
: Target branch to rebase onto, branch onto which the current branch’s changes will be reapplied.-i
: Starts interactive rebase mode<commit_hash>
: Specific commit to rebase from
Example Command Usage
Basic rebase:
$ git checkout feature_branch $ git rebase main
This moves all feature_branch commits to the tip of the main branch. This command will take all commits from feature-branch
and reapply them on top of the latest main
branch, ensuring a linear commit history.
Interactive rebase:
$ git rebase -i HEAD~3
Allows editing/squashing of the last 3 commits in the current branch.
- Rebase creates a cleaner, linear commit history
- Use it for local branches – avoid rebasing public/shared branches
- Resolve conflicts during rebase before continuing
- After rebasing, you may need to force push:
git push --force
- Rebase changes commit hashes, unlike merge
Difference Between Git Merge and Git Rebase
Let’s look at the differences between Git Merge and Git Rebase commands in this section.
Git rebase
is a powerful command used to integrate changes from one branch into another. Instead of creating a merge commit, rebase moves or “reapplies” commits from the source branch onto the target branch, ensuring a cleaner and more linear commit history. This is particularly useful when working in collaborative environments where maintaining a clean commit log is important.
Git Merge | Git Rebase | |
---|---|---|
Commit History | Maintains the complete history with a merge commit. | Rewrites the commit history to create a linear progression. |
New Commit Creation | Creates a new merge commit. | Moves existing commits on top of the target branch. |
Conflict Resolution | Conflicts are resolved once, during the merge. | Conflicts may need to be resolved for each commit being rebased. |
Branch History | Retains the branching history. | Rewrites history to appear as if changes were made sequentially. |
Usage Scenario | Used when preserving branch history is important. | Used when a clean, linear history is preferred. |
Risk Factor | Lower risk as it does not modify commit history. | Higher risk, especially for shared branches, as it rewrites history. |
Git Tutorials
Git Tutorial on this website can be found at: