Table of Contents
Rebasing
Rebasing the history of the repository allows editing, deleting, reverting, or combining previous commits. The rebase will affect the local repository. Subsequently, the changes must be pushed to the remote repository.
To make an example, the following commit log is supposed to exist:
$ git log --oneline b61ca90 (HEAD -> main) conclusion 7e334bf (origin/main) done some math 47db63e more text ad0ca41 added text 00b3839 first release
To start rebasing the last <n>
commits, use the command:
git rebase -i HEAD~<n>
which will open the text editor with the following output:
$ git rebase -i HEAD~4 pick ad0ca41 added text pick 47db63e more text pick 7e334bf done some math pick b61ca90 conclusion # Rebase 00b3839..b61ca90 onto 00b3839 (4 commands) # # Commands: # 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 [-C | -c] <commit> = like "squash" but keep only the previous # commit's log message, unless -C is used, in which case # keep only this commit's message; -c is same as -C but # opens the editor # 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 # # 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. #
Info: The last <n>
commits are listed from the oldest one (top) to the more recent one (bottom).
Info: The changes have to be done on the editor that Git has just opened, replacing the pick
command with one of those listed.
Tip: If changes have to be done on old commits, leaving the most recent ones unchanged, leave the pick
command on the latter.
Edit commit's message
Change the commit from:
pick <hash> <message>
to:
reword <hash> <message>
Then change the <message>
.
Merge commits
To merge commits <hash-newest>
into the previous one <hash-oldest>
, change from:
pick <hash-oldest> <message-o> pick <hash-newest> <message-n>
to this:
pick <hash-oldest> <message-o> squash <hash-newest> <message-n>
Then edit <message-o>
and <message-n
.
Info: <message-o>
will appear as the main message whereas <message-n
in a new line; eventually it can be deleted.
Pushing rebased code
git push origin main --force