Git rebase is a command used to reapply a series of commits on a different branch or timeline. It helps you to integrate changes from one branch into another by reapplying the changes one-by-one. Unlike merge, rebase rewrites the entire branch history to make it look like a straight line.
Here’s an example of how to use Git rebase:
- First, switch to the branch you want to apply the changes to:
git checkout <branch-name>
- Then, run the rebase command:
git rebase <branch-to-rebase-from>
- If there are any conflicts, resolve them by editing the conflicting files and running
git add <file-name>to mark them as resolved.
- Continue the rebase by running
git rebase --continue
- Finally, push the updated branch to the remote repository:
git push -f <remote> <branch-name>
-f flag is used to force-push the changes to the remote repository, as rebasing changes the branch history.
Interactive rebasing is a Git feature that allows you to alter the entire branch history, including squashing multiple commits into one, reordering commits, changing commit messages, and more. It is performed using the “git rebase” command with the “–interactive” or “-i” option.
Git Interactive Rebase
Here is an example of how you can perform an interactive rebase in Git:
- Fetch the latest changes from the upstream repository:
git fetch origin
- Switch to the branch you want to rebase:
git checkout my-branch
- Start the interactive rebase:
git rebase -i HEAD~3(This will allow you to rebase the last 3 commits)
- A text editor will open showing a list of the last 3 commits in the branch. The lines starting with “pick” represent the actual commits, and the actions you can perform on them include “pick”, “reword”, “edit”, “squash”, “fixup”.
- You can reorder the lines to change the order of the commits or change the action for each commit, for example, if you want to change the commit message of a particular commit, you can change the “pick” line to “reword”.
- Once you have made the changes, save and close the editor. Git will then perform the rebase operation according to the actions you have specified. If there are any merge conflicts, Git will prompt you to resolve them.
- After you have resolved the merge conflicts, you can use the
git rebase --continuecommand to continue the rebase operation.
Note: Interactive rebasing should be used with caution as it can rewrite the branch history and make it difficult to collaborate with others. It is recommended to use it only in local branches, not in branches that have been shared with others.
Git Merge vs. Rebase
Git Merge and Rebase are two different methods of integrating changes from one branch into another branch in Git.
- Merging combines multiple branches into a single branch by creating a new merge commit that includes changes from both branches.
- It’s a simple and straightforward method for integrating changes.
- Git merge will preserve the entire history of each branch.
- Use git merge when you want to preserve all the commit messages, branching history and time-stamps of the original branches.
Example: Let’s say you have two branches, master and feature_branch, and you want to merge feature_branch into master. To do this, you can run the following command:
$ git checkout master $ git merge feature_branch
- Rebase changes the base of a branch, making it appear as though the branch was created from a different starting point.
- Rebase replays all the changes from the current branch on top of the target branch.
- Rebase is useful when you want to keep a linear history and make it look like a branch was created from a more recent state.
- Rebase can make it more difficult to understand the history of the repository.
Example: Let’s say you have two branches, master and feature_branch, and you want to rebase feature_branch onto master. To do this, you can run the following command:
$ git checkout feature_branch $ git rebase master
In conclusion, use Git Merge when you want to preserve history, and use Git Rebase when you want to keep a linear history and a cleaner commit tree.