Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: generate a single patch across multiple commits

Tags:

git

patch

This is the situation:

  1. We created a "private" repo (say our-repo) based off an existing open-source git repo (say source-repo).

  2. We have been developing code and have around 20 merges into our repo. So, the repo moved from "State_Initial" to "State_Current".

Now, for business reasons, we want to hand-over all our development to a third party. Based on some legal issues, the only option is we give them a "single" patch file with all our changes. That is a squashed patch between "State_Initial" and "State_Current".

I looked around, and found

git format-patch -X

But, it generates "n" .patch files.

Is there a way to create a single patch file, so that if we create a repo based off "source-repo" and apply the patch, it takes us to "State_Current"?

like image 238
SimpleCoder Avatar asked Oct 19 '18 00:10

SimpleCoder


People also ask

How do I create a patch of committed changes 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 is a patch in git?

Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository. Jerry implements the strcat function for his project.


2 Answers

The following command creates a single .patch file that contains multiple commits.

git format-patch cc1dde0dd^..6de6d4b06 --stdout > foo.patch

You can then apply it like so:

git am foo.patch

Note: Be sure to use ^.. instead of .. if you want the first commit SHA to be included.

like image 93
aleclarson Avatar answered Oct 21 '22 06:10

aleclarson


Create a new branch named squashed that has a single squashed commit. This branch will have the exact same contents as your normal branch but none of the history.

Look it over and if you're satisfied, use format-patch to create a patch file.

$ git checkout -b squashed $(git commit-tree HEAD^{tree} -m 'Squashed history')
$ git format-patch --root HEAD

This is a non-destructive operation and you can switch right back to your normal development branch afterwards. You can tag the squashed branch to save a reference to what you e-mailed them, or use branch -D to delete it if you no longer need it.

$ git branch -D squashed
like image 2
John Kugelman Avatar answered Oct 21 '22 07:10

John Kugelman