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

2022/6/29 23:21:43

本文主要是介绍Create a git patch from the uncommitted changes in the current working directory,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

https://stackoverflow.com/questions/5159185/create-a-git-patch-from-the-uncommitted-changes-in-the-current-working-directory

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

Ask Question Asked 11 years, 4 months ago Modified 1 month ago   1163

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

gitgit-patch se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow" data-controller="se-share-sheet s-popover" data-gps-track="post.click({ item: 2, priv: 0, post_type: 1 })" data-s-popover-placement="bottom-start" data-se-share-sheet-license-name="CC BY-SA 4.0" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-location="1" data-se-share-sheet-post-type="question" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-subtitle="" data-se-share-sheet-title="Share a link to this question" title="Short permalink to this question" href="https://stackoverflow.com/q/5159185" rel="external nofollow" target="_blank">Share   edited Aug 22, 2020 at 9:32 user avatar Antony 3,18222 gold badges2929 silver badges4242 bronze badges asked Mar 1, 2011 at 19:14 user avatar vrish88 18.6k88 gold badges3737 silver badges5656 bronze badges
  • 45 Accepted answer should probably be changed, given the second answer is nearly four times more popular.  – Tim Ogilvy  Feb 19, 2018 at 12:37
  • 6 @TimOgilvy agreed. OP should do it. Second answer is far more popular and gives more information  – John Demetriou  Apr 19, 2018 at 7:21
  • 1 I think it worth to mention you need patch from uncommitted changes in the title either.  – 2i3r  Feb 4, 2020 at 11:09
Add a comment

8 Answers

2348

If you haven't yet commited the changes, then:

git diff > mypatch.patch

But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (git add each file, or just git add .) but don't do the commit, and then:

git diff --cached > mypatch.patch

Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

git diff --cached --binary > mypatch.patch

You can later apply the patch:

git apply mypatch.patch
se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow" data-controller="se-share-sheet s-popover" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-s-popover-placement="bottom-start" data-se-share-sheet-license-name="CC BY-SA 4.0" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-location="2" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-subtitle="" data-se-share-sheet-title="Share a link to this answer" title="Short permalink to this answer" href="https://stackoverflow.com/a/15438863" rel="external nofollow" target="_blank">Share   edited Nov 6, 2020 at 5:52     answered Mar 15, 2013 at 17:43 user avatar jcarballo 25.2k33 gold badges2525 silver badges2727 bronze badges
  • 6 I did exactly that and got "fatal: unrecognized input" upon executing git apply. Any idea what can cause this and how to fix it?  – Vitaly  Dec 22, 2013 at 20:11
  • 9 @Vitaly: is your patch readable if you open it with a text editor? it should be clean with no strange characters, for example if the color.diff setting is set your patch will have some 'color characters' that can make 'git apply' fail, in that case try git diff --no-color. Otherwise, it looks like an encoding problem.  – jcarballo  Dec 22, 2013 at 21:06
  • 8 To create the patch from the already staged changes you could also do git diff --staged > mypatch.patch, because --staged is a synonym for --cached. I think it easier to remember.  – matthaeus  Mar 3, 2017 at 16:52
  • 3 Related to "new files that are untracked": "git diff" and "git diff --cached" only work if "git add <file>" has been called first. (I am new to git and wondered why I got an empty patch everytime)  – Anonymous  Apr 25, 2017 at 8:41
  • 5 This got me out of a strange merge/rebase hell pretty easily, thanks :)  – John Hunt  Aug 31, 2017 at 10:34
Show 8 more comments   532  

git diff for unstaged changes.

git diff --cached for staged changes.

git diff HEAD for both staged and unstaged changes.

se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow" data-controller="se-share-sheet s-popover" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-s-popover-placement="bottom-start" data-se-share-sheet-license-name="CC BY-SA 4.0" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-location="2" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-subtitle="" data-se-share-sheet-title="Share a link to this answer" title="Short permalink to this answer" href="https://stackoverflow.com/a/5159205" rel="external nofollow" target="_blank">Share   edited Sep 5, 2020 at 19:30     answered Mar 1, 2011 at 19:16 user avatar sigjuice 27.3k1111 gold badges6565 silver badges9393 bronze badges
  • 20 yup, git diff is the inverse of git apply  – Spike Gronim  Mar 1, 2011 at 19:20
  • 40 git format-patch also includes binary diffs and some meta info. Actually that would be the best bet for creating a patch, but afaik this does only work for checked in sources/ changes, right?  – Eric  Mar 18, 2012 at 12:24
  • 26 Sometimes it might be useful to create a patch relative to the current directory. To achieve this, use git diff --relative  – ejboy  Jan 8, 2013 at 14:03 
  • 38 git diff > a.patch to write it to a file  – qasimzee  Feb 12, 2013 at 11:33
  • 152 Terse bordering on sarcastic, the answer below is more helpful.  – Air  Dec 7, 2013 at 19:03
Show 8 more comments 101

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 (SinceFETCH_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
se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow" data-controller="se-share-sheet s-popover" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-s-popover-placement="bottom-start" data-se-share-sheet-license-name="CC BY-SA 3.0" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-location="2" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-subtitle="" data-se-share-sheet-title="Share a link to this answer" title="Short permalink to this answer" href="https://stackoverflow.com/a/9864603" rel="external nofollow" target="_blank">Share   answered Mar 25, 2012 at 22:18 user avatar Merlyn Morgan-Graham 56.7k1616 gold badges122122 silver badges179179 bronze badges
  • 5 Technically this does require creating a commit which OP asked to avoid, but it's a temporary one and the answer is useful regardless.  – davenpcj  Jan 24, 2014 at 21:25
  47

To create a patch with both modified & new files (staged) you can run:

git diff HEAD > file_name.patch
se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow" data-controller="se-share-sheet s-popover" data-gps-track="post.click({ item: 2, priv: 0, post_type: 2 })" data-s-popover-placement="bottom-start" data-se-share-sheet-license-name="CC BY-SA 3.0" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-location="2" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-subtitle="" data-se-share-sheet-title="Share a link to this answer" title="Short permalink to this answer" href="https://stackoverflow.com/a/33098742" rel="external nofollow" target="_blank">Share   answered Oct 13, 2015 at 9:17 user avatar Ionel Sirbu 48744 silver badges33 bronze badges
  • 1 Thanks, in my case, this answer works, but git diff --cached > mypatch.patch is not working.  – mining  Sep 23, 2016 at 6:12
  •   I have a question: can file_name.patch be used by the patch command? Are they compatible with each other?  – Rakshith Ravi  Sep 4, 2019 at 7:58
  • 1 git diff + git diff --cached/staged == git diff HEAD (show all the changes since the last commit)  – K. Symbol  Feb 12, 2020 at 5:17
  • 1 @RakshithRavi afaik, yes they are. you may use your patch created by git diff HEAD > file-name.patch e.g. as follows: patch --forward --strip=1 < file-name.patch  – whyer  Jul 23, 2020 at 19:19
Add a comment

这篇关于Create a git patch from the uncommitted changes in the current working directory的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程