EpsilonVector
EpsilonVector

Reputation: 4053

How do you git format-patch between head and commit head - x?

Sometimes I commit my changes and then suddenly remember that I forgot to add some comment or do some simple formatting, and having done that I do yet another commit. When it comes time to format a patch, I don't want that "small changes" commit to become a separate patch. I want to take the last two commits and merge them to one patch as if they were a diff between current state and before I started committing.

Googling the issue didn't help. How do I do this?

Upvotes: 0

Views: 301

Answers (2)

Mark Longair
Mark Longair

Reputation: 467311

The easiest way to do this is probably to use interactive rebase to squash together those two commits. If the incomplete commit was A and the subsequent small correction to that was commit B, giving the following commit graph:

 ---- A --- B (master)

You can do:

 git rebase -i A^

This will pop up a text editor with instructions on how to edit this file in order to reorder, remove, edit or squash together commits in the history. (In this case, the history is just everything since the parent of A, which is only A and B.)

You can change pick to squash t the beginning of the line corresponding to commit B, save the file, exit the editor, and the history master will be rewritten so that they are a single commit.

Obviously, since this is rewriting history, you should avoid this if you've shared those commits with anyone already.

This section of Pro Git has useful information about rewriting history in this way.


Alternatively, this is a simple enough situation that you could just do:

git reset --soft HEAD^
git commit --amend

However, if the two commits aren't right at the tip of your branch, interactive rebase is a better approach.

Upvotes: 0

knittl
knittl

Reputation: 265261

If you haven't pushed your changes, use interactive git rebase and mark those commits as fixup:

git rebase -i HEAD~5

# opens editor with:
pick  deadbeaf commit
fixup abcd123  small change # edit this line to say "fixup", then save and exit editor
pick  3133715  another commit

Alternatively, if you discover you forgot some changes right after committing, add that change to the index and use git commit --amend to add it to your HEAD commit

Upvotes: 1

Related Questions