The git rebase
command enables developers to modify their commit history in a clean and structured manner. It is especially useful in projects where a clean, linear progression of changes is preferred. This guide will focus on how to use the git rebase
command with the HEAD
pointer, including common scenarios like rebasing the last few commits.
Understanding git rebase
Rebasing is a process of moving or combining a sequence of commits to a new base commit. git rebase
can be highly beneficial for simplifying complex commit logs, resolving conflicts out of band, and ensuring that your work does not diverge too far from the work being done by the rest of your team.
Basic concepts
- HEAD: The
HEAD
is a reference to the last commit in the current checked-out branch. When you rebase, you're often moving commits to a new parent commit referenced relative to the HEAD. - Rebasing vs. merging: While merging takes the contents of two branches and integrates them together, rebasing takes the changes that were made in one branch and replays them on another branch, ensuring a clean, linear commit history. Merging on the other hand creates a "merge commit" each time the merge operation is performed, cluttering the history.
How to use git rebase
with HEAD
Simple rebasing
To rebase the current branch’s commits onto a more recent commit, you can use the git rebase
command. For instance, to modify the last commit, which HEAD
points to, you can execute:
git rebase -i HEAD~1
Here, HEAD~1 refers to the commit just before the current HEAD. The -i flag initiates an interactive rebase session, which allows for more detailed control over what happens to each commit during the rebase.
This command opens a text editor with a list of commits starting from the specified commit (HEAD~1
in this case). The editor displays each commit on a new line prefixed with the word pick
, and you can change this prefix to different commands to tell Git how to treat each commit.
This interactive process allows you to reshape the history of your branch, making it possible to clean up commit messages, squash unnecessary commits, or split comprehensive changes into smaller, more manageable chunks. By rebasing with HEAD~1
, you can directly modify the last commit, which is useful for quick amendments or corrections to the most recent changes in your branch.
Rebasing multiple commits
If you need to rebase multiple recent commits, you can use:
git rebase -i HEAD~N
Replace N
with the number of commits you wish to rebase. For example, git rebase -i HEAD~3
starts an interactive rebase for the last three commits.
Process in detail
Start interactive rebase:
Terminalgit rebase -i HEAD~3This command will open your default text editor with a list of the last three commits from the current branch.
Choose Actions for Each Commit: In the text editor, you will see each commit from the oldest to the newest with the word
pick
in front of them. You can changepick
to other commands like:reword
: to change the commit messageedit
: to amend the commitsquash
: to combine this commit with the previous commitfixup
: to combine this commit's changes into the previous commit without keeping this commit's log messagedrop
: to delete the commit
Save and close the editor: After you have made your changes, save and close the editor. Git will then start rebasing the commits as per your instructions.
Handling conflicts during a rebase
If a conflict arises during rebasing, Git will stop and allow you to fix the conflicts manually. Here's how to resolve these conflicts:
- Edit the files listed by Git as having conflicts and resolve the issues.
- Add the resolved files to the staging area using:Terminalgit add <resolved-file>
- Continue the rebase with:Terminalgit rebase --continue
- Abort the rebase if you decide that continuing is not the best course of action:For a more detailed step-by-step walkthrough, see this guide on resolving merge conflicts.Terminalgit rebase --abort
For more detail on rebasing see the official Git documentation.