Reordering git commits

The kernel OPW mentors asked me to change one of my patches. To do this, I needed to make the requested change, amend the commit, and generate the new patch. Amending the previous commit is easy, but I had gone ahead and make a couple more. Fortunately, it’s also easy to change the order of recent commits.

Use the command (where N is the number of previous commits to go back to):

git rebase -i HEAD~N

Your text editor will come up with the commits listed, like this (note line 15):

1 pick 0e239d3 convert formal function header to single line comment
2 pick d124307 convert formal get_handshake() function header to single line c omment
3 pick 41799e3 change ft1000-download.c header to /* */ comment style
5 # Rebase 88762f3..41799e3 onto 88762f3
6 #
7 # Commands:
8 # p, pick = use commit
9 # r, reword = use commit, but edit the commit message
10 # e, edit = use commit, but stop for amending
11 # s, squash = use commit, but meld into previous commit
12 # f, fixup = like "squash", but discard this commit's log message
13 # x, exec = run command (the rest of the line) using shell
14 #
15 # These lines can be re-ordered; they are executed from top to bottom.
16 #
17 # If you remove a line here THAT COMMIT WILL BE LOST.
18 #
19 # However, if you remove everything, the rebase will be aborted.
20 #
21 # Note that empty commits are commented out

Reorder the lines signifying the commits (the ones that begin with “pick”) to your liking, then save and close. Git will do its thing, and let you know when it’s done:

Successfully rebased and updated refs/heads/first-patch.

Remember, don’t ever do this if you have pushed your work to another repo! (Since we contribute by emailing patches, we never push, so it’s ok.)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s