Git Rebase
git rebase takes the changes from one branch and applies them onto another branch, effectively moving the base of the branch to a new starting point. This helps to create a linear and clean project history.
Example: If feature branch A was created at commit-11 and the main branch has since added 3 more commits, rebasing feature branch A will make it look like it was created at commit-14.
Git Rebase vs Git Pull
git pull fetches changes from a remote branch and merges them into your current branch. By default, it performs a merge operation, which may create a merge commit if there are conflicts or differences between the branches.
| Aspect | Git Rebase | Git Pull (with Merge) |
|---|---|---|
| Action | Replays commits from one branch onto another, creating a linear history. | Fetches changes from a remote branch and merges them into the current branch. |
| Use When | You want a clean, linear commit history. | You need to preserve the historical context of changes. |
| Pros | Cleaner history, easier to follow. (For clean, linear history on private/feature branches.) | Maintains commit history, easier to track changes in collaborative environments. |
Git Rebase Steps
- Checkout to your current branch: Ensure you're on the feature branch you want to update.
git checkout your-feature-branch - Ensure your working directory is clean: Make sure you have committed or stashed any changes in your current branch.
- Fetch the latest changes from the remote: This ensures you have the latest updates.
git fetch origin - Rebase your branch with
develop: This applies the changes fromdeveloponto your current branch.git rebase origin/develop - Resolve any conflicts: If there are conflicts, Git will stop and allow you to resolve them. After resolving the conflicts, continue the rebase process (similar to merge).
git add <file-with-conflict> git rebase --continue - Push (or force push) to update the remote feature branch: If your feature branch already has some commits that are conflicting with main's history, you may need to force push the rebased branch (to rewrite the history). To avoid this, rebase every time you push.
or
git push origin your-feature-branchgit push origin your-branch --force
Git Pull Rebase
We can skip steps 3 and 4 from the above steps by using git pull --rebase instead of git fetch origin and git rebase.
Steps 3 and 4 can be replaced by this command:
git pull origin develop --rebase
This will:
- Fetch the latest commits from the remote
developbranch. - Rebase your local branch on top of the updated
developbranch.
Git Rebase Commits Visualization
In both feature branches, rebasing was not done before each push. This causes conflicts with the timelines during rebase and requires a force push. The force push will change the commit times to just after the rebase, resulting in the loss of the original commit times of feature branches.
Before Rebase

After Rebase
