16 | Rewriting History by Rebasing |
Rebasing commits is the one concept in Git that has no counterpart inside the traditional version control world. Using git rebase, you can rewrite the history of a repository in a variety of ways. It is one of the most powerful commands in Git, which makes it one of the most dangerous.
rebase takes a series of commits (normally a branch) and replays them on top of another commit (normally the last commit in another branch). The parent commit changes so all the commit IDs are recalculated. This can cause problems for other developers who have your code because the IDs don’t match up.
There’s a simple rule of thumb with git rebase: use it as much as you want on local commits. Once you’ve shared changes with another developer, the headache is generally not worth the trouble.
git rebase takes a branch (the most common use), a tag, or a commit ID to rebase on top of. You can also pass the --rebase option to git pull, causing it to perform a rebase instead of merging the upstream changes into your local branch.
git rebase requires a clean working tree—that is, a working tree with no modified files. If you have changes that you’re not ready to commit, you can stash them (see Task 26, Temporarily Hiding Changes) until you’re done.
A conflict might arise during the replaying of commits. Like a conflict during a regular merge, a conflict happens when two commits modify the same line of code. git rebase stops when this happens and asks you to fix the conflict (see Task 24, Handling Conflicts) and then continue. You tell Git you’re ready with git rebase --continue.
You can also skip a commit that’s causing a conflict by calling git rebase --skip. That could lead to further conflicts, however. You can abort the rebase too with git rebase --abort.
There’s always a safety net if you need to undo a rebase after it’s completed. Git points ORIG_HEAD at the commit before major changes like git rebase are run. You can use git reset to reset your repository back to that original state.
What To Do... |
For example, rebase your current branch against master:
| prompt> git rebase master |
| First, rewinding head to replay your work on top of it... |
| Applying: simple commit |
You can also rebase against a tag or commit ID. For example, if af0fe21 is the commit ID for master, use this:
| prompt> git rebase af0fe21 |
| ... same as above ... |
| prompt> git reset --hard ORIG_HEAD |
| HEAD is now at e9f9fdc update the README |
Using --hard can be dangerous. Check Task 37, Resetting Staged Changes and Commits for an explanation of its use.
Task 15, Merging Commits Between Branches
Task 24, Handling Conflicts
Task 26, Temporarily Hiding Changes
3.12.136.119