git: rewrite history: reorder and merge commits
You want to do an interactive rebase.
The first thing I like to do when trying out git commands is to make a backup copy of my branch:
$ git branch my-backup-branch
Let's say that your A1 commit has a hash of 152274b
. Try this:
$ git rebase -i 152274b^
That command will bring up your editor (usually vim) with something like this:
pick 152274b A1pick 14b0838 B1pick 661b993 A2pick a6510db B2pick 557e171 A3pick 85da0e4 B3# Rebase 39a47e4..85da0e4 onto 39a47e4## Commands:# p, pick = use commit# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit## If you remove a line here THAT COMMIT WILL BE LOST.# However, if you remove everything, the rebase will be aborted.#
From here you can change the order of the commits, delete them entirely and even squash them. In this example, you probably want to move and squash like this:
pick 152274b A1squash 661b993 A2squash 557e171 A3pick 14b0838 B1squash a6510db B2squash 85da0e4 B3
Give it a try. If you end up in a state that you didn't want, you can always go back to the state you saved in your backup branch:
$ git reset --hard my-backup-branch