Rebasing a branch in Git is a way to move the entirety of a branch to another point in the tree. The simplest example is moving a branch further up in the tree. Say we have a branch that diverged from the master branch at point A:
/o-----o---o--o-----o--------- branch --o-o--A--o---o---o---o----o--o-o-o--- master
When you rebase you can move it like this:
/o-----o---o--o-----o------ branch --o-o--A--o---o---o---o----o--o-o-o master
To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type
git rebase master (where master is the branch you want to rebase on).
It is also possible to rebase on a different branch, so that for example a branch that was based on another branch (let’s call it feature) is rebased on master:
/---o-o branch /---o-o-o-o---o--o------ feature ----o--o-o-A----o---o--o-o-o--o--o- master
git rebase master branch or
git rebase master when you have checked out the branch, you’ll get:
/---o-o-o-o---o--o------ feature ----o--o-o-A----o---o--o-o-o--o--o- master \---o-o branch
Git rebase interactive in the console
git rebase in the console with a list of commits you can choose, edit or drop in the rebase:
git rebase -i HEAD~5with the last number being any number of commits from the most recent backwards you want to review.
- In vim, press
ito start editing the test.
- On the left hand side you can overwrite the
pickwith one of the commands below. If you want to squash a commit into a previous one and discard the commit message, enter
fin the place of the
pickof the commit.
pick 452b159 <message for this commit> pick 7fd4192 <message for this commit> pick c1af3e5 <message for this commit> pick 5f5e8d3 <message for this commit> pick 5186a9f <message for this commit> # Rebase 0617e63..5186a9f onto 0617e63 (30 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but stop to edit the commit message. # e, edit = use commit, but stop to amend or add commit. # s, squash = use commit, meld into previous commit and stop to edit the commit message. # f, fixup = like "squash", but discard this commit's log message thus doesn't stop. # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # 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
:wqto save and quit.
- When rebase is stopped, make the necessary adjustments, then use
git rebase --continueuntil rebase is successful
- If it rebases successfully then you need to force push your changes with
git push -fto add the rebased version to your github repo.
- If there is a merge conflict, there are a number of ways to fix this, including following the suggestions in this guide. One way is to open the files in a text editor and delete the parts of the code you do not want. Then use
git add <file name>followed by
git rebase --continue. You can skip over the conflicted commit by entering
git rebase --skip, exit the git rebase by entering
git rebase --abortin your console.
Points to note
Git Rebase is a very powerful feature. That being said, it is risky as well if it is not used in the right way.
Git rebase alters the commit history. So use it with care. If Rebasing is done in the remote repository, then it can create a lot of issues when other developers try to pull the latest code changes from the remote repository.
As much as possible try using Git Rebase only to rebase a local git repository.