git rebase -i for specific commits?

Given this

pick <unrelated a>
pick A
pick <unrelated b>
pick B
pick <unrelated c>

you can definitely do

pick <unrelated a>
pick <unrelated b>
pick <unrelated c>
pick A
squash B

Provided that your commit are really unrelated to the other changes, this will work just fine.


Rebasing after a merge is a pain in git.

I generally try to avoid this issue, by not merging others' work, but instead I fetch and rebase

git fetch upstream
git rebase upstream/master

(you can also use git pull --rebase upstream master, but this has a drawback of not updating your remotes/upstream/master tracking branch, which git fetch does; hence I prefer fetch && rebase instead of pull --rebase).

That way, my commits are always on top of everything in master, until my development is ready, then I open a pull request with a clean history without merges I.e. at any point in time, last n commits in the branch that I am working on, are my commits, and I can easily reword, squash them etc.

The easiest way to amend some old commits is git rebase -i, and the param you pass to git rebase -i is a single commit-ish; all the commits newer than the provided commit will be displayed in the interactive rebase screen. I don't know of any way for this screen to just display some commits newer than the param.

The alternative to change old commits is git filter-branch, but it's rather much more complicated to use.

Tags:

Git

Git Rebase