Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you squash commits into one patch with git format-patch?

People also ask

How do you squash commits to one commit?

On any given line you can change the command from pick to a command of your choice. I prefer to use the command fixup as this "squashes" the commit's changes into the commit on the line above and discards the commit's message. As the commit on line 1 is HEAD , in most cases you would leave this as pick .

How do I format a patch in git?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.

What command can you use to squash several commits into one commit?

In this case, we can use the commit git merge –squash command to achieve that. As the output above shows, in this Git repository, we've implemented “Feature2” in the feature branch. In our feature branch, we've made four commits.


I'd recommend doing this on a throwaway branch as follows. If your commits are in the "newlines" branch and you have switched back to your "master" branch already, this should do the trick:

[adam@mbp2600 example (master)]$ git checkout -b tmpsquash
Switched to a new branch "tmpsquash"

[adam@mbp2600 example (tmpsquash)]$ git merge --squash newlines
Updating 4d2de39..b6768b2
Fast forward
Squash commit -- not updating HEAD
 test.txt |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

[adam@mbp2600 example (tmpsquash)]$ git commit -a -m "My squashed commits"
[tmpsquash]: created 75b0a89: "My squashed commits"
 1 files changed, 2 insertions(+), 0 deletions(-)

[adam@mbp2600 example (tmpsquash)]$ git format-patch master
0001-My-squashed-commits.patch

Just to add one more solution to the pot: If you use this instead:

git format-patch master --stdout > my_new_patch.diff

Then it will still be 8 patches... but they'll all be in a single patchfile and will apply as one with:

git am < my_new_patch.diff

I always use git diff so in your example, something like

git diff master > patch.txt

As you already know, a git format-patch -8 HEAD will give you eight patches.

If you want your 8 commits appear as one, and do not mind rewriting the history of your branch (o-o-X-A-B-C-D-E-F-G-H), you could :

git rebase -i
// squash A, B, C, D, E ,F, G into H

or, and it would be a better solution, replay all your 8 commits from X (the commit before your 8 commits) on a new branch

git branch delivery X
git checkout delivery
git merge --squash master
git format-patch HEAD

That way, you only have one commit on the "delivery" branch, and it represent all your last 8 commits