Git rebase interactive the last n commits

you should use

git rebase --interactive <sha1>

where <sha1> should not be the sha of the first commit you want to rewrite, but the sha of the commit just before.

if your history looks like this:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit

then you can there are different ways to indicate the commit on which to rebase:

git rebase -i cb85072
git rebase -i 5116d42^

where

  • ^ means the commit just before.
  • -i is just short for --interactive

You can also step back from your last commit by some number of commits. For example, if you want to rebase last 5 commits you can use this command: git rebase -i HEAD~5.


To review and rewrite the last n commits, use:

git rebase -i HEAD~n

p, pick = use commit

f, fixup = like "squash", but discard this commit's log message

https://www.freecodecamp.org/forum/t/how-to-squash-multiple-commits-into-one-with-git-squash/13231

Tags:

Git

Git Rebase