Generate a git patch for a specific commit










1014














I need to write a script that create patches for a list of SHA1 commit numbers.



I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. After a few hundred patches were generated, I had to kill the process.



Is there a way to generate a patch only for the specific SHA1?










share|improve this question




























    1014














    I need to write a script that create patches for a list of SHA1 commit numbers.



    I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. After a few hundred patches were generated, I had to kill the process.



    Is there a way to generate a patch only for the specific SHA1?










    share|improve this question


























      1014












      1014








      1014


      323





      I need to write a script that create patches for a list of SHA1 commit numbers.



      I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. After a few hundred patches were generated, I had to kill the process.



      Is there a way to generate a patch only for the specific SHA1?










      share|improve this question















      I need to write a script that create patches for a list of SHA1 commit numbers.



      I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. After a few hundred patches were generated, I had to kill the process.



      Is there a way to generate a patch only for the specific SHA1?







      git patch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '17 at 19:18









      JasonMArcher

      9,087104749




      9,087104749










      asked Jul 12 '11 at 0:35









      elle

      5,0903104




      5,0903104






















          9 Answers
          9






          active

          oldest

          votes


















          1635














          Try:



          git format-patch -1 <sha>


          or



          git format-patch -1 HEAD


          According to the documentation link above, the -1 flag tells git how many commits should be included in the patch;




          -<n>



              
          Prepare patches from the topmost commits.





          Apply the patch with the command:



          git am < file.patch





          share|improve this answer


















          • 153




            Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
            – Adrian
            Mar 25 '14 at 14:15







          • 2




            It does not seem to work if the last commit is a merge from another branch.
            – Lex Li
            Apr 4 '14 at 3:52






          • 5




            Even easier with 1.8.5 git format-patch -1 @
            – Evan Purkhiser
            Jun 17 '14 at 8:09






          • 22




            Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
            – Matt
            Aug 5 '15 at 21:11






          • 3




            This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
            – Kristof Dombi
            Nov 22 '16 at 13:08



















          248














          For generating the patches from the topmost commits from a specific sha1 hash:



          git format-patch -<n> <SHA1>


          The last 10 patches from head in a single patch file:



          git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch





          share|improve this answer
















          • 1




            can you please be kind enough to provide an example for the first command
            – Kasun Siyambalapitiya
            Dec 14 '16 at 6:22










          • git format-patch -1 HEAD will generate patch for the most recent commit
            – Sriram Murali
            Dec 14 '16 at 18:36






          • 1




            pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
            – Kasun Siyambalapitiya
            Dec 15 '16 at 3:19


















          69














          Say you have commit id 2 after commit 1 you would be able to run:



          git diff 2 1 > mypatch.diff


          where 2 and 1 are SHA hashes.






          share|improve this answer




















          • Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
            – elle
            Jul 12 '11 at 0:45







          • 9




            @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
            – J-16 SDiZ
            Jul 12 '11 at 0:52






          • 2




            git show HEAD > mypatch.diff while you're on the commit should do the same.
            – andho
            Apr 21 '15 at 12:49










          • @dookehester is it correct or is it the other way, git diff 1 2
            – Kasun Siyambalapitiya
            Dec 14 '16 at 6:25










          • I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
            – Rondo
            Aug 8 '17 at 1:39


















          52














          This command (as suggested already by @Naftuli Tzvi Kay):



          git format-patch -1 HEAD


          Replace HEAD with specific hash or range.



          will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.




          -<n> - Prepare patches from the topmost commits.




          Then you can re-apply the patch file in a mailbox format by:



          git am -3k 001*.patch


          See: man git-format-patch.






          share|improve this answer






















          • Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
            – Mike S
            Jun 9 '17 at 15:57






          • 1




            Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
            – underscore_d
            Oct 4 '17 at 22:30



















          20














          git format-patch commit_Id~1..commit_Id 
          git apply patch-file-name


          Fast and simple solution.






          share|improve this answer


















          • 3




            Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
            – iamantony
            Sep 13 '17 at 10:04



















          7














          If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base



          git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

          # or
          git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

          # or
          git config format.useAutoBase true
          git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE


          See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
          (Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)





          format-patch: add '--base' option to record base tree info



          Maintainers or third party testers may want to know the exact base tree
          the patch series applies to. Teach git format-patch a '--base' option
          to record the base tree info and append it at the end of the first
          message (either the cover letter or the first patch in the series).



          The base tree info consists of the "base commit", which is a well-known
          commit that is part of the stable part of the project history everybody
          else works off of, and zero or more "prerequisite patches", which are
          well-known patches in flight that is not yet part of the "base commit"
          that need to be applied on top of "base commit" in topological order
          before the patches can be applied.



          The "base commit" is shown as "base-commit: " followed by the 40-hex of
          the commit object name.

          A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.







          share|improve this answer




























            6














            To generate path from a specific commit (not the last commit):



            git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE





            share|improve this answer




























              3














              if you just want diff the specified file, you can :




              git diff master 766eceb -- connections/ > 000-mysql-connector.patch







              share|improve this answer




























                -1














                What is the way to generate a patch only for the specific SHA1?



                It's quite simple:



                Option 1. git show commitID > myFile.patch



                Option 2. git commitID~1..commitID > myFile.patch



                Note: Replace commitID with actual commit id (SHA1 commit code).






                share|improve this answer


















                • 2




                  Option 1 is downright wrong and is unrelated to the question.
                  – Anshuman Manral
                  Mar 28 '18 at 7:39






                • 2




                  Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                  – Anshuman Manral
                  Mar 28 '18 at 8:44











                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6658313%2fgenerate-a-git-patch-for-a-specific-commit%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                9 Answers
                9






                active

                oldest

                votes








                9 Answers
                9






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1635














                Try:



                git format-patch -1 <sha>


                or



                git format-patch -1 HEAD


                According to the documentation link above, the -1 flag tells git how many commits should be included in the patch;




                -<n>



                    
                Prepare patches from the topmost commits.





                Apply the patch with the command:



                git am < file.patch





                share|improve this answer


















                • 153




                  Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                  – Adrian
                  Mar 25 '14 at 14:15







                • 2




                  It does not seem to work if the last commit is a merge from another branch.
                  – Lex Li
                  Apr 4 '14 at 3:52






                • 5




                  Even easier with 1.8.5 git format-patch -1 @
                  – Evan Purkhiser
                  Jun 17 '14 at 8:09






                • 22




                  Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                  – Matt
                  Aug 5 '15 at 21:11






                • 3




                  This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                  – Kristof Dombi
                  Nov 22 '16 at 13:08
















                1635














                Try:



                git format-patch -1 <sha>


                or



                git format-patch -1 HEAD


                According to the documentation link above, the -1 flag tells git how many commits should be included in the patch;




                -<n>



                    
                Prepare patches from the topmost commits.





                Apply the patch with the command:



                git am < file.patch





                share|improve this answer


















                • 153




                  Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                  – Adrian
                  Mar 25 '14 at 14:15







                • 2




                  It does not seem to work if the last commit is a merge from another branch.
                  – Lex Li
                  Apr 4 '14 at 3:52






                • 5




                  Even easier with 1.8.5 git format-patch -1 @
                  – Evan Purkhiser
                  Jun 17 '14 at 8:09






                • 22




                  Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                  – Matt
                  Aug 5 '15 at 21:11






                • 3




                  This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                  – Kristof Dombi
                  Nov 22 '16 at 13:08














                1635












                1635








                1635






                Try:



                git format-patch -1 <sha>


                or



                git format-patch -1 HEAD


                According to the documentation link above, the -1 flag tells git how many commits should be included in the patch;




                -<n>



                    
                Prepare patches from the topmost commits.





                Apply the patch with the command:



                git am < file.patch





                share|improve this answer














                Try:



                git format-patch -1 <sha>


                or



                git format-patch -1 HEAD


                According to the documentation link above, the -1 flag tells git how many commits should be included in the patch;




                -<n>



                    
                Prepare patches from the topmost commits.





                Apply the patch with the command:



                git am < file.patch






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 '18 at 20:01









                Adam Liss

                40.6k1193131




                40.6k1193131










                answered Jul 12 '11 at 0:43









                manojlds

                211k46369358




                211k46369358







                • 153




                  Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                  – Adrian
                  Mar 25 '14 at 14:15







                • 2




                  It does not seem to work if the last commit is a merge from another branch.
                  – Lex Li
                  Apr 4 '14 at 3:52






                • 5




                  Even easier with 1.8.5 git format-patch -1 @
                  – Evan Purkhiser
                  Jun 17 '14 at 8:09






                • 22




                  Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                  – Matt
                  Aug 5 '15 at 21:11






                • 3




                  This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                  – Kristof Dombi
                  Nov 22 '16 at 13:08













                • 153




                  Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                  – Adrian
                  Mar 25 '14 at 14:15







                • 2




                  It does not seem to work if the last commit is a merge from another branch.
                  – Lex Li
                  Apr 4 '14 at 3:52






                • 5




                  Even easier with 1.8.5 git format-patch -1 @
                  – Evan Purkhiser
                  Jun 17 '14 at 8:09






                • 22




                  Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                  – Matt
                  Aug 5 '15 at 21:11






                • 3




                  This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                  – Kristof Dombi
                  Nov 22 '16 at 13:08








                153




                153




                Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                – Adrian
                Mar 25 '14 at 14:15





                Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally.
                – Adrian
                Mar 25 '14 at 14:15





                2




                2




                It does not seem to work if the last commit is a merge from another branch.
                – Lex Li
                Apr 4 '14 at 3:52




                It does not seem to work if the last commit is a merge from another branch.
                – Lex Li
                Apr 4 '14 at 3:52




                5




                5




                Even easier with 1.8.5 git format-patch -1 @
                – Evan Purkhiser
                Jun 17 '14 at 8:09




                Even easier with 1.8.5 git format-patch -1 @
                – Evan Purkhiser
                Jun 17 '14 at 8:09




                22




                22




                Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                – Matt
                Aug 5 '15 at 21:11




                Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here.
                – Matt
                Aug 5 '15 at 21:11




                3




                3




                This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                – Kristof Dombi
                Nov 22 '16 at 13:08





                This command also works for just a specific file(s) from the commit: git format-patch -1 <sha> path/to/file.js This will create a patch only containing the diffs for the file.js
                – Kristof Dombi
                Nov 22 '16 at 13:08














                248














                For generating the patches from the topmost commits from a specific sha1 hash:



                git format-patch -<n> <SHA1>


                The last 10 patches from head in a single patch file:



                git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch





                share|improve this answer
















                • 1




                  can you please be kind enough to provide an example for the first command
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:22










                • git format-patch -1 HEAD will generate patch for the most recent commit
                  – Sriram Murali
                  Dec 14 '16 at 18:36






                • 1




                  pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                  – Kasun Siyambalapitiya
                  Dec 15 '16 at 3:19















                248














                For generating the patches from the topmost commits from a specific sha1 hash:



                git format-patch -<n> <SHA1>


                The last 10 patches from head in a single patch file:



                git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch





                share|improve this answer
















                • 1




                  can you please be kind enough to provide an example for the first command
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:22










                • git format-patch -1 HEAD will generate patch for the most recent commit
                  – Sriram Murali
                  Dec 14 '16 at 18:36






                • 1




                  pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                  – Kasun Siyambalapitiya
                  Dec 15 '16 at 3:19













                248












                248








                248






                For generating the patches from the topmost commits from a specific sha1 hash:



                git format-patch -<n> <SHA1>


                The last 10 patches from head in a single patch file:



                git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch





                share|improve this answer












                For generating the patches from the topmost commits from a specific sha1 hash:



                git format-patch -<n> <SHA1>


                The last 10 patches from head in a single patch file:



                git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 23 '13 at 14:34









                Sriram Murali

                3,58112028




                3,58112028







                • 1




                  can you please be kind enough to provide an example for the first command
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:22










                • git format-patch -1 HEAD will generate patch for the most recent commit
                  – Sriram Murali
                  Dec 14 '16 at 18:36






                • 1




                  pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                  – Kasun Siyambalapitiya
                  Dec 15 '16 at 3:19












                • 1




                  can you please be kind enough to provide an example for the first command
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:22










                • git format-patch -1 HEAD will generate patch for the most recent commit
                  – Sriram Murali
                  Dec 14 '16 at 18:36






                • 1




                  pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                  – Kasun Siyambalapitiya
                  Dec 15 '16 at 3:19







                1




                1




                can you please be kind enough to provide an example for the first command
                – Kasun Siyambalapitiya
                Dec 14 '16 at 6:22




                can you please be kind enough to provide an example for the first command
                – Kasun Siyambalapitiya
                Dec 14 '16 at 6:22












                git format-patch -1 HEAD will generate patch for the most recent commit
                – Sriram Murali
                Dec 14 '16 at 18:36




                git format-patch -1 HEAD will generate patch for the most recent commit
                – Sriram Murali
                Dec 14 '16 at 18:36




                1




                1




                pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                – Kasun Siyambalapitiya
                Dec 15 '16 at 3:19




                pardon me for asking this, so when it is -2 it generate patches for most recent 2 commits is it, and one more thing to for clarification is the command got format-patch -2 HEAD is same as the line git format-patch HEAD~2
                – Kasun Siyambalapitiya
                Dec 15 '16 at 3:19











                69














                Say you have commit id 2 after commit 1 you would be able to run:



                git diff 2 1 > mypatch.diff


                where 2 and 1 are SHA hashes.






                share|improve this answer




















                • Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                  – elle
                  Jul 12 '11 at 0:45







                • 9




                  @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                  – J-16 SDiZ
                  Jul 12 '11 at 0:52






                • 2




                  git show HEAD > mypatch.diff while you're on the commit should do the same.
                  – andho
                  Apr 21 '15 at 12:49










                • @dookehester is it correct or is it the other way, git diff 1 2
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:25










                • I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                  – Rondo
                  Aug 8 '17 at 1:39















                69














                Say you have commit id 2 after commit 1 you would be able to run:



                git diff 2 1 > mypatch.diff


                where 2 and 1 are SHA hashes.






                share|improve this answer




















                • Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                  – elle
                  Jul 12 '11 at 0:45







                • 9




                  @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                  – J-16 SDiZ
                  Jul 12 '11 at 0:52






                • 2




                  git show HEAD > mypatch.diff while you're on the commit should do the same.
                  – andho
                  Apr 21 '15 at 12:49










                • @dookehester is it correct or is it the other way, git diff 1 2
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:25










                • I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                  – Rondo
                  Aug 8 '17 at 1:39













                69












                69








                69






                Say you have commit id 2 after commit 1 you would be able to run:



                git diff 2 1 > mypatch.diff


                where 2 and 1 are SHA hashes.






                share|improve this answer












                Say you have commit id 2 after commit 1 you would be able to run:



                git diff 2 1 > mypatch.diff


                where 2 and 1 are SHA hashes.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 12 '11 at 0:42









                brandon

                1,013813




                1,013813











                • Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                  – elle
                  Jul 12 '11 at 0:45







                • 9




                  @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                  – J-16 SDiZ
                  Jul 12 '11 at 0:52






                • 2




                  git show HEAD > mypatch.diff while you're on the commit should do the same.
                  – andho
                  Apr 21 '15 at 12:49










                • @dookehester is it correct or is it the other way, git diff 1 2
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:25










                • I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                  – Rondo
                  Aug 8 '17 at 1:39
















                • Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                  – elle
                  Jul 12 '11 at 0:45







                • 9




                  @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                  – J-16 SDiZ
                  Jul 12 '11 at 0:52






                • 2




                  git show HEAD > mypatch.diff while you're on the commit should do the same.
                  – andho
                  Apr 21 '15 at 12:49










                • @dookehester is it correct or is it the other way, git diff 1 2
                  – Kasun Siyambalapitiya
                  Dec 14 '16 at 6:25










                • I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                  – Rondo
                  Aug 8 '17 at 1:39















                Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                – elle
                Jul 12 '11 at 0:45





                Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that.
                – elle
                Jul 12 '11 at 0:45





                9




                9




                @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                – J-16 SDiZ
                Jul 12 '11 at 0:52




                @elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better)
                – J-16 SDiZ
                Jul 12 '11 at 0:52




                2




                2




                git show HEAD > mypatch.diff while you're on the commit should do the same.
                – andho
                Apr 21 '15 at 12:49




                git show HEAD > mypatch.diff while you're on the commit should do the same.
                – andho
                Apr 21 '15 at 12:49












                @dookehester is it correct or is it the other way, git diff 1 2
                – Kasun Siyambalapitiya
                Dec 14 '16 at 6:25




                @dookehester is it correct or is it the other way, git diff 1 2
                – Kasun Siyambalapitiya
                Dec 14 '16 at 6:25












                I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                – Rondo
                Aug 8 '17 at 1:39




                I tend to use --no-prefix and apply with 'patch -p0 < patch file' ... patch files made this way also work interchangeably with subversion's diff output
                – Rondo
                Aug 8 '17 at 1:39











                52














                This command (as suggested already by @Naftuli Tzvi Kay):



                git format-patch -1 HEAD


                Replace HEAD with specific hash or range.



                will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.




                -<n> - Prepare patches from the topmost commits.




                Then you can re-apply the patch file in a mailbox format by:



                git am -3k 001*.patch


                See: man git-format-patch.






                share|improve this answer






















                • Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                  – Mike S
                  Jun 9 '17 at 15:57






                • 1




                  Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                  – underscore_d
                  Oct 4 '17 at 22:30
















                52














                This command (as suggested already by @Naftuli Tzvi Kay):



                git format-patch -1 HEAD


                Replace HEAD with specific hash or range.



                will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.




                -<n> - Prepare patches from the topmost commits.




                Then you can re-apply the patch file in a mailbox format by:



                git am -3k 001*.patch


                See: man git-format-patch.






                share|improve this answer






















                • Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                  – Mike S
                  Jun 9 '17 at 15:57






                • 1




                  Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                  – underscore_d
                  Oct 4 '17 at 22:30














                52












                52








                52






                This command (as suggested already by @Naftuli Tzvi Kay):



                git format-patch -1 HEAD


                Replace HEAD with specific hash or range.



                will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.




                -<n> - Prepare patches from the topmost commits.




                Then you can re-apply the patch file in a mailbox format by:



                git am -3k 001*.patch


                See: man git-format-patch.






                share|improve this answer














                This command (as suggested already by @Naftuli Tzvi Kay):



                git format-patch -1 HEAD


                Replace HEAD with specific hash or range.



                will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.




                -<n> - Prepare patches from the topmost commits.




                Then you can re-apply the patch file in a mailbox format by:



                git am -3k 001*.patch


                See: man git-format-patch.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 23 '17 at 12:18


























                community wiki





                2 revs
                kenorb












                • Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                  – Mike S
                  Jun 9 '17 at 15:57






                • 1




                  Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                  – underscore_d
                  Oct 4 '17 at 22:30

















                • Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                  – Mike S
                  Jun 9 '17 at 15:57






                • 1




                  Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                  – underscore_d
                  Oct 4 '17 at 22:30
















                Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                – Mike S
                Jun 9 '17 at 15:57




                Thanks! I think it's worth noting that applying the patch will create a commit with a commit message prefixed by [PATCH]. That's easy to fix though
                – Mike S
                Jun 9 '17 at 15:57




                1




                1




                Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                – underscore_d
                Oct 4 '17 at 22:30





                Phenomenal. OP, you haven't accepted this, because...? @MikeS No, it doesn't, anymore than any other git-formatted patch does, at least if the user applies it in the correct way.
                – underscore_d
                Oct 4 '17 at 22:30












                20














                git format-patch commit_Id~1..commit_Id 
                git apply patch-file-name


                Fast and simple solution.






                share|improve this answer


















                • 3




                  Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                  – iamantony
                  Sep 13 '17 at 10:04
















                20














                git format-patch commit_Id~1..commit_Id 
                git apply patch-file-name


                Fast and simple solution.






                share|improve this answer


















                • 3




                  Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                  – iamantony
                  Sep 13 '17 at 10:04














                20












                20








                20






                git format-patch commit_Id~1..commit_Id 
                git apply patch-file-name


                Fast and simple solution.






                share|improve this answer














                git format-patch commit_Id~1..commit_Id 
                git apply patch-file-name


                Fast and simple solution.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 12 '17 at 16:06









                iamantony

                8721027




                8721027










                answered Jul 6 '17 at 13:52









                zdrsoft

                73977




                73977







                • 3




                  Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                  – iamantony
                  Sep 13 '17 at 10:04













                • 3




                  Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                  – iamantony
                  Sep 13 '17 at 10:04








                3




                3




                Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                – iamantony
                Sep 13 '17 at 10:04





                Also do not forget to call git apply --check patch-file-name before applying a patch. This will help to avoid problems.
                – iamantony
                Sep 13 '17 at 10:04












                7














                If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base



                git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

                # or
                git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

                # or
                git config format.useAutoBase true
                git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE


                See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
                (Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)





                format-patch: add '--base' option to record base tree info



                Maintainers or third party testers may want to know the exact base tree
                the patch series applies to. Teach git format-patch a '--base' option
                to record the base tree info and append it at the end of the first
                message (either the cover letter or the first patch in the series).



                The base tree info consists of the "base commit", which is a well-known
                commit that is part of the stable part of the project history everybody
                else works off of, and zero or more "prerequisite patches", which are
                well-known patches in flight that is not yet part of the "base commit"
                that need to be applied on top of "base commit" in topological order
                before the patches can be applied.



                The "base commit" is shown as "base-commit: " followed by the 40-hex of
                the commit object name.

                A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.







                share|improve this answer

























                  7














                  If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base



                  git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

                  # or
                  git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

                  # or
                  git config format.useAutoBase true
                  git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE


                  See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
                  (Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)





                  format-patch: add '--base' option to record base tree info



                  Maintainers or third party testers may want to know the exact base tree
                  the patch series applies to. Teach git format-patch a '--base' option
                  to record the base tree info and append it at the end of the first
                  message (either the cover letter or the first patch in the series).



                  The base tree info consists of the "base commit", which is a well-known
                  commit that is part of the stable part of the project history everybody
                  else works off of, and zero or more "prerequisite patches", which are
                  well-known patches in flight that is not yet part of the "base commit"
                  that need to be applied on top of "base commit" in topological order
                  before the patches can be applied.



                  The "base commit" is shown as "base-commit: " followed by the 40-hex of
                  the commit object name.

                  A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.







                  share|improve this answer























                    7












                    7








                    7






                    If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base



                    git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

                    # or
                    git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

                    # or
                    git config format.useAutoBase true
                    git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE


                    See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
                    (Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)





                    format-patch: add '--base' option to record base tree info



                    Maintainers or third party testers may want to know the exact base tree
                    the patch series applies to. Teach git format-patch a '--base' option
                    to record the base tree info and append it at the end of the first
                    message (either the cover letter or the first patch in the series).



                    The base tree info consists of the "base commit", which is a well-known
                    commit that is part of the stable part of the project history everybody
                    else works off of, and zero or more "prerequisite patches", which are
                    well-known patches in flight that is not yet part of the "base commit"
                    that need to be applied on top of "base commit" in topological order
                    before the patches can be applied.



                    The "base commit" is shown as "base-commit: " followed by the 40-hex of
                    the commit object name.

                    A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.







                    share|improve this answer












                    If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base



                    git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE

                    # or
                    git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE

                    # or
                    git config format.useAutoBase true
                    git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE


                    See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
                    (Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)





                    format-patch: add '--base' option to record base tree info



                    Maintainers or third party testers may want to know the exact base tree
                    the patch series applies to. Teach git format-patch a '--base' option
                    to record the base tree info and append it at the end of the first
                    message (either the cover letter or the first patch in the series).



                    The base tree info consists of the "base commit", which is a well-known
                    commit that is part of the stable part of the project history everybody
                    else works off of, and zero or more "prerequisite patches", which are
                    well-known patches in flight that is not yet part of the "base commit"
                    that need to be applied on top of "base commit" in topological order
                    before the patches can be applied.



                    The "base commit" is shown as "base-commit: " followed by the 40-hex of
                    the commit object name.

                    A "prerequisite patch" is shown as "prerequisite-patch-id: " followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 24 '16 at 7:26









                    VonC

                    830k28926113157




                    830k28926113157





















                        6














                        To generate path from a specific commit (not the last commit):



                        git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE





                        share|improve this answer

























                          6














                          To generate path from a specific commit (not the last commit):



                          git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE





                          share|improve this answer























                            6












                            6








                            6






                            To generate path from a specific commit (not the last commit):



                            git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE





                            share|improve this answer












                            To generate path from a specific commit (not the last commit):



                            git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 4 '16 at 16:52









                            Makah

                            2,73123152




                            2,73123152





















                                3














                                if you just want diff the specified file, you can :




                                git diff master 766eceb -- connections/ > 000-mysql-connector.patch







                                share|improve this answer

























                                  3














                                  if you just want diff the specified file, you can :




                                  git diff master 766eceb -- connections/ > 000-mysql-connector.patch







                                  share|improve this answer























                                    3












                                    3








                                    3






                                    if you just want diff the specified file, you can :




                                    git diff master 766eceb -- connections/ > 000-mysql-connector.patch







                                    share|improve this answer












                                    if you just want diff the specified file, you can :




                                    git diff master 766eceb -- connections/ > 000-mysql-connector.patch








                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jun 1 '18 at 6:29









                                    jiahut

                                    884811




                                    884811





















                                        -1














                                        What is the way to generate a patch only for the specific SHA1?



                                        It's quite simple:



                                        Option 1. git show commitID > myFile.patch



                                        Option 2. git commitID~1..commitID > myFile.patch



                                        Note: Replace commitID with actual commit id (SHA1 commit code).






                                        share|improve this answer


















                                        • 2




                                          Option 1 is downright wrong and is unrelated to the question.
                                          – Anshuman Manral
                                          Mar 28 '18 at 7:39






                                        • 2




                                          Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                          – Anshuman Manral
                                          Mar 28 '18 at 8:44
















                                        -1














                                        What is the way to generate a patch only for the specific SHA1?



                                        It's quite simple:



                                        Option 1. git show commitID > myFile.patch



                                        Option 2. git commitID~1..commitID > myFile.patch



                                        Note: Replace commitID with actual commit id (SHA1 commit code).






                                        share|improve this answer


















                                        • 2




                                          Option 1 is downright wrong and is unrelated to the question.
                                          – Anshuman Manral
                                          Mar 28 '18 at 7:39






                                        • 2




                                          Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                          – Anshuman Manral
                                          Mar 28 '18 at 8:44














                                        -1












                                        -1








                                        -1






                                        What is the way to generate a patch only for the specific SHA1?



                                        It's quite simple:



                                        Option 1. git show commitID > myFile.patch



                                        Option 2. git commitID~1..commitID > myFile.patch



                                        Note: Replace commitID with actual commit id (SHA1 commit code).






                                        share|improve this answer














                                        What is the way to generate a patch only for the specific SHA1?



                                        It's quite simple:



                                        Option 1. git show commitID > myFile.patch



                                        Option 2. git commitID~1..commitID > myFile.patch



                                        Note: Replace commitID with actual commit id (SHA1 commit code).







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited May 19 '17 at 20:38









                                        msanford

                                        6,59364365




                                        6,59364365










                                        answered Jan 17 '17 at 9:40









                                        Ankush

                                        231




                                        231







                                        • 2




                                          Option 1 is downright wrong and is unrelated to the question.
                                          – Anshuman Manral
                                          Mar 28 '18 at 7:39






                                        • 2




                                          Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                          – Anshuman Manral
                                          Mar 28 '18 at 8:44













                                        • 2




                                          Option 1 is downright wrong and is unrelated to the question.
                                          – Anshuman Manral
                                          Mar 28 '18 at 7:39






                                        • 2




                                          Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                          – Anshuman Manral
                                          Mar 28 '18 at 8:44








                                        2




                                        2




                                        Option 1 is downright wrong and is unrelated to the question.
                                        – Anshuman Manral
                                        Mar 28 '18 at 7:39




                                        Option 1 is downright wrong and is unrelated to the question.
                                        – Anshuman Manral
                                        Mar 28 '18 at 7:39




                                        2




                                        2




                                        Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                        – Anshuman Manral
                                        Mar 28 '18 at 8:44





                                        Option 2 is also an invalid command. You will get error like: git a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0 git: 'a5f4bcaeb7fa7de27ae79d9522332e872889bbf0~1..a5f4bcaeb7fa7de27ae79d9522332e872889bbf0' is not a git command. See 'git --help'. Pleas check before posting answers
                                        – Anshuman Manral
                                        Mar 28 '18 at 8:44


















                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6658313%2fgenerate-a-git-patch-for-a-specific-commit%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo