Create a git patch from the uncommitted changes in the current working directory

后端 未结 7 1492
小蘑菇
小蘑菇 2020-11-27 08:53

Say I have uncommitted changes in my working directory. How can I make a patch from those without having to create a commit?

相关标签:
7条回答
  • 2020-11-27 09:35

    git diff and git apply will work for text files, but won't work for binary files.

    You can easily create a full binary patch, but you will have to create a temporary commit. Once you've made your temporary commit(s), you can create the patch with:

    git format-patch <options...>
    

    After you've made the patch, run this command:

    git reset --mixed <SHA of commit *before* your working-changes commit(s)>
    

    This will roll back your temporary commit(s). The final result leaves your working copy (intentionally) dirty with the same changes you originally had.

    On the receiving side, you can use the same trick to apply the changes to the working copy, without having the commit history. Simply apply the patch(es), and git reset --mixed <SHA of commit *before* the patches>.

    Note that you might have to be well-synced for this whole option to work. I've seen some errors when applying patches when the person making them hadn't pulled down as many changes as I had. There are probably ways to get it to work, but I haven't looked far into it.


    Here's how to create the same patches in Tortoise Git (not that I recommend using that tool):

    1. Commit your working changes
    2. Right click the branch root directory and click Tortoise Git -> Create Patch Serial
      1. Choose whichever range makes sense (Since: FETCH_HEAD will work if you're well-synced)
      2. Create the patch(es)
    3. Right click the branch root directory and click Tortise Git -> Show Log
    4. Right click the commit before your temporary commit(s), and click reset "<branch>" to this...
    5. Select the Mixed option

    And how to apply them:

    1. Right click the branch root directory and click Tortoise Git -> Apply Patch Serial
    2. Select the correct patch(es) and apply them
    3. Right click the branch root directory and click Tortise Git -> Show Log
    4. Right click the commit before the patch's commit(s), and click reset "<branch>" to this...
    5. Select the Mixed option
    0 讨论(0)
提交回复
热议问题