git
Rebase
git rebase
is a powerful command that allows you to integrate changes from
one branch into another by moving or combining a sequence of commits.
For example, if you have a branch new-feature
that you want to rebase onto the
master
branch, you can do so with the following command:
After running the rebase command, Git will take the commits from new-feature
Resolve Conflicts
There may be conflicts during the rebase process. If this happens, Git will pause. You can resolve the conflicts by editing the files, then rebase again:
-
Find the files with conflicts.
-
Edit the files to resolve the conflicts. The conflicts will be marked in the files.
-
Add the resolved files and rebase again.
Change Multiple Commits
If you want to change multiple commits, you can use the -i
(interactive) flag
with the rebase command:
$ git rebase -i HEAD~3 # Rebase the last 3 commits
$ git rebase -i <commit-hash> # Rebase from a specific commit
Examples
For example, if you have the following commit history:
If you want to change the last three commits, you can run:
It will open an editor with the last three commits listed. You can choose whether to pick or drop the commits:
Action | Description |
---|---|
pick |
Keep the commit as is. |
drop |
Remove the commit from the history. |
The commit b76d5f7
will be dropped. The commit f167f21
will be kept and be rehashed onto the previous commit. The final history will look like this:
Show Log
You can view the commit history of your repository using the git log
command.
Flags
--all
: Show all branches.--graph
: Show a text-based graph of the commit history.--decorate
: Show branch names and tags.--oneline
: Show each commit on a single line.