How can I reorder/combine commits using Git rebase?

git, rebase

Solution

First, re-ordering the `backup` commits.

# Safety, should be a no-op if your gitk snapshot is accurate
git checkout backup

# Temporary branch
git branch backup-save backup

# Move top commit onto 'Fix for #226:
git rebase --onto origin/master HEAD^

# Go back to saved branch's parent (i.e. without the moved commit)
git reset --hard backup-save^

# Rebase onto the moved commit (HEAD@{1} is where HEAD was 1 step
# ago i.e. before the reset.)
git rebase HEAD@{1}

# Don't need the saved branch any more (although you might want
# to keep it for a bit just in case). This deletes it:
git branch -D backup-save

Combine the two commits on twist, ignoring the top commit message.

git checkout twist

git reset --soft HEAD^

# Just re-save the commit message, alternatively to skip the
# editor step do this: git commit --amend -C HEAD
git commit --amend

Merge the `twist` branch into `backup`, remove the twist branch.

git checkout backup
git merge twist
git branch -d twist

Move `master`. There are multiple fancy ways, but this is simplest. I'm assuming that you want `master` to point to the edited `backup` position and not where it originally was.

git checkout master
git reset --hard backup

`remote/origins/master` is the remote tracking branch and tells you where the branch pointer for the `master` branch in the remote repository `origin` is, or rather was when you last fetched, pushed or pulled.

Problem

After quite a few hours playing with rebase, the repo still looks different from what I need: I would like to accomplish the following tasks: [some of which were OK before I started messing with rebase :( ] - Move The top commit ("Removed extraneous...") to before the branch off (Right above "fix for #226"). - Combine the two commits that are in the 'twist/main' branch. "comma" and "Moved loaded..." should be the same commit, and I don't need the commit message of "comma" at all. - Merge the newly combined "Move loaded" commit into the 'backup' branch, and get rid of 'twist'. - Move 'master' to where it now says 'backup'. - What does that "remote/origins/master" tag mean? I realize that this is asking a lot, but please include actual GIT commands. I don't mind reading and trying on my own, but am a bit confused by the results not matching what I would've expected, and I really don't want to accidentally destroy any commits.

Original source