- What is rebase?
Rebase is one of the way to integrate changes from one branch to another. This helps to synchronize one branch with another branch.
- Usage 1
- Make a feature branch up-to-date with master branch
You are working on feature-branch which was created from `master` branch. Let's say, other developers created couple of pull-request of other features against master branch and got them merged.
Now, your feature-branch is not up-to-date with master or it is "stale".
Now, your feature-branch is not up-to-date with master or it is "stale".
So, to create a pull-request and to make it conflict-free you need to "rebase" your feature-branch with master to bring it up to date with the latest version of master branch.
Another way is to do a merge, but that may causes the branch to contain weird merge commits that make reviewing the pull request much more difficult. So its highly recommended to do a rebase.
- Way to do it?
You can rebase your branch by using following command:
(Assuming you are on your feature-branch. Here in example `my-branch`)
git rebase -i master
When you rebase, git finds the base of your branch and all the commits between that base and HEAD of branch and re-plays those commits on the HEAD of the branch you're rebasing onto i.e. master.
Diagram (1) Before Rebase |
Git actually creates new commits that represent what your changes look like on top of master.
In the diagram (2), these commits are called e′ and f′. Git doesn't erase your previous commits that means commit e and commit f are left untouched. If something goes wrong with the rebase then you can easily go right back to previous version.
Diagram (2) After Rebase |
- Usage 2
- Re-edit previous commits from feature branch
Suppose you have multiple commits in your feature branch and want to create single commit with well descriptive commit message to maintain a clean git history. Then you can do Interactive Rebase which will re-apply all commits, one by one and in order.
You checked your commits by
git log
and found 5 commits in your branch. Now, you can squash them in one commit and/or you can edit commit message if you want.git rebase -i HEAD~5
here `~5` means number of commits we want to rebase.After entering above command editor will open and will look like as below:
pick a0a0abc My first commit message
pick b0a0abc Fix linting errors
pick ca0a0abc Just added unit tests
pick d0a0abc Removed css file
pick e0a0abc Resolve review comments
# Rebase a0a0abc..f0a0abc onto a0a0abc
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Now you can squash all commits into one commit as below
pick a0a0abc My first commit message
squash b0a0abc Fix linting errors
squash ca0a0abc Just added unit tests
squash d0a0abc Removed css file
squash e0a0abc Resolve review comments
Then you close editor window and git will do `squash` for you and will create single commit. And you will be see new editor as below, where you can edit git message:
# This is a combination of 5 commits.
# The first commit's message is:
My first commit message
# This is the 2nd commit message:
Fix linting errors
# This is the 3rd commit message:
Just added unit tests
Removed CSS file
# This is the 4th commit message:
# This is the 5th commit message:
Resolve review comments
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.You can edit first commit's message by changing its text or can keep as it is. But, you might need to delete other commits messages by removing it or simply adding `#` before message.
# This is a combination of 5 commits.
# The first commit's message is:
Added My new feature's updated message
# This is the 2nd commit message:
# Fix linting errors
# This is the 3rd commit message:
# Just added unit tests
# This is the 4th commit message:
# Removed CSS file
Now save and exit the editor , you will see successful rebased commit in your branch.
You just need to push this to your remote repo by doing
git push origin my-branch -f
// you need to push forcefully- Risk in rebase? Rebasing can be dangerous!
After rebasing we need to add `--force` in `git push` command. That means we are overwriting repository’s history.
Rewriting history means replacing existing commits by creating new ones. If other developers work is based on your previous commits, and then you rewrite and force-push your commits, your team members will have to re-merge their work.
- How to revert a rebase?
git rebase --abort
But, In case you had already pushed your branch to repository and after a successful rebase
(git rebase --abort gives "No rebase in progress") then you can easily reset branch using below command:
Some useful git rebase command params are as below:
-i or --interactive | interactive shell mode and this means id allows you to edit the commits which are rebased. |
--continue | continue rebasing operation |
--abort | abort and undo rebase operation by resetting HEAD to the original branch |
--quit | abort the rebase operation but HEAD will not reset back to the original branch |
--skip | restart the rebasing process by skipping the current patch. |
Post a Comment