Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `git show` to create and apply patches spanning multiple commits

Lately I've been using git show <hash> to create diffs for later reference because it's easier to type than git diff <hash>~ <hash> and it shows the commit information (timestamp, user, hash, comment). You can then use git apply <filename> to apply the patch.

I discovered that git show -3 will show the last three commits along with the same extra information. However, git apply will squash it all into the working directory as unstaged changes, and loses all the commit information.

Is there something in git that will apply all that information? It would be a lot simpler to just pass in a flag than breaking the patch up into three files, applying them severally, and creating new commits.

like image 750
redbmk Avatar asked Oct 12 '12 22:10

redbmk


People also ask

How do I create a patch in git bash?

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 git patch command?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections.

What is git diff patch?

Git diff is a command used to output the changes between two sources inside the git repository. The data sources can be two different branches, commits, files, etc.


1 Answers

You can use git format-patch to generate MIME emails representing the commits, including their metadata (message, authorship, etc). You can then reapply these with git am.

So git format-patch HEAD~3 will generate 3 patches for the last 3 commits, and you can then pipe these all into git am. If you want to be simpler, git format-patch --stdout HEAD~3 will send the MIME messages out on stdout, so you can pipe them around where you want instead of dealing with 3 separate files.

Of course, if you're trying to save commits to reference later, why not just tag them? You can then reapply the commits from them using git cherry-pick.

like image 140
Lily Ballard Avatar answered Sep 17 '22 18:09

Lily Ballard