Git:Rebase
lastmod 2024-09-30created 2014-02-279 edits
Forward-port local commits to the updated upstream head.
How to use
To rebase onto another branch while working, run something like git rebase origin/master.
CONFLICT
If a conflict occurs during the rebase, a message like the following is printed.
Checking with git status afterwards prints a status message like the following.
Ways to resolve the conflict from here:
- Resolve the conflict,
git addthe code, and continue the rebase withgit rebase --continue. - SKIP the conflict with
git rebase --skip. - Stop the rebase and restore the original branch with
git rebase --abort.
Squashing earlier commits
| WARNING |
| After a rebase, the local repository cannot be returned to its previous state. Think carefully before proceeding. |
Given the following commits:
* 51da922 (HEAD -> develop-2.0) Fix errors in api
* 9cad4de jest init
* 802d313 eslint init
* 3780bdf prettier init
* 4a16ea5 babel init
* bbffc8a webpack-cli init
* 7590a44 yarn init
* 8074314 (origin/develop-2.0) Update api project settings
* 6a79977 Initialize the api module as a typescript project
* 7325a9a vue add electron-builder
Running something like git rebase -i 7590a44 lets you edit the following in vim. (Comments removed)
pick 51da922 Fix errors in api
pick 9cad4de jest init
pick 802d313 eslint init
pick 3780bdf prettier init
pick 4a16ea5 babel init
pick bbffc8a webpack-cli init
pick 7590a44 yarn init
For reference, the comments say:
- p, pick <commit> = use commit
- r, reword <commit> = use commit, but edit the commit message
- e, edit <commit> = use commit, but stop for amending
- s, squash <commit> = use commit, but meld into previous commit
- f, fixup <commit> = like "squash", but discard this commit's log message
- x, exec <command> = run command (the rest of the line) using shell
- b, break = stop here (continue rebase later with 'git rebase --continue')
- d, drop <commit> = remove commit
- l, label <label> = label current HEAD with a name
- t, reset <label> = reset HEAD to a label
- m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
- create a merge commit using the original merge commit's message (or the oneline, if no original merge commit was specified). Use -c <commit> to reword the commit message.
Changing pick to s squashes the commits.
pick 51da922 Fix errors in api
s 9cad4de jest init
s 802d313 eslint init
s 3780bdf prettier init
s 4a16ea5 babel init
s bbffc8a webpack-cli init
s 7590a44 yarn init
Note that if you change every pick to s, the following error is printed.
error: cannot 'squash' without a previous commit
You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
Or you can abort the rebase with 'git rebase --abort'.
The commit to keep (usually the first line, the latest) must be left as pick.
Change Log message
The previous log message can be modified with git commit --amend, but modifying older log messages requires rebase.
Specifying a commit after the given commit displays a list of commits. Find the commit whose comment you want to modify, change pick on that line to edit, then save and quit.
Next, commit with the --amend option and modify the log message.
Finally, continue the rebase with the --continue option.
Abort
To cancel the whole operation during a rebase, enter the following.